Skip to main content
Participating Frequently
June 23, 2021
Answered

Scripting - Find and Replace

  • June 23, 2021
  • 1 reply
  • 3036 views

Hello all,

So i'm having a problem.

I have to translate a lot of files from Portuguese to French and i want to do a mass find and replace.

I'm looking in the community and found 2 topics talking about this but it isn't working when i try to use it.

I dont have a great knowledge in scripting so i'm asking for someones help.

i found this topic from carlos:

https://community.adobe.com/t5/illustrator/illustrator-find-and-replace/m-p/3495220#M9279

 

I added NEW YORK to my text in illustrator to test the script but I get a error (added image to the topic)

 

All i'm doing is creating a .jsx file and going to file script and click on in. 

Am I doing anything wrong?

 

Thank you all for the support.

 

 

This topic has been closed for replies.
Correct answer Charu Rajput

Hi,

Due to shifting of the code from old forum to the new forum, script gets corrupted. Here is teh updated version of the same script by Carlos.

// findReplace.jsx

// carlos canto

// https://forums.adobe.com/thread/2484457

function main() {
    var idoc, search_string, replace_string, tframes, i, tframe, new_string, counter = 0;
    for (var a = 0; a < app.documents.length; a++) {
        idoc = app.documents;
        search_string = /xxxx/gi; // g for global search, remove i to make a case sensitive search
        replace_string = "NEW YORK";
        tframes = idoc[a].textFrames;
        for (i = 0; i < tframes.length; i++) {
            tframe = tframes[i];
            new_string = tframe.contents.replace(search_string, replace_string);
            if (new_string != tframe.contents) {
                tframe.contents = new_string;
                counter++;
            }
        }
    }
    alert(counter + ' changes were made');
    donate(File($.fileName).displayName);
}

main();

function donate(thisScript) {
    var donateStrPref = 'aiscripts.com/donate/' + thisScript;
    var donated = app.preferences.getStringPreference(donateStrPref);
    if (donated != 'true') {
        var showAgain = showDonateWindow();
        if (showAgain) {
            //alert('checked');
            app.preferences.setStringPreference(donateStrPref, 'true');
        }
    }
}

function showDonateWindow() {
    var win = new Window('dialog', 'https://www.paypal.me/aiscripts');
    var lblDonate = win.add('staticText', undefined, 'Do you find the script valuable?\nDo you use the script commercially?\nPlease consider donating', { multiline: true });
    var grp = win.add('group');
    var btnCancel = grp.add('button', undefined, 'Cancel');
    var btnDonate = grp.add('button', undefined, 'Donate');
    var chk = win.add('checkbox', undefined, "Do not show me this message again");
    btnDonate.onClick = function () {
        openURL('www.paypal.me/aiscripts');
        win.close();
    }

    win.center();
    if (win.show() == 2) {
        //alert("Cancelled");
        return null;
    } else {
        return chk.value;
    }


    function openURL(address) {
        var f = File(Folder.desktop + '/aiOpenURL.url');
        f.open('w');
        f.write('[InternetShortcut]' + '\r' + 'URL=http://' + address + '\r');
        f.close();
        f.execute();
        f.remove();
    };
}

 

To test: 

1. Create textframe in the Illustartor with content XXXX

2. Run the script

3. Result : The old content of the textframes that matched XXXX will get replace with 'NEW YORK'.

 

Let us know if this works for you.

1 reply

Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
June 23, 2021

Hi,

Due to shifting of the code from old forum to the new forum, script gets corrupted. Here is teh updated version of the same script by Carlos.

// findReplace.jsx

// carlos canto

// https://forums.adobe.com/thread/2484457

function main() {
    var idoc, search_string, replace_string, tframes, i, tframe, new_string, counter = 0;
    for (var a = 0; a < app.documents.length; a++) {
        idoc = app.documents;
        search_string = /xxxx/gi; // g for global search, remove i to make a case sensitive search
        replace_string = "NEW YORK";
        tframes = idoc[a].textFrames;
        for (i = 0; i < tframes.length; i++) {
            tframe = tframes[i];
            new_string = tframe.contents.replace(search_string, replace_string);
            if (new_string != tframe.contents) {
                tframe.contents = new_string;
                counter++;
            }
        }
    }
    alert(counter + ' changes were made');
    donate(File($.fileName).displayName);
}

main();

function donate(thisScript) {
    var donateStrPref = 'aiscripts.com/donate/' + thisScript;
    var donated = app.preferences.getStringPreference(donateStrPref);
    if (donated != 'true') {
        var showAgain = showDonateWindow();
        if (showAgain) {
            //alert('checked');
            app.preferences.setStringPreference(donateStrPref, 'true');
        }
    }
}

function showDonateWindow() {
    var win = new Window('dialog', 'https://www.paypal.me/aiscripts');
    var lblDonate = win.add('staticText', undefined, 'Do you find the script valuable?\nDo you use the script commercially?\nPlease consider donating', { multiline: true });
    var grp = win.add('group');
    var btnCancel = grp.add('button', undefined, 'Cancel');
    var btnDonate = grp.add('button', undefined, 'Donate');
    var chk = win.add('checkbox', undefined, "Do not show me this message again");
    btnDonate.onClick = function () {
        openURL('www.paypal.me/aiscripts');
        win.close();
    }

    win.center();
    if (win.show() == 2) {
        //alert("Cancelled");
        return null;
    } else {
        return chk.value;
    }


    function openURL(address) {
        var f = File(Folder.desktop + '/aiOpenURL.url');
        f.open('w');
        f.write('[InternetShortcut]' + '\r' + 'URL=http://' + address + '\r');
        f.close();
        f.execute();
        f.remove();
    };
}

 

To test: 

1. Create textframe in the Illustartor with content XXXX

2. Run the script

3. Result : The old content of the textframes that matched XXXX will get replace with 'NEW YORK'.

 

Let us know if this works for you.

Best regards
Participating Frequently
June 24, 2021

Hello @Charu Rajput, it worked!

Thank you.