• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to prevent " copy" being added? (revisit)

Advocate ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

I often have a situation where I want layers copied into a target folder. These are often just a handful to a dozen, but I'd still prefer the fastest way to NOT end up with " copy" added to them (I would consider the simplest way too).

The demo below results in this for me (v23.3.1) ...

Photoshop_Wr7Om8kk56.jpg

An older topic gave a function for this, but I can't get this to work... Can you?

That optionally leaves me to loop through the target group to correct the layer names, or somehow do this at the time of duplication — all involving precious microseconds... 😉

 

Another hint that r-bin suggested in the other topic, I don't know how to apply to my code...

https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-copy-group-of-layers-withou...

 

 

var doc = app.activeDocument;

//--> setup for demo purposes only
var sourceFolder = doc.layerSets.add();
doc.activeLayer.name = "[SOURCE]";

var targetFolder = doc.layerSets.add();
doc.activeLayer.name = "[TARGET]";

var srcLayer = doc.artLayers.add(); 
srcLayer.name = "source layer"; 
doc.activeLayer.move(sourceFolder, ElementPlacement.INSIDE);
// <--

//  Changes the state in Layer Panel options, but script does NOT respect it
setAddCopyToLayerNames(false);

//  How to prevent " copy" being added?
doc.activeLayer.duplicate(targetFolder, ElementPlacement.INSIDE);

