Skip to main content
Participating Frequently
March 12, 2024
Answered

Independent scripts for moving layers/group into existing groups

  • March 12, 2024
  • 2 replies
  • 287 views

Hello everyone, I would like to finding scripts for the following tasks

- I have a lot of files with similar formats that I need to process every day. Repeating the same steps is quite time-consuming, and I believe scripts can help me solve this problem.

- I would like to find 5 separate scripts for the 5 cases I need to handle:

  • Script 1: Delete empty layers (if any):
  • Script 2: Move all layers with the name format "phone + X" to the "Connect" group (where X can be any number or letter)_If the group doesn't exist, do nothing.
  • Script 3: Move all "Camera" groups to the "Digital" group and then delete the "Digital" group_If the group doesn't exist, do nothing.
  • Script 4: Move all text layers (whether hidden or visible) to the "Text" group and then make all text layers visible_If there's no text exist, do nothing.
  • Script 5: Move the layer named "Right" to the "Position" folder_If the layer doesn't exist, do nothing.

- I have attached a sample file below. Thank you very much!

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

 

// move layers of a certain name into group of a certain name;
// 2024, use it at your own risk;
if (app.documents.length > 0) {
var theGroups = collectLayersOfName (/^connect$/i, true);
var theLayers = collectLayersOfName (/^phone \d{1,3}$/i, false);
// if there is only one group;
if (theGroups.length == 1) {
var thisGroup = theGroups[0];
for (var m = 0; m < theLayers.length; m++) {
    var thisLayer = theLayers[m];
// move into group;
    selectLayerByID(thisLayer[2], false);
    moveTo(thisGroup[1]-1);
}
}
};
////// 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])}
};
if (layerSet != "layerSectionStart" && isGroup == false) {
if (theName.match(theRegExp) != null) {theLayers.push([theName, theIndex, theID, theColor])}
};
};
}
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) {}
};

edited

 

2 replies

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

 

// move layers of a certain name into group of a certain name;
// 2024, use it at your own risk;
if (app.documents.length > 0) {
var theGroups = collectLayersOfName (/^connect$/i, true);
var theLayers = collectLayersOfName (/^phone \d{1,3}$/i, false);
// if there is only one group;
if (theGroups.length == 1) {
var thisGroup = theGroups[0];
for (var m = 0; m < theLayers.length; m++) {
    var thisLayer = theLayers[m];
// move into group;
    selectLayerByID(thisLayer[2], false);
    moveTo(thisGroup[1]-1);
}
}
};
////// 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])}
};
if (layerSet != "layerSectionStart" && isGroup == false) {
if (theName.match(theRegExp) != null) {theLayers.push([theName, theIndex, theID, theColor])}
};
};
}
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) {}
};

edited

 

c.pfaffenbichler
Community Expert
Community Expert
March 12, 2024

Ad 1)

Have you tried »Delete All Empty Layers.jsx«? 

 

As for other issues: Something similar came up recently, maybe the Script can serve as a basis for your custom Scripts.  

https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-move-a-layer-group-into-another-group/td-p/14477778