skip to Main Content

So I have 2 open documents with the same layer names.
I want to select a layer in the first document. Then run the script and automaticly select the same layer by name in the other document.

So far i’ve bin able to store the first layer name and open the 2nd document.
But I can’t seem to set the same layer active.

This is my code:

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

2

Answers


  1. Chosen as BEST ANSWER

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

  2. I got this from the adobe forum.
    Someone wrote a function to find the location of the layer more easily.

    //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; }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search