Getting the list of selected layers on Photoshop

How do I get the list of selected layers on Photoshop even if the selected layers are inside groups?

thanks

http://stackoverflow.com/questions/36010408/applescript-getting-list-of-selected-layers-on-photoshop

I’m willing to help, and in fact I’m already looking at the photoshop dictionary at this moment. The problem is that SO is a very strict site that doesn’t allow general open questions and it’s not me who will delete your question, it’s others. It was a warning/heads up to keep your question online there, not to finger pointing or anything.

Edit: I’m sorry to inform that I cannot find a way to get the selected layers in plain AppleScript.

here is how I got the layers (with preserving grouping).


getLayers(document 1 of application "Adobe Photoshop CS6")

on getLayers(aRef)
	set layerNames to {}
	tell application "Adobe Photoshop CS6"
		tell aRef
			repeat with i from 1 to count layers
				if class of layer i = layer set then
					set end of layerNames to my getLayers(layer i)
				else
					set end of layerNames to name of layer i
				end if
			end repeat
		end tell
	end tell
	return layerNames
end getLayers

There is no property if the layer is selected or not. But that doesn’t always mean the end, JavaScript has more properties of objects than AppleScript but artLayer object doesn’t have any sort of ‘selected’ property either.

Last but not least I tried to take a look at GUI scripting. It seems like that the pallets in PhotoShop CS6 are completely drawn by the application itself and doesn’t use any kind of Cocoa objects int (like Tables, buttons etc…). This means they are unaccessible by script either.

Even though GUI scripting is not possible, simple actions can be executed by PhotoShop. While this script is quite buggy (since it doesn’t like layers with the same name, nor layers with special AppleScript characters in their names (‘{’, ‘}’, and ‘,’). But here is maybe a start for you:

See script below, it’s an improved version

Here an more human friendly version of the script. The script is not entirely bug free and will go wrong when the name contains an newline. But is improved compared to the previous version (which has been deleted).

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

fantastic. works like a charm.