Hello Mark,
Also thanks for sharing the script, very usefull. I'm trying to add something to the script so that only selected layers in the document where layers are selected are processed with your renameLayer function. But i can't seem to find the correct code to only get the selected layers and layers sets
from this document. Is this even possible to do ? I'm not an experienced coder myself but I can read your script (and write fairly basic scripts). I was trying to fill an array with the selected layer and layer-sets that needed to be renamed, but I even got stuck there.
Thanks,
Fred
Try this. Just select the layers you want 'copy' removed from and run this script.
if( app.documents.length > 0 ){
app.activeDocument.suspendHistory('Rename selected layers','removeCopyFromSelectedLayersNames()');
}
function removeCopyFromLayerName(){
if( getSelectedLayersIdx().length > 1 ){
var selectedLayers = getSelectedLayersIdx();
makeActiveByIndex( selectedLayers[0], false );
}
var startLoop = Number( !hasBackground() );
var endLoop = getNumberOfLayer() + 1;
for( var l = startLoop;l < endLoop; l++){
while( !isValidActiveLayer( l ) ) {
l++;
}
var oldName = getLayerNameByIndex( l );
var newName = oldName.replace(/\scopy\s?\d*$/i,'');
putLayerNameByIndex( l, newName )
}
if( selectedLayers != undefined ) makeActiveByIndex( selectedLayers, false );
}
function removeCopyFromSelectedLayersNames(){
var selectedLayers = getSelectedLayersIdx();
for( var l = 0;l < selectedLayers.length; l++){
var oldName = getLayerNameByIndex( selectedLayers[ l ] );
var newName = oldName.replace(/\scopy.*$/i,'');
makeActiveByIndex( selectedLayers[ l ], false );
putLayerNameByIndex( selectedLayers[ l ], newName )
}
makeActiveByIndex( selectedLayers, false );
}
function getNumberOfLayer(){
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
var desc = executeActionGet(ref);
var numberOfLayer = desc.getInteger(charIDToTypeID('NmbL'));
return numberOfLayer;
}
function getLayerNameByIndex( idx ) {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID('Prpr') , charIDToTypeID( 'Nm ' ));
ref.putIndex( charIDToTypeID( 'Lyr ' ), idx );
return executeActionGet(ref).getString(charIDToTypeID( 'Nm ' ));;
}
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('setd'), desc, DialogModes.NO );
}
function getActiveLayerIndex() {
var ref = new ActionReference();
ref.putProperty( 1349677170 , 1232366921 );
ref.putEnumerated( 1283027488, 1332896878, 1416783732 );
var res = executeActionGet(ref).getInteger( 1232366921 )
- Number( hasBackground() );
return res;
}
function isValidActiveLayer( idx ) {
var propName = stringIDToTypeID( 'layerSection' );
var ref = new ActionReference();
ref.putProperty( 1349677170 , propName);
ref.putIndex( 1283027488, idx );
var desc = executeActionGet( ref );
var type = desc.getEnumerationValue( propName );
var res = typeIDToStringID( type );
return res == 'layerSectionEnd' ? false:true;
}
function hasBackground(){
var res = undefined;
try{
var ref = new ActionReference();
ref.putProperty( 1349677170 , 1315774496);
ref.putIndex( 1283027488, 0 );
executeActionGet(ref).getString(1315774496 );;
res = true;
}catch(e){ res = false}
return res;
}
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++){
selectedLayers.push( desc.getReference( i ).getIndex());
}
}else{
var ref = new ActionReference();
ref.putProperty( charIDToTypeID('Prpr') , charIDToTypeID( 'ItmI' ));
ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( 'ItmI' )));
}
return selectedLayers;
}
function makeActiveByIndex( idx, visible ){
if( idx.constructor != Array ) idx = [ idx ];
for( var i = 0; i < idx.length; i++ ){
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putIndex(charIDToTypeID( 'Lyr ' ), idx[i])
desc.putReference( charIDToTypeID( 'null' ), ref );
if( i > 0 ) {
var idselectionModifier = stringIDToTypeID( 'selectionModifier' );
var idselectionModifierType = stringIDToTypeID( 'selectionModifierType' );
var idaddToSelection = stringIDToTypeID( 'addToSelection' );
desc.putEnumerated( idselectionModifier, idselectionModifierType, idaddToSelection );
}
desc.putBoolean( charIDToTypeID( 'MkVs' ), visible );
executeAction( charIDToTypeID( 'slct' ), desc, DialogModes.NO );
}
}
Hi,
I copy'n'pasted your script into Text Edit and saved as .jsx, saved that into PS4 > Presets > Scripts and tried it but it didn't work.
I selected the layer I wanted to remove 'copy' from but got the following error.
Any possible solutions? The initial script at the top of this thread worked, but I have too many layers to work through to use it effectively.
Any help appreciated.
--cheers, matt
I am not sure why it is not woking for you unless Text Edit did not save it as plain text.
That script is one of two that I have in a Configurator panel I made. One button removes 'copy' from all layers and the other button removes from selected layers. You can get the panel at http://ps-scripts.com/bb/viewtopic.php?f=10&t=3056
North America
Europe, Middle East and Africa
Asia Pacific