Ghoul Fool
Ghoul Fool

Reputation: 6949

getDocumentByName in Photoshop

Whilst it's possible to grab a layer, history state or layerset using getByName, there doesn't appear to be one for documents when using two or more open Photoshop files.

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

Is that right?

The work around being looping over documents array till a string matches the required document:

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

Upvotes: 0

Views: 360

Answers (1)

Sergey Kritskiy
Sergey Kritskiy

Reputation: 2269

When not sure just check the scripting reference (p 104):

enter image description here

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

You can also activate documents by ID which is more reliable:

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

Upvotes: 1

Related Questions