如何使用JS代码脚本在Photoshop中暂停/恢复?



我目前正在尝试使用JS代码脚本在Photoshop(最新版本(中自动执行某项任务。每个过程是1。加载图像 -> 2。做点什么 -> 3.实施操作 -> 4.保存图像 -> 5。重复此操作。很简单,对吧?切入正题,这是我下面的代码片段。

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);
}

所以,我的问题是,当程序加载图像并执行某些操作时,我如何暂停程序,并获得像"w"这样的输入键并再次恢复整个过程?

对于我的问题,代码片段将如下所示

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);
}

请帮助我。 如果无法构建或我需要澄清这个问题,也请告诉我。 谢谢。

我会重组脚本以首先运行导出(以防已经打开了一个文件,然后打开下一个文档。然后,当脚本完成后,用户可以对 Photoshop 中打开的文档执行某些操作。完成此操作后,他们将再次运行脚本。

这需要一些额外的检查,检查是否有文档已经打开,以及是否有任何文件需要处理。但它基本上看起来像这样:

// 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

相关内容

  • 没有找到相关文章