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

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

New Here ,
Dec 06, 2011 Dec 06, 2011

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.

TOPICS
Scripting
9.6K
Translate
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 1 Correct answer

Community Expert , Dec 06, 2011 Dec 06, 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 = "";

Translate
Adobe
Community Expert ,
Dec 06, 2011 Dec 06, 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 = "";

Translate
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
New Here ,
Dec 07, 2011 Dec 07, 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.

Translate
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 ,
Dec 07, 2011 Dec 07, 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 = "";

Translate
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
New Here ,
Dec 07, 2011 Dec 07, 2011

Much better - thank you!  (my file has more like 10k items).

I tried something similar but with pageItems, and couldn't make it work.  I didn't realize you didn't need pageItems.

Translate
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 Beginner ,
Jan 04, 2014 Jan 04, 2014

Hi guys,

I'm new here on forum. I have a problem with naming a large number of objects (not layers).

My object names curently have this format:

Section:100_a

Section:100_b

Section:201_21

etc..

It is possible with a script to replace the "_" with a space and a new word so that the objects will be named as:

Section:100 Subsection:a

Section:100 Subsection:b

Section:201 Subsection:21

The script can be set up to work with only selected objects or all objects within document (whatever is easy).

Thank you in advance,

Adrian

Translate
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 ,
Jan 04, 2014 Jan 04, 2014

Hi Adrian, yes, sure

...Considering you want to rename any kind of pageItems (paths, groups, texts and so on).. an example would be:

var doc = app.activeDocument;

var items = doc.pageItems;

for (var g=0;g<items.length;g++){

    items.name = items.name.replace ("_", " Subsection ");

};

app.redraw();

If you want to rename just a kind of object (like paths or groups only) change what "items" variable receive.

P.S: not necessary to select any object. But, after running the script, if the Layers panel do not redraw, try to select and deselect any object....

Let me know if it helps.

Gustavo.

Translate
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 Beginner ,
Jan 04, 2014 Jan 04, 2014

Hello Gustavo,

Thank you, it works great:)

Translate
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
Explorer ,
Jul 29, 2014 Jul 29, 2014

Can you do like a video tutorial on this. I would love you for ever.

Translate
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 Beginner ,
Oct 30, 2015 Oct 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

Translate
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 ,
Oct 30, 2015 Oct 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;
Translate
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 Beginner ,
Oct 31, 2015 Oct 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

Translate
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 ,
Oct 31, 2015 Oct 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.

Translate
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 Beginner ,
Nov 01, 2015 Nov 01, 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.

Translate
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 Beginner ,
Nov 12, 2015 Nov 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

Translate
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
Mentor ,
Nov 12, 2015 Nov 12, 2015
LATEST

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

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

Translate
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