Page 2 of 2

UI progressbar example

Posted: Wed Sep 02, 2009 9:37 pm
by Larry Ligon
Here is a variation of Mike Hale's code. Doesn't have the flickering.

Code: Select all//progress bar
var value = 0;
var win = new Window("palette{text:'Please wait...',bounds:[100,100,550,140]," +
               "progress:Progressbar{bounds:[20,10,430,28] , minvalue:0,value:" + value + "}" +
               "};"
         );

var sFolder = new Folder('/c/temp2');
var files = sFolder.getFiles("*.jpg");
win.progress.maxvalue = files.length;

while(files.length>0){
   win.center();
   win.show();
   var doc = app.open(files.pop());
   doc.close(SaveOptions.DONOTSAVECHANGES);
   win.progress.value++;
   win.layout.layout(true);
};

 

It only works in versions of Photoshop that have autolayout!

UI progressbar example

Posted: Wed Sep 02, 2009 9:55 pm
by Mike Hale
Nice one Larry,

I use autolayout most of the time and this is much better than the show/hide dance.

UI progressbar example

Posted: Wed Apr 20, 2011 12:40 am
by kpt
I don't know how you manage to see the progress bar update itself without an app.refresh() ?

UI progressbar example

Posted: Tue Feb 14, 2012 5:22 pm
by mycort
I've tried adding Mike's code for this progressbar and the code before, tried adding to the front and end of my script.....still I dont see a progressbar. Am I doing something wrong? I have the most recent PHotoshop CS5 version.

UI progressbar example

Posted: Tue Feb 14, 2012 10:28 pm
by Mike Hale
It seems that in Photoshop CS5 some changes were made to the ScriptUI that are not documented and cause older code to break.

I modified Xbytor's progressbar function and it works for me in CS5.

Here is the function:
Code: Select allfunction createProgressWindow(title, message, min, max, parent, useCancel) {
   var win = new Window('palette', title);
   win.bar = win.add('progressbar', undefined, min, max);
   win.bar.preferredSize = [300, 20];
   win.stProgress = win.add('statictext');
   win.stProgress .preferredSize.width = 200;
   win.parent = undefined;

   if (parent) {
      if (parent instanceof Window) {
         win.parent = parent;
      } else if (useCancel == undefined) {
         useCancel = parent;
      }
   }

   if (useCancel) {
      win.cancel = win.add('button', undefined, 'Cancel');
      win.cancel.onClick = function() {
      try {
         if (win.onCancel) {
            var rc = win.onCancel();
            if (rc || rc == undefined) {
               win.close();
            }
         } else {
            win.close();
         }
         } catch (e) { alert(e); }
      }
   }

   win.updateProgress = function(val) {
      var win = this;
      if (val != undefined) {
         win.bar.value = val;
      }else {
         win.bar.value++;
      }
      if (win.recenter) {
         win.center(win.parentWin);
      }
      win.update();
   }
   win.center(win.parent);
   return win;
};

And here is an example of how to use it:
Code: Select all// set up the progress bar with a title and range
var progressWindow = createProgressWindow("Please wait...", undefined, 0, 10,undefined, false);
progressWindow.show();
progressWindow.isDone = false;
// if you code does several things you can change the message under the bar as needed
progressWindow.stProgress.text = ("Creating template document");
// update the bar as needed
progressWindow.updateProgress();
// change the message if desired
progressWindow.stProgress.text = ("Placing Images");
// when done
progressWindow.isDone = true;
progressWindow.close();
// the inDone flag is useful if you have code like a function
// that you may or may not want to update the progress bar
// in that case you can do something like
if(!progressWindow.isDone) progressWindow.upDateProgress();


The above explains how to use the function in 'auto increment' mode. You can also control the amount of progress by passing the value to use.
Code: Select allprogressWindow.updateProgress( 3 );

If I have a process that may vary in the number of steps I can use the function in auto mode and pass the value at some point to step back the bar so I can use the same bar for say a 10 or 13 step process. In the extra steps I just pass the current value. The bar length doesn't change for that step but I can change the message.

Re: UI progressbar example

Posted: Fri Mar 08, 2019 2:00 am
by roldy
How do I stop the progress bar window from stop flickering?