Copy link to clipboard
Copied
Hi all,
I need a little function which is telling me if there is a selected layer or not. Now I always get back the active layer even if it isn't selected with this AM Code:
getSelectedLayerId: function()
{
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Lyr '),charIDToTypeID('Ordn'),charIDToTypeID('Trgt') );
return executeActionGet(ref).getInteger(stringIDToTypeID( "layerID" ));
},
Thank You in advance!
I believe this has been fixed in the latest version of Photoshop, but I dont't have it so not sure.
Up to the latest you could use the following code as it checks to see if a single layer is selected by checking the layers visibility.
...function getSelectedLayersIdx(){
var selectedLayers = new Array();
var backGroundCounter = 1;
if(activeDocument.artLayers.length > 0){
backGroundCounter = activeDocument.artLayers[activeDocument.artLayers.length - 1].isBackgroundLaye
Copy link to clipboard
Copied
I believe this has been fixed in the latest version of Photoshop, but I dont't have it so not sure.
Up to the latest you could use the following code as it checks to see if a single layer is selected by checking the layers visibility.
function getSelectedLayersIdx(){
var selectedLayers = new Array();
var backGroundCounter = 1;
if(activeDocument.artLayers.length > 0){
backGroundCounter = activeDocument.artLayers[activeDocument.artLayers.length - 1].isBackgroundLayer ? 0 : 1;
}
var ref = new ActionReference();
ref.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("targetLayers"));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if( desc.hasKey( stringIDToTypeID( "targetLayers" ) ) ){
desc = desc.getList( stringIDToTypeID( "targetLayers" ));
var c = desc.count;
var selectedLayers = new Array();
for(var i=0;i<c;i++){
selectedLayers.push( desc.getReference( i ).getIndex() +backGroundCounter );
}
if(app.version.match(/^\d+/) > 15) return selectedLayers ;
}else{
if(app.version.match(/^\d+/) > 15) return selectedLayers ;
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
if(!backGroundCounter){
selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )) -1);
}else{
selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));
}
var vis = app.activeDocument.activeLayer.visible;
if(vis == true) app.activeDocument.activeLayer.visible = false;
var desc9 = new ActionDescriptor();
var list9 = new ActionList();
var ref9 = new ActionReference();
ref9.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
list9.putReference( ref9 );
desc9.putList( charIDToTypeID("null"), list9 );
executeAction( charIDToTypeID("Shw "), desc9, DialogModes.NO );
if(app.activeDocument.activeLayer.visible == false) selectedLayers.shift();
app.activeDocument.activeLayer.visible = vis;
}
return selectedLayers;
};
Copy link to clipboard
Copied
Thank You SuperMerlin! It is working well.
Copy link to clipboard
Copied
SuperMerlin , it hasn't been fixed (at least not until CC 2017) – if you try with basic DOM code such as
app.activeDocument.activeLayer
it keeps pointing to a layer even if none is active.
Davide