1

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

5 に答える 5

2

Adobe Photoshop CS2 JavaScriptスクリプティングガイドによると、Application.activeDocumentそのドキュメントをアクション用に現在選択されているドキュメントにするために、プロパティに割り当てる必要があるようです。saveForWebPNG最初のブロックのイテレータでドキュメントを明示的にアクティブ化せずに関数でそのプロパティを使用しているため、これは理にかなっています。次の変更と同じくらい簡単かもしれません:

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

ただし、Photoshopのコピーを持っておらず、このソリューションを検証していません。

于 2010-06-26T06:13:30.880 に答える
0

これがかなり遅れていることは知っていますが、この質問に出くわしたばかりで、将来誰かが使用できる解決策があるかもしれないと感じています. だからここに行きます。

1 つの解決策は、for ループを調整することです。5 つのドキュメントが開いているとします。app.documents.length は 5 になります。1 つを閉じると、長さは 4、次に 3 になります。app.documents.length が減少するにつれて、i はカウントアップします。i = 3 の場合、app.documents.length = 2 です。逆方向に繰り返すとうまくいくと思います。

for (i = app.documents.length - 1; i >= 0; i--) { ... }
于 2012-01-08T03:50:06.307 に答える
0

@maerics からの答えは正しいです。

ただし、特定のドキュメントのインデックスがわからない場合は、次の例のように名前で参照できます。

// 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
于 2020-04-18T04:02:37.650 に答える
-1

jbizのソリューションに+1。ループが完了しない理由がわかりませんでした。

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 は、完了時にドキュメントを閉じると、ループを繰り返すたびに短くなります。

于 2013-05-09T04:13:42.200 に答える