2

I'm currently trying to automate a certain task in Photoshop (the latest version) with a JS code script. The each process is that 1. load an image -> 2. do something -> 3. implement an action -> 4. save the image -> 5. repeat this. Pretty simple, right? Cut to the chase, it's my code snippet below.

var opts;
opts = new ExportOptionsSaveForWeb();
opts.format = SaveDocumentType.PNG;
opts.PNG8 = false;
for(var i = 1; i < 6; i ++){
        var filename = '/Users/abcde/Desktop/ap/' + i.toString() + '.PNG';
        var file = File(filename);
    var document = app.open(file);
    // do something in Photoshop manually !!
    app.doAction('bbbb', 'aaaa');
    var savename = '/Users/abcde/Desktop/ap_save/' + i.toString() + '.PNG';
    var savefile = new File(savename)
    app.activeDocument.exportDocument(savefile, ExportType.SAVEFORWEB, opts);
}

So, my question is how do I pause the program when it loaded an image and do something, and get an input key like 'w' and resume the entire process again?

With my question, the code snippet is going to be like below

var opts;
opts = new ExportOptionsSaveForWeb();
opts.format = SaveDocumentType.PNG;
opts.PNG8 = false;
for(var i = 1; i < 6; i ++){
    var filename = '/Users/abcde/Desktop/ap/' + i.toString() + '.PNG';
    var file = File(filename);
    var document = app.open(file);
    // pause this code script (which type of code can I put in here?)
    // do something(I'm going to do something manually in the Photoshpp when this script's been paused, this part doesn't has to be written in code)
    // resume when it gets a key input(I want to type 'w' when that something task has been done, again.. which type of code function can I put?) 
    app.doAction('bbbb', 'aaaa');
    var savename = '/Users/abcde/Desktop/ap_save/' + i.toString() + '.PNG';
    var savefile = new File(savename)
    app.activeDocument.exportDocument(savefile, ExportType.SAVEFORWEB, opts);
}

Please help me. If either it's impossible to build or I need to clarify this question more let me know too. Thanks.

Seung
  • 21
  • 3
  • Iam not sure whether you can open `dialogs` or `popups` using javascript inside photoshop. But if thats possible ,try opening the dialog or popup having buttons for example like `continue` and `stop` near the code where you want to pause the flow.Attach handler to continue and stop button to set flows. Check this https://stackoverflow.com/questions/38701446/photoshop-scriptui-show-a-dialog-window-close-it-show-it-again-gives-an-empty – M A Salman May 15 '20 at 19:47
  • Thank you so much for your help. But I'm new at both JS and Photoshop so I have no idea what dialogs or popups are.. anyways I will take a look at the link you sent! Have a good one! – Seung May 18 '20 at 00:49

1 Answers1

0

I would restructure the script to run the export first (in case there is already a file open and then open the next document. Then, when the script is finished, the user can do something with the open document in Photoshop. Once they are done with that, they run the script again.

This needs a bit of extra checking if there is a document already open and also if there are any files left to handle. But it would basically look like this:

// set export options
var opts;
opts = new ExportOptionsSaveForWeb();
opts.format = SaveDocumentType.PNG;
opts.PNG8 = false;


// find out, which destination files already exists, to know which new one to open
// increase i until it finds an index that does not exist yet
var i = 1;
while( File('/Users/abcde/Desktop/ap_save/' + i.toString() + '.PNG').exists ) {
  i++;
}


// if a document is already open, run the action on it and export it
if(app.documents.length) {
  app.doAction('bbbb', 'aaaa');
  var savename = '/Users/abcde/Desktop/ap_save/' + i.toString() + '.PNG';
  var savefile = new File(savename)
  app.activeDocument.exportDocument(savefile, ExportType.SAVEFORWEB, opts);
  app.activeDocument.close();
  // increase i once more to jump to the next file
  i++;
}



// try to open a new source file, if there is one left
var srcFile = File('/Users/abcde/Desktop/ap/' + i.toString() + '.PNG)'

if(!srcFile.exists) {
  // no further source file does exist, exit the script
  exit();
}

var document = app.open(file);

// script ends here and the user can do stuff
// and then use a hot key to run the script again
mdomino
  • 1,195
  • 1
  • 8
  • 22