2016-03-15 88 views
0

我正在创建一个AppleScript,我需要在Photoshop上对所选图层执行某些操作。Applescript:在Photoshop中获取选定图层的列表

即使所选图层位于组内,我如何获取Photoshop上选定图层的列表?

我没有现在显示的代码,因为它全部从选定图层列表开始,对不起。

回答

2

所选图层不是JavaScript的artLayer对象中的属性,并且所选图层并非AppleScript中的layer对象的属性。但是,我们可以在PhotoShop中使用AM,并使用动作和描述符结果来获取选定的图层。因为根据是否有背景图层,图层可能需要快速移动,所以我们首先创建一个具有选定索引的数组(代码基于this post),然后我们解析图层的名称。

tell application "Adobe Photoshop CS6" 
    tell document 1 
     set selectedLayers to paragraphs of (do javascript " 
var typeDocument  = stringIDToTypeID('document'); 
var typeItemIndex  = stringIDToTypeID('itemIndex'); 
var typeLayer   = stringIDToTypeID('layer'); 
var typeName   = stringIDToTypeID('name'); 
var typeOrdinal   = stringIDToTypeID('ordinal'); 
var typeProperty  = stringIDToTypeID('property'); 
var typeTarget   = stringIDToTypeID('targetEnum'); 
var typeTargetLayers = stringIDToTypeID('targetLayers'); 
var selectedLayers = new Array(); 
var actionRef   = new ActionReference(); 

actionRef.putEnumerated(typeDocument, typeOrdinal, typeTarget); 
var actionDesc = executeActionGet(actionRef); 

if(actionDesc.hasKey(typeTargetLayers)){ 
    actionDesc = actionDesc.getList(typeTargetLayers); 
    var c = actionDesc.count 

    for(var i=0;i<c;i++){ 
     try{ 
      activeDocument.backgroundLayer; 
      selectedLayers.push(actionDesc.getReference(i).getIndex()); 
     }catch(e){ 
      selectedLayers.push(actionDesc.getReference(i).getIndex()+1); 
     } 
    } 
}else{ 
    var actionRef = new ActionReference(); 
    actionRef.putProperty(typeProperty , typeItemIndex); 
    actionRef.putEnumerated(typeLayer, typeOrdinal, typeTarget); 
    try{ 
     activeDocument.backgroundLayer; 
     selectedLayers.push(executeActionGet(actionRef).getInteger(typeItemIndex)-1); 
    }catch(e){ 
     selectedLayers.push(executeActionGet(actionRef).getInteger(typeItemIndex)); 
    } 
} 

var selectedLayerNames = new Array(); 

for (var a in selectedLayers){ 
    var ref = new ActionReference(); 
    ref.putIndex(typeLayer, Number(selectedLayers[a])); 
    var layerName = executeActionGet(ref).getString(typeName); 
     selectedLayerNames.push(layerName); 
} 

selectedLayerNames.join('\\n'); 
") 

    end tell 
end tell 
+0

太棒了。奇迹般有效。 – SpaceDog

相关问题