Script to Batch Rename Layers/Artboards with Search/Replace Function?

Community Beginner ,
Oct 06, 2017 Oct 06, 2017

Copy link to clipboard

Copied

So, I have been spoiled by an After Effects plugin called AE Global Renamer (AEGR) and was wondering if Illustrator had a plug in or script that could do something similar. The beauty of AEGR is that it allows you to search a consistent variable and replace it globally (please check out the example below):

ae_global_renamer_01-before.jpg

I’ve search the threads and the closest thing I’ve found was a cool plugin by Qwertyfly...​ that allows you to batch rename artboards. Though, the script is restricted to artboards (it would be great if it could rename layers) and doesn’t really have a search/replace option (or I'm not using it correctly):

https://forums.adobe.com/message/7625415#7625415

If any of you know of something that can Batch Rename Layers/Artboards with a search/replace function that would be great!

Thanks!
Will

Message was edited by: Will N

TOPICS
Scripting

Views

4.0K

Likes

translate

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 1 Correct Answer

Adobe Community Professional , Sep 03, 2018 Sep 03, 2018
here's a script to rename layer names   // findReplaceLayers.jsx // carlos canto // https://forums.adobe.com/thread/2391868 function main () { var idoc, search_string, replace_string, layers, i, tframe, new_string, counter=0; var idoc = app.activeDocument; var search_string = Window.prompt ('Enter Search string', 'text 1', 'Find/Replace Layer Names'); if (search_string == null) { alert ('Cancelled by user'); return; } var replace_string = Windo...

Likes

translate

Translate

Translate
New Here ,
Sep 03, 2018 Sep 03, 2018

Copy link to clipboard

Copied

I have created a custom script to rename Photoshop layers automatically, you can find it here: http://www.sosfactory.com/batch-rename-photoshop-layers/

Likes

translate

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
Adobe Community Professional ,
Sep 03, 2018 Sep 03, 2018

Copy link to clipboard

Copied

here's a script to rename layer names

 

// findReplaceLayers.jsx
// carlos canto
// https://forums.adobe.com/thread/2391868

function main () {
    var idoc, search_string, replace_string, layers, i, tframe, new_string, counter=0;
    var idoc = app.activeDocument;
    
    var search_string = Window.prompt ('Enter Search string', 'text 1', 'Find/Replace Layer Names');
    
    if (search_string == null) {
        alert ('Cancelled by user');
        return;
    }

    var replace_string = Window.prompt ('Enter Replace string', 'text 2', 'Find/Replace Layer Names');
    
    if (replace_string == null) {
        alert ('Cancelled by user');
        return;
    }
     
    layers = idoc.layers;
     
    for (i = 0 ; i < layers.length; i++) {
        ilayer = layers[i];
        new_string = ilayer.name.replace(search_string, replace_string);

        if (new_string != ilayer.name) {
            ilayer.name = new_string;
            counter++;
        }
    }
    alert(counter + ' changes were made');
}

main();


 

 

[11/27/19 - re uploaded the script after migrating to new forum][Carlos]

Likes

translate

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
New Here ,
Nov 27, 2019 Nov 27, 2019

Copy link to clipboard

Copied

Hi Carlos,
should your script work with CC19? I got an error 1302 ...

Just to be sure I copy-paste your code to text file and saved as .js (tried .jsx as well) and imported via Cmd+F12, if that is corect way. I tried to rename primarily sublayers - grouped two objects "<Group>", but didn't work on regular layers (Layer 1, Layer 2, ...) as well.

Screen Shot 2019-11-27 at 12.44.02.png

Screen Shot 2019-11-27 at 12.43.24.png

Likes

translate

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
New Here ,
Jan 15, 2020 Jan 15, 2020

Copy link to clipboard

Copied

I wsn'table to make your script work in Illustrator.

Likes

translate

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
Adobe Community Professional ,
Nov 27, 2019 Nov 27, 2019

Copy link to clipboard

Copied

Hi mond, the new forum format messed with the script, I just re uploaded it. Please replace your old script. However, this script's purpose is to rename Layers, not pageItems ie Groups

Likes

translate

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
New Here ,
Nov 28, 2019 Nov 28, 2019

Copy link to clipboard

Copied

Thank you for quick response and update. Works 🙂 Sadly for me I ended up with manual renaming of 200 <Groups>. Because I could place Layer under Layer (to create sublayer) I thought -probably script doesn't care if it's layer or ect ... just an item in a list (structure).

BTW Seems to me more versatile (universal) if there would be a way to let script rename already selected any kind of items in layer panel (list) and be able to rename what ever (with what ever name) from the list to some name (instead of search for specific part of text in name only). Also for batch rename action people often look for ability of sequential numbering (ascending/descending).

 

I'm not asking for that, but probably it can touch bigger audience.
Because of my needs  ... I searched cross a net with many old not functional scripts (tried on Layers too) that try to cover batch rename, so I'm quite surprised there doesn't exist any (discoverable) working script or made by Adobe yet ...

Likes

translate

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 Beginner ,
Nov 11, 2020 Nov 11, 2020

Copy link to clipboard

Copied

Hi Carlos,

 

I just tried your script and got this error.

Screen Shot 2020-11-11 at 3.18.32 PM.png

Likes

translate

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
Engaged ,
Nov 11, 2020 Nov 11, 2020

Copy link to clipboard

Copied

Try this one:

// You can change these settings:
var settings = {
    // Should the script guess the name for unnamed items like <Group>, <Path>, etc:
    replaceGenerics: true,
    // Should the script replace "HELLO" when findText is "Hello":
    caseSensitive: false
}
// What to find:
var findText = 'layer'
// 
// What to replace it with:
var replaceText = 'Hello world'

/**
 * 
 * Don't edit below unless you know what you're doing
 * 
 */

Array.prototype.forEach = function (callback) {
    for (var i = 0; i < this.length; i++) callback(this[i], i, this);
};
function get(type, parent) {
    if (arguments.length == 1 || !parent) parent = app.activeDocument;
    var result = [];
    if (!parent[type]) return [];
    for (var i = 0; i < parent[type].length; i++) {
        result.push(parent[type][i]);
        if (parent[type][i][type])
            result = [].concat(result, get(type, parent[type][i]));
    }
    return result || [];
}

function findReplace(find, replace, options) {
    var layers = get('layers');
    var pageItems = get('pageItems');
    var list = [].concat(layers, pageItems);
    var findRX = new RegExp(getEscapedRXString(find), !options.caseSensitive ? 'i' : null)
    list.forEach(function (item) {
        if (item.name.length)
            item.name = item.name.replace(findRX, replace)
        else
            item.name = options.replaceGenerics && findRX.test(item.typename)
                ? item.typename.replace(/item/i, '').replace(findRX, replace)
                : item.name
    })
}

function getEscapedRXString(string) {
    return string.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&');
}

findReplace(findText, replaceText, settings)

It should handle any layer or group of any depth no matter how they're nested. It can't affect "<Rectangle>" or "<Ellipse>" because those are typenamed PageItems but otherwise solves for what every one wanted above.

Likes

translate

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
Adobe Community Professional ,
Nov 11, 2020 Nov 11, 2020

Copy link to clipboard

Copied

Hi robbie, you need to save the file as plain text instead of RTF.

Likes

translate

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