UI progressbar example

Photoshop Script Snippets - Note: Full Scripts go in the Photoshop Scripts Forum

Moderators: xbytor, DavideBarranca, Paul MR

Larry Ligon

UI progressbar example

Postby Larry Ligon » Wed Sep 02, 2009 9:37 pm

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!



Mike Hale

UI progressbar example

Postby Mike Hale » Wed Sep 02, 2009 9:55 pm

Nice one Larry,

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

kpt

UI progressbar example

Postby kpt » Wed Apr 20, 2011 12:40 am

I don't know how you manage to see the progress bar update itself without an app.refresh() ?

mycort

UI progressbar example

Postby mycort » Tue Feb 14, 2012 5:22 pm

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.

Mike Hale

UI progressbar example

Postby Mike Hale » Tue Feb 14, 2012 10:28 pm

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 all
function 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 all
progressWindow.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.


Return to “Photoshop Scripting: Code Snippets”

Who is online

Users browsing this forum: No registered users and 1 guest