Move selection to new group

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

PhantomX
Posts: 3
Joined: Wed Jan 10, 2018 4:40 pm

Move selection to new group

Post by PhantomX »

Hey,
I'm trying to move the layer selection to a new group while keeping the same hierarchy.

I get the selected layers using this function:

Code: Select all


function GetSelectedLayers()
{
var idGrp = stringIDToTypeID( "groupLayersEvent" );
var descGrp = new ActionDescriptor();
var refGrp = new ActionReference();
refGrp.putEnumerated(charIDToTypeID( "Lyr " ),charIDToTypeID( "Ordn" ),charIDToTypeID( "Trgt" ));
descGrp.putReference(charIDToTypeID( "null" ), refGrp );
executeAction( idGrp, descGrp, DialogModes.ALL );

var resultLayers = new Array();

for (var ix=0;ix<app.activeDocument.activeLayer.layers.length;ix++)
{
resultLayers.push(app.activeDocument.activeLayer.layers[ix])
}

var desc5 = new ActionDescriptor();
var ref2 = new ActionReference();

ref2.putEnumerated( cTID("HstS"), cTID("Ordn"), cTID("Prvs"));
desc5.putReference( cTID("null"), ref2 );
executeAction(cTID("slct"), desc5, DialogModes.NO );

return resultLayers;
}
and try to move them in a new layer set using this one:

Code: Select all


function MoveToNewSet(setName,selection)
{
var doc = app.activeDocument;
var newLayerSet = doc.layerSets.add();
newLayerSet.name = setName;

for(i = 0; i < selection.length; i++)
{
selection[i].move(newLayerSet, ElementPlacement.INSIDE);
}
}
As soon as it encounters a group I get an error, so I tried to move the group separately, but can't find a way to do it as I get an illegal argument error. Tried to duplicate the group into the new group, looks like you cannot do that either.

I'm very new to the Photoshop scripting so any help would be appreciated!
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Move selection to new group

Post by Kukurykus »

Moving, so copying content to new destination and removing its source:

Code: Select all

function sTT(v) {return stringIDToTypeID(v)}
(ref1 = new ActionReference()).putClass(sTT('layerSection'));
(dsc1 = new ActionDescriptor()).putReference(sTT('null'), ref1);
(ref2 = new ActionReference()).putEnumerated(sTT('layer'), sTT('ordinal'), sTT('targetEnum'))
dsc1.putReference(sTT('from'), ref2), executeAction(sTT('make'), dsc1, DialogModes.NO)