设置图层活动的photoshop脚本

时间:2017-10-25 09:23:09

标签: photoshop

所以我有2个具有相同图层名称的打开文档。 我想在第一个文档中选择一个图层。然后运行脚本并在另一个文档中按名称自动选择相同的图层。

到目前为止,我能够存储第一层名称并打开第二个文档。 但我似乎无法将同一层设置为活动状态。

这是我的代码:

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 个答案:

答案 0 :(得分:0)

想出来!由于该图层位于某个组中,因此无法找到该图层。 使用以下代码修复它:

activeDocument.activeLayer = activeDocument.layerSets[groupname].artLayers.getByName (actLay);

答案 1 :(得分:0)

我从adobe论坛得到了这个。 有人写了一个函数来更容易地找到图层的位置。

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