Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
get array of selected layers in photoshop via extendscript
var docRef = app.activeDocument;
cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };
function newGroupFromLayers(doc) {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putClass( sTID('layerSection') );
desc.putReference( cTID('null'), ref );
var lref = new ActionReference();
lref.putEnumerated( cTID('Lyr '), cTID('Ordn'), cTID('Trgt') );
desc.putReference( cTID('From'), lref);
executeAction( cTID('Mk '), desc, DialogModes.NO );
};
function undo() {
executeAction(cTID("undo", undefined, DialogModes.NO));
};
function getSelectedLayers(doc) {
var selLayers = [];
newGroupFromLayers();
var group = doc.activeLayer;
var layers = group.layers;
for (var i = 0; i < layers.length; i++) {
selLayers.push(layers[i]);
}
undo();
return selLayers;
};
var selectedLayers = getSelectedLayers(app.activeDocument);
for( i = 0; i < selectedLayers.length; i++) {
selectedLayers[i].selected = true;
docRef.activeLayer = selectedLayers[i];
alert(docRef.activeLayer.name)
}
@peterceluch
Copy link

peterceluch commented Jul 24, 2020

Thank you for sharing this with us!

I'm always intrigued by these mysterious charID / actionDescriptor / executeAction snippets. They always look so machine-generated to me. How do you come up with these seemingly random method parameters? Is there a way to somehow export this from Photoshop/actions?

Thanks,
Peter

@Picazsoo
Copy link

Picazsoo commented Jan 7, 2021

Hi @peterceluch- I guess you already found this by now, but anyway - get xtools scripts here (http://ps-scripts.sourceforge.net/xtools.html).

These allow you to convert recorded actions into javascript. From there you can modify the code into useful functions with custom parameters and reuse at will.

Regards,
Jachym

@peterceluch
Copy link

peterceluch commented Jan 22, 2021

Thank you very much, @Picazsoo - I did not know about that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment