Skip to main content
August 27, 2010
Answered

Working with putCustomOptions and getCustomOptions

  • August 27, 2010
  • 1 reply
  • 1953 views

Hello,

I want to create a global variable with getCustomOptions:

var myCounter = 15;

var desc = new ActionDescriptor();// create a descriptor to hold value
desc.putInteger(0, myCounter);
app.putCustomOptions('myScript', desc, true );// store the descriptor by unique name

But first of all I want to check if this key/value was set before. If not, then I would execute the code above to set this "global variable".

How can I check if this key/value was set before? Maybe with "typeof xyz === 'undefined'" or something like that?

carlos

This topic has been closed for replies.
Correct answer Michael_L_Hale

Here is one way, try to get the customOptions first.

var myCounter;
// see if the counter is already stored
try{
     var desc = app.getCustomOptions('myScript');
}catch(e){}
if( undefined != desc ){// has counter stored
     myCounter = desc.getInteger(0);
     // check value - here if it is 0 reset to 15
     if( myCounter = 0 ) myCounter = 15;
}else{// not stored so set default
     myCounter = 15;
}
// do something here
myCounter=myCounter-1;// update var
// store the value
var desc = new ActionDescriptor();// create a descriptor to hold value
desc.putInteger(0, myCounter);
app.putCustomOptions('myScript', desc, true );// store the descriptor by unique name

1 reply

Michael_L_HaleCorrect answer
Inspiring
August 27, 2010

Here is one way, try to get the customOptions first.

var myCounter;
// see if the counter is already stored
try{
     var desc = app.getCustomOptions('myScript');
}catch(e){}
if( undefined != desc ){// has counter stored
     myCounter = desc.getInteger(0);
     // check value - here if it is 0 reset to 15
     if( myCounter = 0 ) myCounter = 15;
}else{// not stored so set default
     myCounter = 15;
}
// do something here
myCounter=myCounter-1;// update var
// store the value
var desc = new ActionDescriptor();// create a descriptor to hold value
desc.putInteger(0, myCounter);
app.putCustomOptions('myScript', desc, true );// store the descriptor by unique name

August 27, 2010

Hello Michael,

thank you for your code -- it works!

Michael L Hale wrote:

if( undefined != desc ){// has counter stored

One question: the code above is the same as

if (typeof desc !== "undefined") {

?

It seems to work, too.

Carlos

Inspiring
August 27, 2010

typeof returns a string which has the name of the data type. 'undefined' is one of the possible types so yes you could use your expression instead.

However !== is not a standard javascript operator. It can be used to compare both type and value. Because typeof always returns a string there is no need to check type. The != operator would be more standard and clearer as to the intent of the statement.

if (typeof desc != "undefined") {