Skip to content

Instantly share code, notes, and snippets.

@hilukasz
Last active July 12, 2023 15:52
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save hilukasz/03b17ee78414aadff995 to your computer and use it in GitHub Desktop.
Save hilukasz/03b17ee78414aadff995 to your computer and use it in GitHub Desktop.
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)
}
@mattbruv
Copy link

Thanks for this. It's pretty ridiculous that Adobe didn't provide a simple function that would do this considering how important this kind of thing is for scripting

@Kailang
Copy link

Kailang commented Sep 1, 2019

Thanks! I wrote an exporter based on your script!
https://gist.github.com/Kailang/cef096bea6f63646569fd81c2d1b2fe8

@samavan
Copy link

samavan commented Dec 11, 2019

Also Ridiculous that is the only decent solution to get selected groups name.
Indeed browsed photoshop PDFs and found nothing for it.
1000 thanks for it! :)

@catsmu
Copy link

catsmu commented Mar 6, 2020

Thanks very much for the solution - worked perfectly!

Regarding the comments about this not being supported via the API - at a purely practical level I agree, and it's annoying that you have to resort to a mixture of grouping trickery and Script Listener output to make this work. One should be able to perform this sort of operation without modifying the state of the underlying document (even temporarily) - it's just a query.

Speculatively: it's implemented this way because the PS API reflects how Photoshop internally thinks about the document. You can't edit more than one active ArtLayer at the same time, so the concept of "selected layers" doesn't really make sense to the underlying system - which the API ostensibly exposes.

Not sure if Adobe provides a script interface to the PS UI, which (if my guess above is correct-ish) would be the place to find / implement this kind of functionality.

@schroef
Copy link

schroef commented Mar 24, 2020

Why grouping the layers and then regrouping, doesn't that slow down the script?

@schroef
Copy link

schroef commented Mar 24, 2020

One thing, the script's name isnt correct I believe. SInce this groups layers, why not name accordingly.
PS why is that undo() in that loop?

@peterceluch
Copy link

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

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

@myrecs
Copy link

myrecs commented Dec 11, 2022

when i do selectedLayers[i].resize it resizes all layers in selection set. cant figure out why it happens. could you help?

@myrecs
Copy link

myrecs commented Dec 11, 2022

when i do selectedLayers[i].resize it resizes all layers in selection set. cant figure out why it happens. could you help?

got it. we need to select each layer for resize

@schroef
Copy link

schroef commented Dec 15, 2022

@myrecs
Well you can change the function to your liking, so for example results ze each one after the other. Depends where you out the resize

When i think about it, it should actually resize each layer one by one. At least if you out the code like you wrote. Since its using [i] per layer. Dont understand why it would resize them all at once. My only guess is that it also resize the artlayergroup at the end. It would need some extra code it guess

@NolPixel
Copy link

One thing, the script's name isnt correct I believe. SInce this groups layers, why not name accordingly. PS why is that undo() in that loop?

My opinion, the main idea of this script is get the child layer, after grouping. He collect the child layer by that group (doc.activeLayer.layers), after collecting, he undo back to ungroup.

@schroef
Copy link

schroef commented Jul 12, 2023

@no
I think this resize all items at once and the undoes the grouping.

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