Select layer by name in other document
Copy link to clipboard
Copied
So i have 2 open documents.
They both have the same names in layers.
I wrote a script that should select a layer with the same name as the selected layer in the first document.
Here is the script. Currently the code that does not work is commented out.
var aDoc = app.activeDocument;
var AllDocs = app.documents;
var actLay = aDoc.activeLayer;
if (AllDocs.length > 1) {
var itemDoc = null;
var win = new Window("dialog","select the same name in other document");
this.windowRef = win;
win.Txt1 = win.add ("statictext", undefined, "Paste in which open document?");
win.NewList=win.add ("dropdownlist", undefined, AllDocs);
win.NewList.selection = 0;
itemDoc = win.NewList.selection.index;
win.testBtn4 = win.add('button', [260,140,100,50], 'select the same name in other document', {name:'doding1'});
win.testBtn4.onClick = dothing;
//Get selected document from list
win.NewList.onChange= function () {
itemDoc = win.NewList.selection.index;
return itemDoc;
}
//Show al items
win.show();
function dothing()
{
//Make the selected document the active document.
app.activeDocument = app.documents[itemDoc];
app.refresh();
//This outputs [Artlayer layername]
//alert (actLay);
//Find right layer and set active THIS DOES NOT WORK!!
//app.activeDocument.activeLayer = app.activeDocument.layers.itemByName(actLay);
win.close();
}
}
else
{
alert ("No other documents open");
}
Explore related tutorials & articles
Copy link to clipboard
Copied
do in such way
app.activeDocument.activeLayer = app.activeDocument.layers.getByName(actLay.name);
Copy link to clipboard
Copied
Right from the beginning, define what docs are what to keep them in order, then you can refer to the right document in your script:
var docA = app.documents[0];
var docB = app.documents[1];
app.activeDocument = docB//makes docB active.
Copy link to clipboard
Copied
Figured it out! Because the layer was in a certain group it couldn't find the layer.
fixed it with the following code:
activeDocument.activeLayer = activeDocument.layerSets[groupname].artLayers.getByName (actLay);
Copy link to clipboard
Copied
This function will help you to select a layer wherever it is located in document.
//usage example:
select_layer(actLay.name);
////////////////////////////////////////////////////////////////////////////////////////////
function select_layer(id, add, viz)
{
try {
var d = new ActionDescriptor();
if (viz == undefined) viz = false;
var r = new ActionReference();
if (typeof(id) == "string") r.putName( charIDToTypeID( "Lyr " ), id);
else r.putIdentifier( charIDToTypeID( "Lyr " ), id);
d.putReference( charIDToTypeID( "null" ), r );
d.putBoolean( charIDToTypeID( "MkVs" ), viz );
if (add == true) d.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
if (add == -1) d.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "removeFromSelection" ) );
var ok = true;
try { executeAction( charIDToTypeID( "slct" ), d, DialogModes.NO ); } catch(e) { ok = false; }
d = null;
return ok;
}
catch (e) { alert(e); return false; }
}
Copy link to clipboard
Copied
r-bins function for me seems to be the only one working solution as of 20 dec 2017 on a newly created document
Copy link to clipboard
Copied
Because I saw bena10018036​ used your snippet in his newest script (Search and Replace Layers ) I'd like to share my little find about some unconsistency in quite good example (especially that was one of you first scripts I found on this forum )
d.putBoolean( charIDToTypeID( "MkVs" ), viz ); // where viz = true
doesn't work when desired layer was selected before running your snippet. So if its visibility was set originally to false then it will remain false as it can't be selected when it's already selected. It can be solved adding after above line these two lines:
d.putReference(stringIDToTypeID('null'), r)
executeAction(stringIDToTypeID('selectNoLayers'), d, DialogModes.NO)
So that layer would be firstly unselected by a snippet to be selected and made visible.
That'd be good if later there weren't selectionModifier parts. In this situation it is best in select_layer function to change:
d.putBoolean( charIDToTypeID( "MkVs" ), viz );
to
if (viz) ExecuteAction(stringIDToTypeID('show'), d, DialogModes.NO)
A tweak for either singly selected layer or more layers they're going to be added to selection, or desired one removed from.

