Copy link to clipboard
Copied
Hi everyone!
Would it be possible to create a script that recognizes the amount of groups with a specific name and runs a corresponding action?
I have images with groups named Alpha 1 through Alpha 32. The number of groups varies between them.
I need a script that recognizes how many groups with that name Alpha exist, and run an action.
Ex: If Alpha groups 1 to 8 exist, run the action "8 Groups"; Alpha 1 to Alpha 9, run "9 Groups" and so on.
I have a script that recognizes if the layer exists and runs an action if it exists or not, but as the names are sequential, it doesn't work correctly. I don't know if this is a starting point, but this is what I have here:
#target Photoshop
app.bringToFront();
if ( layerExist("Alpha 8") ){
} else {
app.doAction("Warning 8", "MVS");
}
if ( layerExist("Alpha 9") ){
} else {
app.doAction("8 Groups", "MVS");
}
if ( layerExist("Alpha 10") ){
} else {
app.doAction("9 Groups", "MVS");
}
function layerExist(lyrName){
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putName( charIDToTypeID( "Lyr " ), lyrName);
desc.putReference( charIDToTypeID( "null" ), ref );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
try{
executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
}catch(e){return false;}
return true;
};
With this script, because there are no Alpha 9, it runs the "8 Groups" action, but consequently, we don't have Alpha 10 and it ends up running the "9 Groups" as well and all the others in the sequence.
So I need a way to recognize the number of "Alphas", not the group name, to be able to run the specific action.
Thanks!
try this
for (var i = 32; i >= 1; i--)
{
if (has_layer("Alpha " + i))
{
app.doAction(i + " Groups", "MVS");
break;
}
}
function has_layer(name)
{
var r = new ActionReference();
r.putName(stringIDToTypeID("layer"), name);
try { executeActionGet(r); return true; } catch (e) { return false; }
}
Copy link to clipboard
Copied
Edit: The code collects an Array of Arrays that contain the name, index and id of the groups the names of which match the pattern »alpha XX«.
var theRegExp = new RegExp(/^alpha [0-9]{1,5}$/i);
var theGroups = collectGroupsOfCertainNames (theRegExp)
alert (theGroups.length+" groups with the name:\n"+theGroups.join("\n"));
////// collect layers //////
function collectGroupsOfCertainNames (searchName) {
// 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 == "layerSectionStart") {
var theName = layerDesc.getString(stringIDToTypeID('name'));
// collect if condition is met;
if (theName.match(searchName) != undefined) {
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
theLayers.push([theName, theIndex, theID])
}
};
}
catch (e) {};
};
return theLayers
};
////// based on code by mike hale, via paul riggott //////
function selectLayerByID(id,add){
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);
}
};
Copy link to clipboard
Copied
try this
for (var i = 32; i >= 1; i--)
{
if (has_layer("Alpha " + i))
{
app.doAction(i + " Groups", "MVS");
break;
}
}
function has_layer(name)
{
var r = new ActionReference();
r.putName(stringIDToTypeID("layer"), name);
try { executeActionGet(r); return true; } catch (e) { return false; }
}
Copy link to clipboard
Copied
It worked perfectly! Thanks @r-bin