Closing a dialog problem

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

paulemasters

Closing a dialog problem

Post by paulemasters »

In PC PhotoShop CS3.

I have an error dialog/window that is displayed after the main data entry window is closed.
There are a number of strange things happening.

1. If the window is run separately as in the attached code, the OK button works correctly. That is, the window is displayed and then goes away when the OK button is clicked or enter is pressed.
If the same code is in the main / larger script, the window flashes on the the screen and then the script continues.
To stop that I have added a . (period) in the button so it won't be OK (it is OK.).
But that means that the window won't complete if the button is clicked or Enter is pressed.
It is interesting that an alert window with an OK button works correctly.
(I don't want to use an alert because you can't have multiple lines - or can you?)

2. Found how to close the dialog and added the onClick. That works if the button is clicked.

3. Found the enterKey item. Added the addEventListener and .onEnterKey.
Get no errors but neither works. (Comment out one than the other.)
Perhaps one or both are not coded correctly.

The book seems to indicate that if there is a handler on the OK button, that overrides the default action. That appears to not be the case because when in the larger script, the window still flashes with the button of OK and the .onClick.

It is interesting that clicking on the X or pressing Esc does what a Cancel button would do. However, there is no Cancel button.

Thanks for any suggestions.

Paul Masters

Code: Select all#target Photoshop
app.bringToFront();
main();
function main(){
      var errW = new Window ("dialog", "Data not numeric");
      var errMsg1 = errW.add("statictext", undefined, "The following fields contain non numeric characters.");   
      var errMsg2 = errW.add("statictext", undefined, "a variable");
      var errMsg3 = errW.add("statictext", undefined, "No letters, spaces or special characters allowed.");   
      var errButtonGroup = errW.add ("group");
      errButtonGroup.alignment = "center";
      var errOKButton = errButtonGroup.add ("button", undefined, "OK." );
      errOKButton.onClick = function() {errW.close(1);};
      errW.addEventListener ( "enterKey", function() {errW.close(1)});
      errW.onEnterKey = function() {errW.close(1);};
      errW.show();
}

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Mike Hale

Closing a dialog problem

Post by Mike Hale »

CS3 doesn't support keyboard and mouse listeners.

You can override the default OK button callback by creating an onClick function but in this case your custom function does the same as the default action.

Clicking on the cancel button( if there is one ), clicking on the X in the upper right of the window, or pressing the ESC key when the dialog is showing all cancel the dialog and return 2 as default. So you don't need a cancel button but most seem to have one.

As far as it not working with another script, it is hard to say. My guess it has something to do with how it is called from the main script.
paulemasters

Closing a dialog problem

Post by paulemasters »

Thanks for the quick reply.

I tried the 'keydown' I found in Peter Kahrels excellent document on ScriptUI for CS5 but that didn't work, so I suspected it was not in CS3.

However, I found the enterKey items in the documentation that came with CS3 so I figured they would work. Guess not.

The error dialog is inside a while loop along with the data entry dialog. The data entry window has ended before the error window creation happens. My only thought is that something is still 'active' so for some reason, the error window 'thinks' the OK button or Enter has been pressed. The window does appear but very fast and then goes away.
It is interesting that an alert window works.

I changed the button to OK and the onClick to modify a variable. The error window displayed OK. But, if I change back to the err.W.close(1) the error window flashes and does not stay on the screen - that is, it acts like the button was clicked or Enter was pressed but that wasn't done, except for ending the data entry screen. So, it appears that something is still 'set' and the error window sees it and closes.

Oh, well, it mostly works. I just wanted it to work like all the others so the user wouldn't be confused (like I am) that on some windows you have to click on the button to close and others Enter works as well.

Thanks for all your help.

Paul Masters
Mike Hale

Closing a dialog problem

Post by Mike Hale »

paulemasters wrote: I just wanted it to work like all the others so the user wouldn't be confused (like I am) that on some windows you have to click on the button to close and others Enter works as well.

A ScriptUI dialog can have a default element. That element's onClick button is called when the enter key is pressed. ScriptUI assigns the default element to the 'OK' button if there is one. So if the dialog has an 'OK' button it can be closed by pressing the enter key. If the dialog doesn't have an 'OK' button you have to assign the default element yourself to whatever control you want to handle the enter key otherwise pressing the enter key doesn't effect the dialog
paulemasters

Closing a dialog problem

Post by paulemasters »

OK. That's why I added the onEnterKey shown as a callback and the addEventListner which is shown as a UIEvent type in the JAVA Script Tools Guide under the SDK in the EST Help on page 77, which came with CS3.
The text appears to imply that those can be 'attached' to any level and even have a sample showing an addEventListner related to the dialog.

