Skip to main content
Participant
December 7, 2011
Answered

How do I batch-rename objects/paths (not just layers)?

  • December 7, 2011
  • 4 replies
  • 9427 views

Question from a complete n00b:

I need to rename a large number of selected objects/paths. This is because I want to use another script that only works when my objects have the default name, "<Path>". So really, I just need to remove existing names of objects.

This other script is here:

http://kelsocartography.com/blog/?p=325

Of course, I could manually remove names by double-clicking on objects in the layers palette and deleting the name, but that's no fun.

I've found some great scripts for renaming layers, but nothing for non-layer objects.

First, I'm trying to remove names from ALL objects in the document.  once that's working, I want to only remove names of selected objects.

Here's my very simple non-working javascript - (the whole thing):

app.activeDocument.pageItem.name = "";

my test Ai file is also really basic: a few rectangles, some of which have been named in the layers palette.

When I run this, I get:

Error 21: undefined is not and object.

Line: 1

->     app.activeDocument.pageItem.name = "";

I've tried a number of other approaches, and either get "undefined is not an object" or nothing happens. 

pageItem has "name" as a writable property, so I think it's what to use, but I really don't know what I'm doing - help!

I'd also happy to use actions, but I'm stuck on that too.

This topic has been closed for replies.
Correct answer CarlosCanto

->     app.activeDocument.pageItem.name = "";

I've tried a number of other approaches, and either get "undefined is not an object" or nothing happens.

pageItem has "name" as a writable property, so I think it's what to use, but I really don't know what I'm doing - help!

name is a valid property of pageItem, now the script needs to know which pageItem you're refering to, use

app.activeDocument.pageItems[0].name = "";

4 replies

Participating Frequently
November 12, 2015

Hi guys,

Is there a way to write a script that rename the objects names with lowercase?

For example I have objects with following names:

text_A

text_B1

etc.

I need something that rename like this:

text_a

text_b1

Best,

Adrian

Inspiring
November 12, 2015

AdrianDragne wrote:  Is there a way to write a script that rename the objects names with lowercase?

sel.name = sel.name.toLowerCase();

Participating Frequently
October 31, 2015

Hi Carlos,

Many thanks for this.

The script is great but is not working for text elements. For other elements like paths the script works perfectly.

I have text elements like:

A

B

C1

D235

etc..

The script will rename as follow:

text

text

text

text

.....

So the script add the word "text" but delete the initial name.

I think the script consider that the text objects have no names. Some thing when we have, let's say, a circle. Illustrator automatically name this as <Path>. When I run the script the name will be "text" and not "text <Path>" But when I rename the circle from <Path> to other name, the script works.

Not sure how to solve this text naming, because I don't want to rename the text objects.

Best,

Adrian

CarlosCanto
Community Expert
Community Expert
October 31, 2015

what you see in the layers panel is either a "preview" of the text contents for Text Elements, or the type of object "<path>" for example, for other elements. These are added by default until the user names the objects.

...so if you haven't explicitly named your paths or text elements, then all you'll see after running the script is

text

text

text

in your case use this instead

sel.name = "text " + sel.contents;


[edit]

that will only work on text elements, it will raise an error if you select path items or other items.

Participating Frequently
November 1, 2015

Thank you Carlos, I only need text elements to rename. So the script is working like magic

Once again thank you for your quick replay.

Participating Frequently
October 30, 2015

Hello again,

Guys I need a small script. I'll be forever grateful if you can help me out.

I want to add a prefix to my object names.

So currently I have the following format:

ExampleName1

ExampleName2

etc...

It needs to be like this:

text ExampleName1

text ExampleName2

So basically to add "text" and a space before the current name.

Thank you in advance!

Adrian

CarlosCanto
Community Expert
Community Expert
October 30, 2015

same as original example, almost the same, select your items before running the script

  1. var docRef = activeDocument; 
  2. var sel = docRef.selection; 
  3. for (var i=0; i < sel.length; i++) 
  4.      sel.name = "text " + sel.name;
CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
December 7, 2011

->     app.activeDocument.pageItem.name = "";

I've tried a number of other approaches, and either get "undefined is not an object" or nothing happens.

pageItem has "name" as a writable property, so I think it's what to use, but I really don't know what I'm doing - help!

name is a valid property of pageItem, now the script needs to know which pageItem you're refering to, use

app.activeDocument.pageItems[0].name = "";

dfcooke1Author
Participant
December 7, 2011

Thanks!!

Here's my final script:

////START SCRIPT////

//how to use: select objects that you want to rename, then run script

//note - new name may not display until you unselect objects


var docRef = activeDocument;


for (var i=0; i < docRef.pageItems.length; i++)

{

       if (docRef.pageItems.selected == true)

       {

               docRef.pageItems.name = "";

       }

}

////END SCRIPT////

currently, it renames with no text.  In other words, it removes existing names.

One thing that was messing me up was that new names are not immediately displayed - you have to unselect your selected objects before names in the layers palette refresh.

CarlosCanto
Community Expert
Community Expert
December 7, 2011

not bad for starters, now what if we have 1000 items and we select 10 to rename? not very efficient

try looping thru the selection instead of all pageItems

var docRef = activeDocument;

var sel = docRef.selection;

for (var i=0; i < sel.length; i++)

     sel.name = "";