Skip to main content
Known Participant
March 9, 2024
Answered

Script to move a layer/ group into another group

  • March 9, 2024
  • 2 replies
  • 458 views

Hello, I have several files to relocate with same format every day as picture below.

I need to find a script to move:

  • layer "set 1" into group 1
  • group "set 2" into group 2
  • layer "set 3" into group 3

And if the file don't have layer/ group "set 1", "set 2" or "set 3" just ignore it. 

Thank you in advance!

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

Mabe this can help get you started. 

It collects two Arrays, one of all the Layers with a name like »group x« and one of the layers with a name like »set x«. 

You can then run them against each other to compare the names and process them accordingly. 

 

// 2024, use it at your own risk;
if (app.documents.length > 0) {
var theGroups = collectLayersOfName (/^group \d{1,3}$/i);
var theSets = collectLayersOfName (/^set \d{1,3}$/i);
alert ("layers with »group« in their names\n"+theGroups.join("\n"));
alert ("layers with »set« in their names\n"+theSets.join("\n"));
};
////// collect layers //////
function collectLayersOfName (theRegExp) {
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
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 group collect values;
if (layerSet != "layerSectionEnd" /*&& layerSet != "layerSectionStart" && isBackground != true*/) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theColor = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("color")));
if (theName.match(theRegExp) != null) {theLayers.push([theName, theIndex, theID, theColor, theName.match(/\d{1,3}$/)])}
};
}
catch (e) {};
};
return theLayers
};

 

 

2 replies

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
March 11, 2024

Mabe this can help get you started. 

It collects two Arrays, one of all the Layers with a name like »group x« and one of the layers with a name like »set x«. 

You can then run them against each other to compare the names and process them accordingly. 

 

// 2024, use it at your own risk;
if (app.documents.length > 0) {
var theGroups = collectLayersOfName (/^group \d{1,3}$/i);
var theSets = collectLayersOfName (/^set \d{1,3}$/i);
alert ("layers with »group« in their names\n"+theGroups.join("\n"));
alert ("layers with »set« in their names\n"+theSets.join("\n"));
};
////// collect layers //////
function collectLayersOfName (theRegExp) {
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
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 group collect values;
if (layerSet != "layerSectionEnd" /*&& layerSet != "layerSectionStart" && isBackground != true*/) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theColor = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("color")));
if (theName.match(theRegExp) != null) {theLayers.push([theName, theIndex, theID, theColor, theName.match(/\d{1,3}$/)])}
};
}
catch (e) {};
};
return theLayers
};

 

 

KathyDangAuthor
Known Participant
March 12, 2024

Thank you for your kind!

c.pfaffenbichler
Community Expert
Community Expert
March 12, 2024

// find matches of layers named »group x« and »set x«;
// move layers »set x« into groups »group x«;
// 2024, use it at your own risk;
if (app.documents.length > 0) {
var theGroups = collectLayersOfName (/^group \d{1,3}$/i, true);
var theSets = collectLayersOfName (/^set \d{1,3}$/i, false);
for (var m = 0; m < theSets.length; m++) {
    var thisLayer = theSets[m];
    for (var n = 0; n < theGroups.length; n++) {
        var thisGroup = theGroups[n];
// if number matches move layer;
        if (thisLayer[4] == thisGroup[4]) {
            var theIndex = getLayerIndex(thisGroup[2]);
            selectLayerByID(thisLayer[2], false);
            moveTo(theIndex);
        }
    }
}
};
////// collect layers //////
function collectLayersOfName (theRegExp, isGroup) {
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
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 group collect values;
if (layerSet != "layerSectionEnd" /*&& layerSet != "layerSectionStart" && isBackground != true*/) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
var theColor = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("color")));
if (layerSet == "layerSectionStart" && isGroup == true) {
if (theName.match(theRegExp) != null) {theLayers.push([theName, theIndex, theID, theColor, Number(theName.match(/\d{1,3}$/))])}
};
if (layerSet != "layerSectionStart" && isGroup == false) {
if (theName.match(theRegExp) != null) {theLayers.push([theName, theIndex, theID, theColor, Number(theName.match(/\d{1,3}$/))])}
};
};
}
catch (e) {};
};
return theLayers
};
////// based on code by mike hale, via paul riggott //////
function selectLayerByID(id,add){
try {
add = undefined ? add = false:add 
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), id);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) ); 
desc.putBoolean( charIDToTypeID( "MkVs" ), false ); 
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message); 
}
} catch (e) {}
};
////// move layer into group //////
function moveTo(index) {
try {
var desc = new ActionDescriptor();
    var ref = new ActionReference();
    ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
desc.putReference( charIDToTypeID('null'), ref );
    var ref = new ActionReference();
    ref.putIndex( charIDToTypeID('Lyr '), index );
desc.putReference( charIDToTypeID('T   '), ref );
desc.putBoolean( charIDToTypeID('Adjs'), false );
desc.putInteger( charIDToTypeID('Vrsn'), 5 );
executeAction( charIDToTypeID('move'), desc, DialogModes.NO );
} catch (e) {}
};
////// by mike hale, via paul riggott //////
function getLayerIndex(theId){
var ref = new ActionReference(); 
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('itemIndex'));
ref.putIdentifier(charIDToTypeID("Lyr "), theId); 
d = executeActionGet(ref);
return (d.getInteger(stringIDToTypeID('itemIndex'))-1); 
}; 
c.pfaffenbichler
Community Expert
Community Expert
March 10, 2024

This should be possible even with DOM code, so what is giving you problems?