Skip to main content
Shine BR
Known Participant
February 5, 2015
Answered

Move group into another group

  • February 5, 2015
  • 2 replies
  • 2467 views

Hi all,

Can any one help to create a script to move the group named "Shine" into another group named "Shine All".

Thanks in advance.

Snapshot show for better understanding.

This topic has been closed for replies.
Correct answer c.pfaffenbichler

Does this help?

// move groups of a certain name into group of a certain name;

// 2015, use it at your own risk;

#target "photoshop-70.032"

if (app.documents.length > 0) {

var myDocument = app.activeDocument;

var name1 = "Shine";

var targetName = "Shine All";

var targetGroup = layersWithName (targetName);

// if exactly one group qualifies;

if (targetGroup.length == 1) {

// collect groups;

var theGroups = layersWithName(name1);

// process groups;

for (var m = theGroups.length - 1; m >= 0; m--) {

  moveTo(theGroups[1], targetGroup[0][1]);

  };

}

else {alert ("too many or too few groups called »"+targetName+"«")}

};

////////////////////////////////////

function layersWithName (thisName) {

// the file;

var myDocument = app.activeDocument;

// get number of layers;

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var applicationDesc = executeActionGet(ref);

var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));

// process the layers;

var theLayers = new Array;

for (var m = 0; m <= theNumber; m++) {

try {

var ref = new ActionReference();

ref.putIndex( charIDToTypeID( "Lyr " ), m);

var layerDesc = executeActionGet(ref);

var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));

var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));

// if not layer group collect values;

if (layerSet != "layerSectionEnd" /*&& layerSet != "layerSectionStart"*/ && isBackground != true) {

var theName = layerDesc.getString(stringIDToTypeID('name'));

var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));

if (theName == thisName) {

theLayers.push([theName, theID)

}

};

}

catch (e) {};

};

return (theLayers)

};

////// move //////

function moveTo (theLayer, theGroup) {

// get index;

var ref = new ActionReference();

ref.putIdentifier( charIDToTypeID( "Lyr " ), theGroup - 1);

var layerDesc = executeActionGet(ref);

var theIndex = layerDesc.getInteger(stringIDToTypeID("itemIndex"));

// =======================================================

var idmove = charIDToTypeID( "move" );

    var desc5 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref4 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        ref4.putIdentifier( idLyr, theLayer );

    desc5.putReference( idnull, ref4 );

    var idT = charIDToTypeID( "T  " );

        var ref5 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        ref5.putIndex( idLyr, theIndex );

    desc5.putReference( idT, ref5 );

    var idAdjs = charIDToTypeID( "Adjs" );

    desc5.putBoolean( idAdjs, false );

    var idVrsn = charIDToTypeID( "Vrsn" );

    desc5.putInteger( idVrsn, 5 );

executeAction( idmove, desc5, DialogModes.NO );

};

edited

2 replies

c.pfaffenbichler
Community Expert
Community Expert
February 6, 2015

I suspect I got the correct results by accident (in connection with the layer in the Group) – I am not sure when I will have time to look into this further.

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
February 5, 2015

Does this help?

// move groups of a certain name into group of a certain name;

// 2015, use it at your own risk;

#target "photoshop-70.032"

if (app.documents.length > 0) {

var myDocument = app.activeDocument;

var name1 = "Shine";

var targetName = "Shine All";

var targetGroup = layersWithName (targetName);

// if exactly one group qualifies;

if (targetGroup.length == 1) {

// collect groups;

var theGroups = layersWithName(name1);

// process groups;

for (var m = theGroups.length - 1; m >= 0; m--) {

  moveTo(theGroups[1], targetGroup[0][1]);

  };

}

else {alert ("too many or too few groups called »"+targetName+"«")}

};

////////////////////////////////////

function layersWithName (thisName) {

// the file;

var myDocument = app.activeDocument;

// get number of layers;

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

var applicationDesc = executeActionGet(ref);

var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));

// process the layers;

var theLayers = new Array;

for (var m = 0; m <= theNumber; m++) {

try {

var ref = new ActionReference();

ref.putIndex( charIDToTypeID( "Lyr " ), m);

var layerDesc = executeActionGet(ref);

var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));

var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));

// if not layer group collect values;

if (layerSet != "layerSectionEnd" /*&& layerSet != "layerSectionStart"*/ && isBackground != true) {

var theName = layerDesc.getString(stringIDToTypeID('name'));

var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));

if (theName == thisName) {

theLayers.push([theName, theID)

}

};

}

catch (e) {};

};

return (theLayers)

};

////// move //////

function moveTo (theLayer, theGroup) {

// get index;

var ref = new ActionReference();

ref.putIdentifier( charIDToTypeID( "Lyr " ), theGroup - 1);

var layerDesc = executeActionGet(ref);

var theIndex = layerDesc.getInteger(stringIDToTypeID("itemIndex"));

// =======================================================

var idmove = charIDToTypeID( "move" );

    var desc5 = new ActionDescriptor();

    var idnull = charIDToTypeID( "null" );

        var ref4 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        ref4.putIdentifier( idLyr, theLayer );

    desc5.putReference( idnull, ref4 );

    var idT = charIDToTypeID( "T  " );

        var ref5 = new ActionReference();

        var idLyr = charIDToTypeID( "Lyr " );

        ref5.putIndex( idLyr, theIndex );

    desc5.putReference( idT, ref5 );

    var idAdjs = charIDToTypeID( "Adjs" );

    desc5.putBoolean( idAdjs, false );

    var idVrsn = charIDToTypeID( "Vrsn" );

    desc5.putInteger( idVrsn, 5 );

executeAction( idmove, desc5, DialogModes.NO );

};

edited

Shine BR
Shine BRAuthor
Known Participant
February 5, 2015

Thank you pfaffen,

I am using CS6, I got a error message @the line 66, it would be great if you could fix this. plz see the screen grab below.

c.pfaffenbichler
Community Expert
Community Expert
February 5, 2015

I can’t reproduce the issue, can you post the (downsampled) file in which the behaviour occurs?