Skip to main content
New Participant
October 6, 2017
Answered

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

  • October 6, 2017
  • 4 replies
  • 18779 views

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):

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

This topic has been closed for replies.
Correct answer CarlosCanto

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]

4 replies

New Participant
September 9, 2021

I adapted this script to work for artboards as well as layers by simply doing a 'find and replace' function in text edit to replace 'layers' with 'artboards'.

 

Simply copy and paste the below under the '------------' into a plain text document (.txt rather than the default .rtf) in text edit and save with the suffix '.jsx' so for example: findAndReplaceArtboards.jsx

 

To use for layers do a find and replace in text edit for 'artboards' and replace with 'layers' and select all, and then save with an alternative file name such as findAndReplaceLayers.jsx

 

In Illustrator go to File > Scripts > Other Scripts and navigate to the relevant .jsx document you just saved. You will tehn be prompted to enter the original text that appears on the layers/artboards that you wish to replace, and then you will be prompted to enter the text you wish to replace it with. The script will then run and tell you how many replacements were made.

 

------------

// findReplaceartboards.jsx
// carlos canto
// https://forums.adobe.com/thread/2391868
function main() {
var counter = 0;
var search_string = Window.prompt('Enter Search string', 'text 1', 'Find/Replace artboard Names');
if (search_string == null) {
alert('Cancelled by user');
return;
}
var replace_string = Window.prompt('Enter Replace string', 'text 2', 'Find/Replace artboard Names');
if (replace_string == null) {
alert('Cancelled by user');
return;
}
var replaceName = function (artboard) {
var name = artboard.name.replace(new RegExp(search_string), replace_string);
if (name !== artboard.name) {
artboard.name = name;
counter++;
}
};
var traverseartboards = function (artboards, action) {
if (artboards) {
for (var i = 0, len = artboards.length; i < len; i++) {
traverseartboards(artboards[i].pageItems, action);
action(artboards[i]);
}
}
};
traverseartboards(app.activeDocument.artboards, replaceName);
alert(counter + ' changes were made');
}
main();

Rodrigo225252887pkw
New Participant
January 6, 2022

Hi everibody,

Is there a way to make that script work on Photoshop? (The prompts apeared but nothing changed).

Also, can the script be used to change just part of the artboard's name?

Ex.: I have 30 artboards name like "project9393_1080x1080px", so every new project needs the "9393" part renamed/updated, for all the 30 artboards, a script to do that would be amazing!

CarlosCanto
Brainiac
January 6, 2022

Hi Rodrigo, no, unfortunatelly Illustrator scripts don't work in Photoshop.

CarlosCanto
Brainiac
November 27, 2019

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

robbytrione
New Participant
November 11, 2020

Hi Carlos,

 

I just tried your script and got this error.

Participating Frequently
March 20, 2024

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.


The code works well. Could you check if we can search a first string "AB CD" and replace a whole layer name.

For examples:

Layer: AB CD DEFGH = _D_

Layer: AB CD 4ADF68 = _D_

Layer: AB CD 4AKLDA = _D_

Layer: AB CD 56K89A = _D_
...

CarlosCanto
CarlosCantoCorrect answer
Brainiac
September 3, 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 = 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]

Participating Frequently
November 27, 2019

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.

New Participant
September 3, 2018

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