Copy link to clipboard
Copied
Seems like a really fundamental/basic question that I can't answer, but is there a way (in ESTK) to get a layer reference by (doc and) layer Id? This is mainly a problem if you are sending a script to be evaluated from outside like Generator plugin, i.e. you know the Id of the layer you want to do something and are giving it as a parameter to JSX scripts. First trivial option is to traverse the DOM and search for your layer but in the worst case this will take minutes. Second option would be to use AM-code as everything works with layer Ids, but then you can't use the Javascript DOM API and you are really hampering your options. Third option that is closest viable would be to set the layer as active using AM-code (which might be needed in many cases anyhow) and use the Document.activeLayer. But can you use AM-code to get a DOM layer reference? Essentially I'm looking for a Document.getLayerById().
Hi matias.kiviniemi,
I thought SuperMerlin published a snippet which could help you - but I can't find the old thread. Try it
function getLayerID(){
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Lyr '),charIDToTypeID('Ordn'),charIDToTypeID('Trgt') );
return executeActionGet(ref).getInteger(stringIDToTypeID( "layerID" ));
};
If so, have fun
Copy link to clipboard
Copied
alert(app.activeDocument.layers[0]);
alert(app.activeDocument.layers[0].name);
Copy link to clipboard
Copied
Hi matias.kiviniemi,
I thought SuperMerlin published a snippet which could help you - but I can't find the old thread. Try it
function getLayerID(){
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Lyr '),charIDToTypeID('Ordn'),charIDToTypeID('Trgt') );
return executeActionGet(ref).getInteger(stringIDToTypeID( "layerID" ));
};
If so, have fun
Copy link to clipboard
Copied
CC2018. It is checked on the document (not active) with approximately 500 layers among which there are many nested folders. Works very quickly.
function get_layer_by_id(id, doc_id)
{
try {
var doc;
if (doc_id == undefined) doc = activeDocument;
else for (var i = 0; i < documents.length; i++)
{
if (documents.id == doc_id)
{
doc = documents;
break;
}
}
if (doc == undefined) { alert("Bad document " + doc_id); return null; }
var r = new ActionReference();
r.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("json"));
if (doc_id == undefined) r.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
else r.putIdentifier(charIDToTypeID("Dcmn"), doc_id);
eval("var json = " + executeActionGet(r).getString(stringIDToTypeID("json")));
if (json == undefined) return null;
var set = new Array();
function search_id(layers, id)
{
for (var i = 0; i < layers.length; i++)
{
if (layers.id == id) { set.push(i); return true; }
}
for (var i = 0; i < layers.length; i++)
{
if (layers.layers)
{
if (search_id(layers.layers, id)) { set.push(i); return true; }
}
}
}
if (search_id(json.layers, id))
{
var ret = doc.layers;
for (var i = set.length-1; i > 0; i--)
{
ret = ret[set].layers;
}
return ret[set[0]];
}
return null;
}
catch (e) { alert(e); }
}
Copy link to clipboard
Copied
This function doesn't work as advertized for me (i.e. it fails to retreive a layer by its ID), but in its shortened form it solves a completely different problem I was having for a long time.
If we turn a small piece of your code into its own function...
function getDocData() {
var r = new ActionReference();
r.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("json"));
r.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
eval("var json = " + executeActionGet(r).getString(stringIDToTypeID("json")));
return json;
}
...it retrieves an object with data so exhaustive that traversing DOM becomes almost completely useless in a lot of cases (which is a good thing seeing how it's 3 to 4 orders of magnitude slower).
This is an actual mythical weapon right here. Thank you for sharing!
Copy link to clipboard
Copied
This function doesn't work as advertized for me (i.e. it fails to retreive a layer by its ID)
By @13×666
"[i]" removed from the code text ... : (
Copy link to clipboard
Copied
You can edit own posts if you want...
Copy link to clipboard
Copied
Ah. The code makes much more sense now! For a while I'd thought I was going insane. 😄