Accessing Window Dialog Properties with variable names

Photoshop Script Snippets - Note: Full Scripts go in the Photoshop Scripts Forum

Moderators: Tom, Kukurykus

Andrew

Accessing Window Dialog Properties with variable names

Post by Andrew »

Say we have defined a Dialog window:

myWindow = new Window (myDialogRef);

It has a button, btn1, and we can do:

myWindow.btn1.onClick = function () {alert('something');}

Now say we have btn1 - btnN where N varies according to circumstance. Is there any tidy way to create a working list of

Window.btn1.onClick =
to
Window.btnN.onClick =

The following comes close but fails at the final post:

Code: Select allbtnAr = new Array();k = 0;

for (p in myWindow)
{
   if (p.substr(0,3) == 'btn') {btnAr[k] = myWindow[p]}
}

then...

btnAr[0].onClick = function () {dosomething} // this works

unfortunately

for (k=0;k<btnAr.length;k++) {
  btnAr[k].onClick = function () {dosomething} // this does not work
}


The alternative uses eval

Code: Select alltString = 'myWindow.btn1'; // which can be easily generated in a for loop

eval(tString).onClick = function () {dosomething} // works



Again it will fail once placed in a for loop. Maybe a bit more messing will get me there with eval but I still tend to view eval == evil, maybe I should get over it

Andrew
Andrew

Accessing Window Dialog Properties with variable names

Post by Andrew »

Yes it can be done with eval:

Code: Select alltString  = 'myWindow.btn1.onClick = function () {dosomething}
tString += 'myWindow.btn2.onClick = function () {dosomething}
tString += 'myWindow.btnN.onClick = function () {dosomething}

eval(tString);

(To tell the truth I think Xbytor put me onto this a while ago. I still don't like the eval thang but...).

Andrew
obiwan1129

Accessing Window Dialog Properties with variable names

Post by obiwan1129 »

Andrew, this was a very interesting idea. You mentioned that you had it worked out. Can you share the full code snippet for what you worked out for this?

Thanks.
Andrew

Accessing Window Dialog Properties with variable names

Post by Andrew »

I use this technique all the time now. Here is an example of a fairly complex dynamic dialog from a script I am currently working on. Obviously there are different variations on how it can be done but the fundamental point is simple - use eval to define the dialog variables within a for loop.

To make things easier, when an onClick event has a complex function, this function can be called by its function name within onClick and then defined the normal way outside the loop eg with

ctextAr["+i+"] = recordConditionStep(myData);

recordConditionStep(myData) is just a normal function.

Andrew
cmyk

Accessing Window Dialog Properties with variable names

Post by cmyk »

Hi Folks,

Would it be possible to get the full code snippet re-posted. Andrew's link from above is no longer valid for this aging thread:)

thanks
cmyk

Accessing Window Dialog Properties with variable names

Post by cmyk »

thanks