2016-03-15 65 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

相關問題