Scripting - Find and Replace

Community Beginner ,
Jun 23, 2021 Jun 23, 2021

Copy link to clipboard

Copied

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.

 

 

TOPICS
Scripting

Views

327

Likes

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 , Jun 23, 2021 Jun 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 ...

Likes

Translate

Translate
Adobe Community Professional ,
Jun 23, 2021 Jun 23, 2021

Copy link to clipboard

Copied

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

Likes

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 ,
Jun 24, 2021 Jun 24, 2021

Copy link to clipboard

Copied

Hello @Charu Rajput, it worked!

Thank you.

Likes

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 ,
Jun 24, 2021 Jun 24, 2021

Copy link to clipboard

Copied

Is it possible to replace 5 words at the same time?

Likes

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 ,
Jun 24, 2021 Jun 24, 2021

Copy link to clipboard

Copied

LATEST

5 different words? like you want to input 5 search terms and 5 replacements and have the script change any of those 5 search terms in the document with the corresponding replacement?

 

Or you have 5 text frames and you want them all to change at the same time? The script is already setup to do this. if you have 100 text frames that contain the text "xxxx" they'll all be updated with "NEW YORK".

 

If you're asking about the first option, consider this updated script. I've move the search/replace terms to be passed into the function. So you can call the function as many times as you want with whatever search/replace terms you want. I've added in some examples that you can test if you want.

 

So it's not exactly "at the same time" as you said, but it'll all happen together under one script execution.

 

// findReplace.jsx

// carlos canto

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

function main(search_string,replace_string) {
    var idoc, tframes, i, tframe, new_string, counter = 0;
    for (var a = 0; a < app.documents.length; a++) {
        idoc = app.documents;
        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(/xxxx/gi,"NEW YORK");
main(/yyyy/gi,"CHICAGO");
main(/zzzz/gi,"LOS ANGELES");
main(/aaaa/gi,"SEATTLE");

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();
    };
}

Likes

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