Photoshop 中的 getDocumentByName

getDocumentByName in Photoshop

虽然可以使用 getByName 获取图层、历史状态或图层集,但在使用两个或多个打开的 Photoshop 文件时似乎没有用于文档的功能。

var srcDoc = app.Documents.getByName("Gwen_Stefani"); // doesn't work

对吗?

围绕文档数组循环直到字符串匹配所需文档的工作:

getDocumentByName("Gwen_Stefani");


function getDocumentByName(docname)
{
  for (var i = 0; i < documents.length; i++)
  {
    var someDoc = docname.replace(/\..+$/, "");
    if (someDoc.toLowerCase() == docname.toLowerCase())
    {
      alert(someDoc);
      app.activeDocument = documents[i];
    }
  }
}

如果不确定,请检查 the scripting reference(第 104 页):

var srcDoc = documents.getByName("Gwen_Stefani");
activeDocument = srcDoc;

也可以通过ID激活文档,比较靠谱:

// selects a doc with provided ID
function selectByID(id) {
    var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putIdentifier(charIDToTypeID("Dcmn"), id);
    desc.putReference(charIDToTypeID("null"), ref);
    executeAction(charIDToTypeID("slct"), desc, DialogModes.NO);
}

// gets an ID for the active doc
function getDocID() {
    var ref = new ActionReference();
    ref.putProperty(stringIDToTypeID("property"), stringIDToTypeID("documentID"));
    ref.putEnumerated(charIDToTypeID("Dcmn"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
    var desc = executeActionGet(ref);
    return desc.getInteger(stringIDToTypeID("documentID"));
}