Copy link to clipboard
Copied
Hi, I'm not very good at java scripts so I was wondering if you could help.
I'd like to be able to select a bunch of layers in photoshop, and run a script that would prefix all the layers names with whatever I choose in the script.
I don't want any UI, I just want it to add the prefix and be done, no questions asked. I'll edit the script to modify the exact prefix text. It has to only affect selected layers OR the layers within a selected folder.
I know it's probably stupid simple.
Thanks in advance.
Ian
Yup simple...
...#target photoshop;
app.bringToFront();
if(documents.length){
var prefix = "my new prefix-"
var selLayers = getSelectedLayersIdx();
for (var a in selLayers){
selectLayerByIndex(selLayers);
activeDocument.activeLayer.name = prefix + activeDocument.activeLayer.name;
}
}
function selectLayerByIndex(index,add){
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), index);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(
Copy link to clipboard
Copied
Yup simple...
#target photoshop;
app.bringToFront();
if(documents.length){
var prefix = "my new prefix-"
var selLayers = getSelectedLayersIdx();
for (var a in selLayers){
selectLayerByIndex(selLayers);
activeDocument.activeLayer.name = prefix + activeDocument.activeLayer.name;
}
}
function selectLayerByIndex(index,add){
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), index);
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){}
};
function getSelectedLayersIdx(){
var selectedLayers = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
var c = desc.count
var selectedLayers = new Array();
for(var i=0;i<c;i++){
try{
activeDocument.backgroundLayer;
selectedLayers.push( desc.getReference( i ).getIndex() );
}catch(e){
selectedLayers.push( desc.getReference( i ).getIndex()+1 );
}
}
}else{
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
try{
activeDocument.backgroundLayer;
selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);
}catch(e){
selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));
}
var vis = app.activeDocument.activeLayer.visible;
if(vis == true) app.activeDocument.activeLayer.visible = false;
var desc9 = new ActionDescriptor();
var list9 = new ActionList();
var ref9 = new ActionReference();
ref9.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
list9.putReference( ref9 );
desc9.putList( charIDToTypeID('null'), list9 );
executeAction( charIDToTypeID('Shw '), desc9, DialogModes.NO );
if(app.activeDocument.activeLayer.visible == false) selectedLayers.shift();
app.activeDocument.activeLayer.visible = vis;
}
return selectedLayers;
};
Copy link to clipboard
Copied
Super helpful!
More complicated of a script than I though it would be.
If you get a min, can you point me in the direction of a script that will remove "copy" without a UI?
Many thanks!
Ian
Copy link to clipboard
Copied
Moving to Photoshop Scripting Forum.
Copy link to clipboard
Copied
Here you are.
#target photoshop;
app.bringToFront();
if(documents.length) main();
function main(){
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;
try{
activeDocument.backgroundLayer;
var i = 0; }catch(e){ var i = 1; };
for(i;i<count;i++){
if(i == 0) continue;
ref = new ActionReference();
ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
var desc = executeActionGet(ref);
var layerName = desc.getString(charIDToTypeID( 'Nm ' ));
if(layerName.match(/copy/gi)) putLayerNameByIndex( i, layerName.replace(/copy/gi,'').replace(/\s+/g,' ') );
};
};
function putLayerNameByIndex( idx, name ) {
if( idx == 0 ) return;
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( 'Lyr ' ), idx );
desc.putReference( charIDToTypeID('null'), ref );
var nameDesc = new ActionDescriptor();
nameDesc.putString( charIDToTypeID('Nm '), name );
desc.putObject( charIDToTypeID('T '), charIDToTypeID('Lyr '), nameDesc );
executeAction( charIDToTypeID( 'slct' ), desc, DialogModes.NO );
executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
};
Copy link to clipboard
Copied
Perfect! Worked great!