Fix for active and onChange

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

Mickster

Fix for active and onChange

Post by Mickster »

Is there any way to set the focus to a control in onChange?

Code like this doesn't work...

Code: Select allmyEdit.onChange = function()
{
   if(!isValid(this.text)
      {
      alert("You entered bad data, stupid! Go back and fix it.");
      this.active = true;
      }
}

It's strange, the first time I enter data in the edittext, the onChange method gets called, but the focus doesn't change back to the edittext after the alert. After that, the onChange method is never called again, even when I enter new data and move the focus. However, when I close the dialog with the Cancel button, it gets called just before the dialog closes.

Is there a fix or workaround for this, or do I have to settle for doing validation in the dialog's onClose method?

BTW, I'm using CS2. Has this been fixed in later versions?
Paul MR

Fix for active and onChange

Post by Paul MR »

Hi, I would have thought Code: Select allmyEdit.onChange = function()should be Code: Select allmyEdit.onChanging = function()
Mickster

Fix for active and onChange

Post by Mickster »

No, onChanging is a different event. It's called for each incremental change. The onChange method is supposed to be called when the user is finished making changes and moves the focus to another control. Unfortunately, the event handling code appears to get confused if you try to change the focus by setting a control's 'active' attribute in onChange. It just stops calling onChange for some reason. And then later, when the user closes the dialog, the onChange gets called. If you don't try to change the focus, then onChange is called every time the user finishes making a change.
Mickster

Fix for active and onChange

Post by Mickster »

Is there any way to get this to work?

Does this work in later versions of PS (CS3 or later)?

help..?
Mike Hale

Fix for active and onChange

Post by Mike Hale »

No, it doesn't work with CS5.

I can use window.control.active = true to set the focus to a control widget before the window is shown. But once it is shown changing the control's active property doesn't change the focus.

However with new versions of Photoshop you can use a keyboard event handler to limit what chars the control accepts while it has focus. Depending on what kind of validation you need that may help
Mickster

Fix for active and onChange

Post by Mickster »

Mike Hale wrote:No, it doesn't work with CS5.

I can use window.control.active = true to set the focus to a control widget before the window is shown. But once it is shown changing the control's active property doesn't change the focus.
Bummer! I'm surprised that Adobe lets something like this hang around for so many versions. I guess they must have too many other issues that are more important.

Mike Hale wrote:However with new versions of Photoshop you can use a keyboard event handler to limit what chars the control accepts while it has focus. Depending on what kind of validation you need that may help
No, incremental validation like that is a pain, for me and the user. Oh well...

Thanks Mike!
Mike Hale

Fix for active and onChange

Post by Mike Hale »

Mickster wrote:No, incremental validation like that is a pain, for me and the user.I think that would depend on the user. For example, I would much rather a numeric editext control not allow me to enter letters than allow me to do so then only let me know what I was entered is wrong after I have moved on to another control.
Paul MR

Fix for active and onChange

Post by Paul MR »

This seems to work.....

Code: Select allvar win = new Window('dialog','test');
win.et1 = win.add('edittext');
win.et1.preferredSize=[50,20];
win.et2 = win.add('edittext');
win.et2.preferredSize=[50,20];
win.add('button',undefined,'Cancel');
win.et1.onChange = function(){
    this.active=true;
  if(!isValid(this.text)){
      alert("You entered bad data, stupid! Go back and fix it.");
      return;
      }
}
function isValid(str){
    if(Number(str)<200) {
        return false;
        }
    return true;
}
win.show();

EDIT: it seems as if it will work in ESTK but only with using the Tab key, it doesn't seem to work at all in Photoshop.
back to the drawing board!
Mike Hale

Fix for active and onChange

Post by Mike Hale »

It is not ideal but if you are willing to let the user press the enter key to confirm the control you can do something like this. It works because pressing enter does not change the focus. However most users expect enter to close the dialog and it still doesn't return focus if the user tabs or clicks on another control.
Code: Select allvar win = new Window('dialog','test');
win.et1 = win.add('edittext');
win.et1.preferredSize=[50,20];
win.et2 = win.add('edittext');
win.et2.preferredSize=[50,20];
win.add('button',undefined,'Cancel');
win.et1.enterKeySignalsOnChange = true;
win.et1.onChange = function(){
  if(!isValid(this.text)){
      alert("You entered bad data, stupid! Go back and fix it.");
      return;
      }
}
Mickster

Fix for active and onChange

Post by Mickster »

Mike Hale wrote:Mickster wrote:No, incremental validation like that is a pain, for me and the user.I think that would depend on the user. For example, I would much rather a numeric editext control not allow me to enter letters than allow me to do so then only let me know what I was entered is wrong after I have moved on to another control.
The problem is my edittext controls are alphanumeric. They work like Photoshop's. The user can enter a distance or position value in whichever ruler units they choose and the code handles any necessary conversion. The user can also enter a value without a unit designation and the code converts it to the current ruler units for validation. For example, if the current ruler unit is millimeters and the user enters '2' or '2.0' as the value, the code assumes they mean millimeters and appends the corresponding unit designation '2 mm'. However, if the user enters '2.0 pc' or '2 pica' or '2 picas', then the code converts it into the current ruler units before validating the distance value.

The validation and everything else is working fine. The only problem is the inability to set focus when validation fails.