//  https://community.adobe.com/t5/photoshop-ecosystem-discussions/script-to-copy-group-of-layers-without-adding-word-quot-copy-quot/m-p/9549351#M135193
function setAddCopyToLayerNames(add){
	var desc = new ActionDescriptor();
	var ref = new ActionReference();
	ref.putProperty( charIDToTypeID( "Prpr" ), stringIDToTypeID( "addCopyToLayerNames" ) );
	ref.putEnumerated( charIDToTypeID( "capp" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
	desc.putReference( charIDToTypeID( "null" ), ref );
	desc.putBoolean(stringIDToTypeID( "addCopyToLayerNames" ), add );
	executeAction( charIDToTypeID( "setd" ), desc, DialogModes.NO );
}

 

 

 

 

 

TOPICS
Actions and scripting

Views

283

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 3 Correct answers

Community Expert , Aug 09, 2022 Aug 09, 2022

OK, even better:

 

var doc = app.activeDocument;

//--> setup for demo purposes only
var sourceFolder = doc.layerSets.add();
doc.activeLayer.name = "[SOURCE]";

var targetFolder = doc.layerSets.add();
doc.activeLayer.name = "[TARGET]";

var srcLayer = doc.artLayers.add(); 
srcLayer.name = "source layer"; 
doc.activeLayer.move(sourceFolder, ElementPlacement.INSIDE);
// <--

/***** NEW CODE *****/
var idcopyToLayer = stringIDToTypeID( "copyToLayer" );
executeAction( idcopyToLayer, undefined, Dialo
...

Votes

Translate

Translate
Advocate , Aug 09, 2022 Aug 09, 2022

I don't really get what's happening and it doesn't work in my actual code.

It does work here in the example, so I might give it a second look one day for speed reasons — thanks! I'm also surprised move leaves the layer in place in your code (which is what I need).

For now, I've added the loop method...

var a = doc.activeLayer;  //  ([TARGET] group has to be active)
for(var i = a.layers.length - 1; i >= 0; i--) {
    a.layers[i].name = a.layers[i].name.replace(/ copy$/g, '');	// DEL " copy" at the b
...

Votes

Translate

Translate
Enthusiast , Aug 10, 2022 Aug 10, 2022

Rename the layer when it's copied. Your code...

doc.activeLayer.move(sourceFolder, ElementPlacement.INSIDE);

That returns the layer object moved. Capture that object in a variable and change its name property.

layer = doc.activeLayer.move(sourceFolder, ElementPlacement.INSIDE);
layer.name = "Name it whatever you want";

If just to remove "copy" use regex as you mention in later posts.

 

Votes

Translate

Translate
Adobe
Community Expert ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

@Signfeld – Layer panel options:

 

panel-options.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

I always have that off.  Seems my scripting engine doesn't look at it?

Signfeld_0-1659997314030.jpeg

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 08, 2022 Aug 08, 2022

Copy link to clipboard

Copied

I've checkmarked Add "copy" to see what would happen.

The setAddCopyToLayerNames(false) function does turn that option off, but the scripting engine doesn't respect it...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 09, 2022 Aug 09, 2022

Copy link to clipboard

Copied

You may need select the target group's first layer, then rename:

 

activeDocument.activeLayer = activeDocument.layerSets.getByName('[TARGET]').layers[0];
activeDocument.activeLayer.name = activeDocument.activeLayer.name.replace(/ copy$/i, '');

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 09, 2022 Aug 09, 2022

Copy link to clipboard

Copied

OK, even better:

 

var doc = app.activeDocument;

//--> setup for demo purposes only
var sourceFolder = doc.layerSets.add();
doc.activeLayer.name = "[SOURCE]";

var targetFolder = doc.layerSets.add();
doc.activeLayer.name = "[TARGET]";

var srcLayer = doc.artLayers.add(); 
srcLayer.name = "source layer"; 
doc.activeLayer.move(sourceFolder, ElementPlacement.INSIDE);
// <--

/***** NEW CODE *****/
var idcopyToLayer = stringIDToTypeID( "copyToLayer" );
executeAction( idcopyToLayer, undefined, DialogModes.NO );
doc.activeLayer.move(targetFolder, ElementPlacement.INSIDE);
/********************/

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 09, 2022 Aug 09, 2022

Copy link to clipboard

Copied

I don't really get what's happening and it doesn't work in my actual code.

It does work here in the example, so I might give it a second look one day for speed reasons — thanks! I'm also surprised move leaves the layer in place in your code (which is what I need).

For now, I've added the loop method...

var a = doc.activeLayer;  //  ([TARGET] group has to be active)
for(var i = a.layers.length - 1; i >= 0; i--) {
    a.layers[i].name = a.layers[i].name.replace(/ copy$/g, '');	// DEL " copy" at the back only
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Aug 10, 2022 Aug 10, 2022

Copy link to clipboard

Copied

Rename the layer when it's copied. Your code...

doc.activeLayer.move(sourceFolder, ElementPlacement.INSIDE);

That returns the layer object moved. Capture that object in a variable and change its name property.

layer = doc.activeLayer.move(sourceFolder, ElementPlacement.INSIDE);
layer.name = "Name it whatever you want";

If just to remove "copy" use regex as you mention in later posts.

 

William Campbell

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 10, 2022 Aug 10, 2022

Copy link to clipboard

Copied

I think your "sourceFolder" should be "targetFolder".

-

"Move" is not my code. I used "duplicate" cos I need the copied layer to stay in place as well (and "move" moved it). It might receive other properties and need to be copied again.

-

I tried replacing my 

 

doc.activeLayer.duplicate(targetFolder, ElementPlacement.INSIDE);

 

with

 

var layer = doc.activeLayer.duplicate(targetFolder, ElementPlacement.INSIDE);
layer.name = layer.name.replace(/ copy$/g, '');

 

That does work well and seems a best reply to what I was looking for — thanks! 🙂

That leaves me to figure out what is the fastest way, this or the loop...

I also don't know when I don't have to use var (which you left off)...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Aug 10, 2022 Aug 10, 2022

Copy link to clipboard

Copied

LATEST

Ah, yes, you discoverd a flaw in ExendScipt that some of us bang our head against. Some methods return an object and some do not. The "duplicate" method does not return an object, which it certainly should, the same that "move" does. But it does not. That's Adobe. So we have to make all sort of work-a-rounds to get there. In which case, your work-around to rename the layers when done is probably the the most elegant of all, considering what we're dealing with. Don't worry so much about micro-sceonds.

 

William Campbell

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines