Home > InDesign Scripts > QuickTip: Find Object Properties Quickly

QuickTip: Find Object Properties Quickly

Finding properties in OMV (Object Model Viewer) can be really painful, and slow. Personally, my best friend when writing scripts is Theunis de Jongs (aka Jongware) InDesign JavaScript Reference Guide but also trying to remember connections between objects and when to use which property, class, method… But, there is also another way to find out what properties you can use, and it’s already built in. Key is in Object reflection! So, let’s take a look how to put it in use! :)


For example, we want to get all properties available for Application Object. We can retrieve them very easily with reflect property: (Properties are returned in Array)

var myAppProperties = app.reflect.properties;

// display results
alert(myAppProperties);

Great, but, what is so great about this?! As we know, JavaScript is Case-sensitive language, so myProperty != myproperty :) so we can check have we typed property with correct case!

Further more, by combining this method with Scrollable alert by Peter Kahrel we get very great tool! Here is one example:

// Scrollable alert function
function alert_scroll (title, input){
   if (input instanceof Array)
       input = input.join ("\r");
   var w = new Window ("dialog", title);
   var list = w.add ("edittext", undefined, input, {multiline: true, scrolling: true});
   list.maximumSize.height = w.maximumSize.height-100;
   list.minimumSize.width = 250;
   w.add ("button", undefined, "Close", {name: "ok"});
   w.show ();
}

// object
var reflectProperties = app.colorSettings;

// display properties
alert_scroll("Object properties", reflectProperties.reflect.properties.sort());

I also sorted properties by adding simple sort() at the end.

That’s it!

Happy coding! :-D

About these ads
  1. January 25, 2011 at 00:05

    nice snip!

    i just added some functions… especially useful if you work with an active Selection!

    gregor

    // object
    var _objekt = app.colorSettings;
    //~ var _objekt = app.selection[0];

    var _resultArray = [];

    for (var i = 0; i < _objekt.reflect.properties.length; i++) {
    var _property = _objekt.reflect.properties[i];
    if (_property.toString() === "__proto__" || _property.toString() === "reflect" || _property.toString() === "properties") continue;
    try { _result = _objekt[_property]; }
    catch (e) { _result = "cannot read"; }
    if (_result != null && _result.constructor.name === "Array") _resultArray.push(_objekt.constructor.name + "." + _property + " = [" + _result + "]");
    else _resultArray.push(_objekt.constructor.name + "." + _property + " = " + _result);
    }

    // display properties
    alert_scroll("Object properties", _resultArray.sort());

    • January 25, 2011 at 21:19

      Hey Gregor!

      Thank you very much for this snip! It’s really great! I will update my post ASAP and add this to it! It’s really great way to get all property values! Thanks!

      Greetings from Serbia!
      Marijan.

  1. December 29, 2010 at 15:47

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 35 other followers

%d bloggers like this: