开发者

How do I focus another document with photoshop javascript scripting?

This following script almost does what I need. What I'm trying to do is go through the opened documents, 139 of them, and save them as jpeg. However what it's lacking is moving from one opened document to the other, so开发者_开发知识库 it saved the same image over 139 times. I assumed doc.close() would close the opened document and give a new one focus, but it doesn't.

Here's the code:

var destination = "C:/Documents and Settings/Administrator/My Documents/small images"
for(var i = 0; i < 5; i++)
{
    doc = documents[i];
    name_ = doc.name.substring(0, doc.name.indexOf('.'))
    saveForWebPNG(destination, name_);
    doc.close();
}

function saveForWebPNG(outputFolderStr, filename)
{
    var opts, file;
    opts = new ExportOptionsSaveForWeb();
    opts.format = SaveDocumentType.JPEG;
    opts.quality = 60;
    if (filename.length > 27) {
        file = new File(outputFolderStr + "/temp.jpg");
        activeDocument.exportDocument(file, ExportType.SAVEFORWEB, opts);
        file.rename(filename + ".jpg");
    }
    else {
        file = new File(outputFolderStr + "/" + filename + ".jpg");
        activeDocument.exportDocument(file, ExportType.SAVEFORWEB, opts);
    }
}


According to the Adobe Photoshop CS2 JavaScript Scripting Guide it looks like you need to assign to the Application.activeDocument property to make that document the currently selected one for any actions. This makes sense since you're using that property in the saveForWebPNG function without explicitly activating the document in the iterator in the first block. It might be as simple as the following change:

for (var i = 0; i < 5; i++) {
  var doc = documents[i];
  app.activeDocument = doc; // Select that document.
  var name = doc.name.substring(0, doc.name.indexOf('.'))
  saveForWebPNG(destination, name);
  doc.close();
}

However, I don't have a copy of Photoshop and haven't verified this solution.


I know this is a lot late, but I just came across this question and feel like I might have a solution that someone down the road may be able to use. So here goes.

One solution is to adjust the for loop. Let's say there are 5 documents open. app.documents.length would be 5. When you close one, the length is now 4, then 3, etc. i is counting up as app.documents.length is decreasing. When i = 3, app.documents.length = 2. I think that iterating backwards would do the trick.

for (i = app.documents.length - 1; i >= 0; i--) { ... }


The answer from @maerics is correct.

However, if you don't know the index of the given document you can reference it by name like in this example:

// Assume 'hello.psd' and 'world.psd' are in same folder at script.
// Open the first file. It is the active document.
var scriptFile = new File($.fileName)
var scriptPath = scriptFile.parent.fsName
var file1obj = File(scriptPath + '/hello.psd')
var file1 = open(file1obj)

// Open the second file. The second file is now the active document.
var file2obj = File(scriptPath + '/world.psd')
var file2 = open(file2obj)

// Make the first file the active document
app.activeDocument = file1

// Make the second file the active document
app.activeDocument = file2


+1 for jbiz's solution. I couldn't figure out why my loop wouldn't complete.

for (i=app.documents.length-1; i >= 0; i--) {
  doc = app.documents[i];
  app.activeDocument = doc;

  ...
  do whatever you need, save & close
  ...
}

app.documents.length gets shorter with every iteration though the loop if you close the document when you're done.


You want to close the current selected (or active) document:

app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