Skip to main content
Participating Frequently
July 30, 2015
Question

Simulate button press in script UI

  • July 30, 2015
  • 4 replies
  • 4603 views

I have a Script UI dialog box that is displayed within my script asking for user input on what to do next (two buttons).

However, because it is part of a batch script I'd like the script to automatically click a default buton after a predetermined amount of time so that the script doesn't hang up waiting for user input.

I'm good with creating the timer, but I haven't figured out how to make it run while the dialog is still displayed. It seems like I can get it to start timing either BEFORE the dialog is displayed or AFTER the dialog is closed, but not WHILE it is displayed.

Edit: is there an onLoad that I could add to the window element?

This topic has been closed for replies.

4 replies

cbournAuthor
Participating Frequently
August 1, 2015

Thanks! I only needed to add "optionWin.update()" inside the while loop to get the onClick to be recognized.

The following is a WORKING timed palette with early exit upon button click.

#target photoshop

app.bringToFront();

app.displayDialogs = DialogModes.NO;

// dummy variable

foo = true;

optionWin = new Window('palette');

  optionWin.grpButtons = optionWin.add('group');

  optionWin.grpButtons.btn1 = optionWin.grpButtons.add('button', undefined, 'btn1');

  optionWin.grpButtons.btn1.onClick = function() {

  // change dummy variable

  foo = false;

  };

  optionWin.grpButtons.btn2 = optionWin.grpButtons.add('button', undefined, 'btn2');

  optionWin.grpButtons.btn2.onClick = function() {

  // change dummy variable

  foo = false;

  };

optionWin.center();

optionWin.show();

// keep palette busy (i.e., showing) for 5000ms

var startTime = new Date().getTime();

while ( new Date().getTime() - startTime < 5000 && foo ) {

  optionWin.update();

}

// see if buttons worked

alert(foo);

matias.kiviniemi
Legend
August 1, 2015

Don't remember exactly why I ended up with that, but I think there was some different behaviour between Win & Mac. So if you experience issues try the other.

cbournAuthor
Participating Frequently
July 31, 2015

I created this scaled-down version of the palette to see if I could get it to work. The palette is showing for the desired amount of time, but there are two issues. I'm hoping that there's some simple solutions that I'm not seeing.

1) The buttons aren't changing the value of a dummy variable as I expected them to (this could be due to the way I have set up the timer in a while loop), the sleep function had similar results

2) I was hoping to catch instances when the user clicks one of the buttons that it would escape the timer rather than waiting for the timer to complete

Any thoughts would be appreciated.

#target photoshop

app.bringToFront();

app.displayDialogs = DialogModes.NO;

// dummy variable

foo = true;

optionWin = new Window('palette');

  optionWin.grpButtons = optionWin.add('group');

  optionWin.grpButtons.btn1 = optionWin.grpButtons.add('button', undefined, 'btn1');

  optionWin.grpButtons.btn1.onClick = function() {

  // change dummy variable

  foo = false;

  };

  optionWin.grpButtons.btn2 = optionWin.grpButtons.add('button', undefined, 'btn2');

  optionWin.grpButtons.btn2.onClick = function() {

  // change dummy variable

  foo = false;

  };

optionWin.center();

optionWin.show();

// keep palette busy (i.e., showing) for 5000ms

var startTime = new Date().getTime();

while ( new Date().getTime() - startTime < 5000 ) {}

// see if buttons worked

alert(foo);

matias.kiviniemi
Legend
August 1, 2015

Try adding following in the while loop, that probably locks up everything, i.e. your onClick never gets called.

optionWin.update()

app.refresh()


For #2 change the while condition to be "(foo == true) && (new Date().getTime() - startTime < 5000)" so it breaks when variable changes

Inspiring
July 30, 2015

Use a palette window type. The code would basically do the following.

  • Create a palette window.
  • Register onClicks for your buttons that save which button was pressed.
  • Show the window.
  • $.sleep() for however long you want the window to be visible.
  • After the sleep is finished, check to see which if any of the buttons were pressed.
  • Close the window.
  • Continue your script.

This may or may not work. It's supposed to but palette window behavior over the years has not always been consistent.

cbournAuthor
Participating Frequently
July 30, 2015

We're on the same page. I'll see if I can get it to work.

Thank you both for your suggestions.

Chuck Uebele
Community Expert
Community Expert
July 30, 2015

I know DBarranca Davide Barrenca was trying to get modeless UIs to work without much success, so he's been doing a lot with extensions.

Chuck Uebele
Community Expert
Community Expert
July 30, 2015

I think that's going to be a hard one to do, as the UI locks things up until it's dismissed. Just out of curiosity, is this something others are going to use or just for your own use? You can always program the script to bypass the UI if conditions are met, like a particular user ID. Even if you got the timer to run, you might have problems dismissing the UI. I had this issue with AS3. It seems there is a security feature that doesn't allow you to program something that is suppose to be user interaction.

cbournAuthor
Participating Frequently
July 30, 2015

Thanks for your response, Chuck.

It's for others to use. And yes, that is exactly what happens--the UI hangs up all processes until one of the buttons is clicked.

Chuck Uebele
Community Expert
Community Expert
July 30, 2015

I've only done a little with extensions, but that might be a way around this. You can have a modeless panel, which will allow you to use the timer function. I just don't think you can do that with the standard UI. I used Brackets for the few panels I made, and it sets up pretty easy. Just a bit of HTML coding and CSS, but all your JS is integrated for you.