automaticly perform functions after a window object is shown

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

Patrick

automaticly perform functions after a window object is shown

Post by Patrick »

I am trying to build a window object that stays open while the script does its thing, then closes at the end. I am having trouble figuring out how to make the script continue to perform after the window.show(); function is called -- it stops the script until the window is closed.

There is a window event handler for onShow, but it performs the function before making the window object visible. Anyone know if there is a way to automaticly do something after? Thanks in advance,

Patrick
xbytor

automaticly perform functions after a window object is shown

Post by xbytor »

The only thing you can do is do something like set the value of a control. It's onChange method will get called _after_ the window is open. If necessary, create a control that's not visible for just this purpose.

-X
Patrick

automaticly perform functions after a window object is shown

Post by Patrick »

x, I'm not having any luck getting that to work. Here is what I tried, if you get a chance can you glance at it and let me know if I missed something?

Code: Select allvar win = new Window("dialog{\
text:'Building HR file...',bounds:[100,100,280,140],\
maintxt:StaticText{bounds:[10,0,170,17] , text:'Buidling file...' ,properties:{scrolling:false,multiline:false}},\
bar:Progressbar{bounds:[10,20,170,30] , minvalue:0,maxvalue:100,value:0}\
};");

win.onShow = function() {
   win.bar.value = 15;
};

win.bar.onChange = function() {
   alert("this should be over the window");
};
win.center();
win.show();


Thanks,
Patrick
xbytor

automaticly perform functions after a window object is shown

Post by xbytor »

Instead of
Code: Select allwin.onShow = function() {
   win.bar.value = 15;
};

try just this:
Code: Select all win.bar.value = 15;

win.bar.onChange = function() {
   alert("The window should be open");
   win.bar.onChange = function(){};
}

You don't need to override Window.show but you do the the onChange function.

-X
Patrick

automaticly perform functions after a window object is shown

Post by Patrick »

I spent a decent chunk of time tinkering with this over the last couple of days, no luck though. The element onChange does not get triggered at all if the change happens before the show(); function is called. Thanks for the suggestions, I ended up making it so the user has to click a button in the dialog box to start the sequence, as there doesn't appear to be any easy way to have it automaticly happen.

Regards,
Patrick
xbytor

automaticly perform functions after a window object is shown

Post by xbytor »

It does work for dropdown lists. Try this on for size:

Code: Select allTestUI = function() {};

TestUI.createWindow = function() {
  var wrect = {
    x: 200,
    y: 200,
    w: 370,
    h: 200
  };
  var win = new Window('dialog', 'Post onShow code execution test',
                       [wrect.x, wrect.y, wrect.x+wrect.w, wrect.y+wrect.h]);

  var ddlist = win.add('dropdownlist', [10, 10, 20, 35],
                       ['one', 'two']);

  ddlist.onChange = function() {
    alert("In onChange callback");
  }

  ddlist.items[0].selected = true;

  ddlist.visible = false;

  alert("Before show");
  win.show();
  alert("After show");
};

TestUI.createWindow();



-X
Patrick

automaticly perform functions after a window object is shown

Post by Patrick »

Just tried that, and the 'after show' alert does not come up until I close the dialog window . Does that come up over the dialog window on your end?
Larry Ligon

automaticly perform functions after a window object is shown

Post by Larry Ligon »

Give onResize a try. I've been trying it and it sorta works. Let me know what you find out.

Here is my test code:

Code: Select allvar layersCount = 0;
var myCount = 0;
var myMessage = "Made "+ layersCount + " layers"
// Create an  dialog window near the upper left of the screen
var dlg = new Window('dialog', 'Creat second dialog');

//Place OK and Cancel buttons at the bottom 
dlg.buttons = dlg.add('group');
dlg.buttons.alignment = 'right';
dlg.buttons.btnClose = dlg.buttons.add("button",undefined , "Ok",{name:'Ok'} );
dlg.buttons.btnClose.onClick = function() {
   
   createSecondDialog();
      
  };
dlg.buttons.btnCancel = dlg.buttons.add("button",undefined , "Cancel",{name:'cancel'} );
dlg.buttons.btnCancel.onClick = function() { dlg.close(); };

function createSecondDialog(){
   var dlg2 = new Window('dialog', 'Progress Bar test');
   dlg2.alignment = "right";
   with (dlg2){
   orientation = 'column';            
   alignChildren = "center";   
   myProgessMessage = add('statictext', undefined," ");
   myProgessMessage.preferredSize = [100,20];
   myProgessMessage.alignment = "center";
   myProgressBar = add('progressbar',undefined,0,100);
   myProgressBar.preferredSize = [250,20];
   myRunButton = add('button', undefined,'Run');
   
   }
   dlg2.onResize = function (){
      //alert("window resize");
      myCount = myCount + 1;
      //alert(myCount);
      if(myCount > 2){      
      myRunButton.notify();   
      };      
};      
   
   myRunButton.onClick = function (){
   var intNumberofColumns = 3;
   var intNumberofRows = 3;
   var intNumberofSlices = intNumberofColumns * intNumberofRows;
   var myProgressBarincrement = Math.round(100/intNumberofSlices);

   for (var J = intNumberofRows-1 ; J >=0 ; J--) {
      for ( var I = intNumberofColumns-1 ; I >=0 ; I--) {
         layersCount = layersCount + 1;
         for ( var K = 100000-1 ; K >=0 ; K--) {
         MYVALUE = K;         
         }
   //alert("J = "+ J +"  I = "+I);
   myProgressBar.value = myProgressBar.value + myProgressBarincrement;;      
   myProgessMessage.text =  "Made "+ layersCount + " layers";
   };
   };
   };
   
   dlg2.center();   
   dlg2.show();      
}

dlg.center();
dlg.show();
xbytor

automaticly perform functions after a window object is shown

Post by xbytor »

You should see:
' Before show'
' In onChange callback'
' After show'

in that order. 'win.show' does not complete until after that window is closed. If you want to have code execute while the window is still in 'show', you have to either register a callback on a button click, or register an onChange callback on a control like I did here.

This example executes a piece of code after the window opens without an interaction from the user. This is as close as you get. When that code executes, the UI blocks. This is the way the JS interpreter in Photoshop works.

It is not multithreaded.

Web browser JS implementations have the same problem, but they also have timers that let you execute code at some arbitrary point in the future. The syntax is some like:

Code: Select allsetTimeout('someFunction()', 5000);

which would execute the 'someFunction' 5 seconds in the future. This lets you have copperative interleaved execution which kinda works like multithreading.

-X
Holograph

automaticly perform functions after a window object is shown

Post by Holograph »

You might find my recent post helpful. It's about getting palettes to stay focused and on top of other child windows while a script is running. I built a progressbar, but you could use it for other things as well.

http://ps-scripts.com/bb/viewtopic.php?p=4108#4108

Let me know if that helped you out!

Thanks!

-Brian-