I realize that I am guessing most of the time as I am not very good with this type of programming. (The documentation isn't very helpful to me for that reason. It 'assumes' that the reader knows what is is talking about most of the time and does not always give good examples.) I am learning some of this, but it is slow going.

It is possible that I specified something wrong. However, running the script there are no errors. It appears that if a 'feature' is not supported, as long as the syntax is correct, that command is ignored.

Thanks for any suggestions.

Paul Masters
Mike Hale

Closing a dialog problem

Post by Mike Hale »

I guess what I am trying to understand is why you are going to the trouble of over ridding the the default handlers if the functions you replace the default handler with do the same thing as the defaults.

If you think the default handlers are the reason the dialog will not work correctly when called from your main script shouldn't you replace them with functions that do something other than what the default handlers do?
paulemasters

Closing a dialog problem

Post by paulemasters »

Perhaps.... However, I was trying to bypass what ever is 'automatically' happening by taking control of the button. That is, by not allowing the default to be in effect. When the default is in effect, no handlers, the window flashes on the screen as if the button was clicked or enter was pressed. By 'turning off' the default, having the button name OK., the window stays on the screen so it can be read. Using the handler implies that the action won't happen until the button is clicked - which in fact works. I am / was trying to create a handler to do what the Enter key does with the same thinking. However, that does not appear to work.

I know that this is 'going around the barn' but for some reason the 'system' is 'stuck' so I am trying to write code to do what it would do if it worked correctly.

Thanks.

Paul Masters
paulemasters

Closing a dialog problem

Post by paulemasters »

Perhaps.... However, I was trying to bypass what ever is 'automatically' happening by taking control of the button. That is, by not allowing the default to be in effect. When the default is in effect, no handlers, the window flashes on the screen as if the button was clicked or enter was pressed. By 'turning off' the default, having the button name OK., the window stays on the screen so it can be read. Using the handler implies that the action won't happen until the button is clicked - which in fact works. I am / was trying to create a handler to do what the Enter key does with the same thinking. However, that does not appear to work.

I know that this is 'going around the barn' but for some reason the 'system' is 'stuck' so I am trying to write code to do what it would do if it worked correctly.

Thanks.

Paul Masters
paulemasters

Closing a dialog problem

Post by paulemasters »

PC PhotoShop CS3 Windows 7 64 bit.

Here is a quick sample I threw together. It is a very cut down version of the real script but it works the same way. Or doesn't work, however you want to look at it.

Run it and press Eenter. You won't see the second window. (Just discovered that if you click on OK, it works correctly.)
Change the second window OK button to OK. and you will. (Of course you have to click on the X or the Cancel button or press Escape as I did not include the onClick to make it work like the OK button does.)

May be this will help someone to help me in understanding what is going on. And how I can get the Enter key to work for the second dialog the same way it does for the first.

Thanks for any comments.

Paul Masters

Code: Select all#target PhotoShop
app.bringToFront();
main();
function main()
   {
   //  Dialog #1
   var entryW = new Window ("dialog", "Dialog #1");
   entryW.alignChildren = ["fill","fill"];
   //
   var ewP1 = entryW.add("panel");
   ewP1.add ("statictext", undefined, "Heading 1.");
   //   
   var ewP2 = entryW.add ("panel");
   ewP2.add ("statictext", undefined, "Enter some numbers.");
   var ewP2G1 = ewP2.add ("group");
   ewP2G1.add ("statictext", undefined, "Field 1:");
   var Normal = ewP2G1.add ("edittext");
   Normal.characters = 3;
   Normal.text = "0";
   Normal.active = true;
   //
   var ewBG = entryW.add ("group");
   ewBG.alignment = "center";
   ewBG.add ("button", undefined, "OK");
   ewBG.add ("button", undefined, "Cancel");
   entryW.show();
   //  Dialog #2
   var selectW = new Window ("dialog", "Dialog #2");
   selectW.alignChildren = ["fill","fill"];
   //
   var swP1 = selectW.add("panel");
   swP1.add ("statictext", undefined, "Heading 2.");
   //   
   var swP2 = selectW.add ("panel");
   swP2.add ("statictext", undefined, "Just some messages.");
   var swP2G1 = swP2.add ("group");
   swP2G1.add ("statictext", undefined, "A message.  No data entry.:");
   //
   var swBG = selectW.add ("group");
   swBG.alignment = "center";
   swBG.add ("button", undefined, "OK");
   swBG.add ("button", undefined, "Cancel");
   selectW.show();
   }