0

I found a script on here that adds percentage symbols to numbers in illustrator. It works great and I figured out way to auto populate the fields with the needed strings to run the code without having to remember what to fill in. However, you still need to hit enter three times to get it to run. Not a huge deal. But, I am using this script to create dozens of graphs per day, and it is kind of annoying. It would be much easier if I could just have the script run without the need for the prompts. Can somebody please help me out with this? Thank you in advance!

Here is the script:

var scope = app.activeDocument.selection.length ? app.activeDocument.selection : app.activeDocument.pageItems;
var find = prompt("Find: (Text or GREP/regex)","(\\d+\\,?\\d*\\.?\\d*)");
if (find !== null) {
    var replace = prompt("Replace: (Text or GREP/regex)","$1%");
    if (replace !== null) {
        var changes = 0;
        for (var i = 0; i < scope.length; i++) {  
            var text = scope[i];
            var string = text.contents;  
            if (typeof string == "string") {
                var newstring = string.replace(new RegExp(find, 'g'), replace);
                if (newstring != string) {
                    changes++;
                    var paragraphsArray = newstring.split("\n");
                    text.paragraphs.removeAll(); 
                    for (var ii = 0; ii < paragraphsArray.length; ii++) {  
                         text.paragraphs.add(paragraphsArray[ii]);
                    }
                }
            }
        }
        alert(changes == 1 ? "1 text object changed" : changes + " text objects changed");
    }
}
1

Actually it's easily fixed:

var scope = app.activeDocument.selection.length ? app.activeDocument.selection : app.activeDocument.pageItems;
var find = "(\\d+\\,?\\d*\\.?\\d*)";
var replace = "$1%";
var changes = 0;
for (var i = 0; i < scope.length; i++) {  
    var text = scope[i];
    var string = text.contents;  
    if (typeof string == "string") {
        var newstring = string.replace(new RegExp(find, 'g'), replace);
        if (newstring != string) {
            changes++;
            var paragraphsArray = newstring.split("\n");
            text.paragraphs.removeAll(); 
            for (var ii = 0; ii < paragraphsArray.length; ii++) {  
                 text.paragraphs.add(paragraphsArray[ii]);
            }
        }
    }
}

The three prompts come from the two prompt commands and the alert command:

var find = prompt("Find: (Text or GREP/regex)","(\\d+\\,?\\d*\\.?\\d*)");
var replace = prompt("Replace: (Text or GREP/regex)","$1%");
alert(changes == 1 ? "1 text object changed" : changes + " text objects changed");

Instead of prompting you for a value for find and replace, where you just hit Enter to use the default value given in the script, I just set the variables directly:

var find = "(\\d+\\,?\\d*\\.?\\d*)";
var replace = "$1%";

The if statement checking if replace !== null can be removed as we now know that replace always has a value.

The alert command simply tells you what changes were made and can be removed without affecting functionality.

It might be possible to optimize the script further, but I'll stop here as it seems to work.

3
  • Welcome to GDSE! I wanted to help you with this. Not sure if the question will live for long as it's really more about coding than about graphic design. 😀
    – Wolff
    Mar 10 at 18:06
  • Thank you so much! I have been trying to figure this out for so long! You're awesome! Mar 10 at 18:15
  • You're welcome! Remember to accept the answer at some point. In general it's considered polite to wait a little to see if someone else comes up with a better answer.
    – Wolff
    Mar 10 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.