Run and Cancel Buttons in JS Windows

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

Moderators: Tom, Kukurykus

Andrew

Run and Cancel Buttons in JS Windows

Post by Andrew »

This is in response to a question from Stan about setting up run and cancel buttons in JS windows.

The first function is fully specified, using onClick for both run and cancel, but see what you get when you hit Esc or Enter as well.

The second function assigns the name OK and cancel to the two buttons, these will have their own built-in default onClick function - you do not need to specify it. Furthermore, if you use OK and Cancel in the text you do not even need the name property specification - a useful shortcut.

Once you have returned from the show statement, you can assign tha show value to a variable (in this case 'result') and use that to determine what to do next.

Code: Select allrsW();
   
function rsW () {
   var rsWR =
         "dialog { text: 'AH KB Resize Settings', bounds:[100,20,310,80]], \
         s1: StaticText { text:'Width mm', bounds:[10, 5, 70, 25]}, \
         w: EditText { text:'100', bounds:[75, 5, 120, 25]}, \
         s2: StaticText { text:'Height mm', bounds:[10, 30, 70, 50]}, \
         h: EditText { text:'200', bounds:[75, 30, 120, 50]}, \
         runBtn: Button { text:'Run', bounds:[130, 5, 200, 25]}, \
         quitBtn: Button { text:'Quit', bounds:[130, 30, 200, 50]} \
   }";
   var rsD = new Window (rsWR);
   rsD.runBtn.onClick = function() {rsD.close(3);}
   rsD.quitBtn.onClick = function() {rsD.close(4);}
   var result = rsD.show();
   alert(result + '\n' + rsD.w.text + '\n' + rsD.h.text);
}

function rsW2 () {
   var rsWR =
         "dialog { text: 'AH KB Resize Settings', bounds:[100,20,310,80]], \
         s1: StaticText { text:'Width mm', bounds:[10, 5, 70, 25]}, \
         w: EditText { text:'100', bounds:[75, 5, 120, 25]}, \
         s2: StaticText { text:'Height mm', bounds:[10, 30, 70, 50]}, \
         h: EditText { text:'200', bounds:[75, 30, 120, 50]}, \
         runBtn: Button { text:'Run', bounds:[130, 5, 200, 25], properties:{name:'ok'}}, \
         quitBtn: Button { text:'Quit', bounds:[130, 30, 200, 50], properties:{name:'cancel'}} \
   }";
   var rsD = new Window (rsWR);
   var result = rsD.show();
   alert(result + '\n' + rsD.w.text + '\n' + rsD.h.text);
}

I would add that working from some old existing script window of mine is generally not a time saver, you need a simple example to develop from scratch - hopefully this helps with that.

Andrew
Guest

Run and Cancel Buttons in JS Windows

Post by Guest »

I like the second one.

My script is actually set up similarly to what you have posted above. You have the line:

rsD.quitBtn.onClick = function() {rsD.close(4);}

and the line in my script is:
cancelBtn.onClick = function () { this.parent.parent.close(0);};
but it's not stopping the script; the script continues to process after hitting cancel.

by the way, what does the '4' in your line above do?
Andrew

Run and Cancel Buttons in JS Windows

Post by Andrew »

Whatever number is in the brackets is passed to the 'result' variable - it doesn;t matter what the number is, it lets you differentiate between different close processes.

When nothing happens it's because something is wrong in the statement - probably the parent.parent bit.

Andrew
garek007

Run and Cancel Buttons in JS Windows

Post by garek007 »

But Andrew, I find that even with the above instructions, when I hit cancel, the scrpt still runs. Why is this?