Skip to main content
May 31, 2012
Question

Extendscript (JS): How to guard undefined object properties?

  • May 31, 2012
  • 1 reply
  • 4246 views

Just picked up this Extendscript for a quick utility function. I can't seem to use any of the "usual" Javascript logic contstructs to test for undefined object properties.

Example:

typeof(app.activeDocument) === "undefined"

This is an *error* in Extendscript (Error 1302: No such element). I don't believe it should be, but if I'm supposed to do something different in Extendscript I am all ears.

Thank you

Edit: Now posted to the correct forum.

This topic has been closed for replies.

1 reply

Paul Riggott
Inspiring
May 31, 2012

You would do something like...

if(documents.length){

alert(typeof(app.activeDocument));

}else{

    alert("There are no documents open.");

}

or use a try catch

May 31, 2012

I considered that as a solution, I guess I was hoping for an answer on why Extendscript doesn't support a common Javascript nomenclature (unless I'm mistaken and Extendscript isn't supposed to resemble Javascript).

No worries, thanks for the reply.

June 24, 2012

There are several properties that will throw an exception if the property is not defined. Using app.documents.length is the correct technique to use before accessing app.activeDocument.

This was a deliberate, though unfortunate, design decision on the part of Adobe. We discussed this in depth a few years ago.

-X


Do you have a link to that discussion? It’s clearly a deliberate API decision; I just think it’s annoying. Copying all the properties to a new object as with that nicer_obj function is a perfectly reasonable alternative method though, especially if you care about other properties beyond activeDocument that might or might not be set. (I don’t have any idea what those would be.)