Skip to main content
Participant
January 4, 2021
Answered

Anyone can aid me putting together a script for inserting numbers like the video in the description?

  • January 4, 2021
  • 5 replies
  • 2750 views

I'm a designer looking to create an illustrator script similar to the video link below which I'm intending to use as a way of numbering pieces that will be later referenced in an assembly manual. I'm hoping if someone can either guide me or help me write a script as I don't have any experience doing so.

Similar to the script used in the video I would like it to have a functinality of being able to specify a prefix, then a number, and a suffix with each mouse click it places the text with those number & incremental increase in the number followed by additional clicks. (ex. A1b, A2b, A3b, A4c, and so on...)

 

 I've tried contacting the original uploader of the video through their blog but no response from them so

any guidance is greatly appreciated!

https://vimeo.com/34803163

This topic has been closed for replies.
Correct answer m1b

Hi rcraighead, thank you for the updated script. If it's not too much work, is it possible me to ask for last request? Is there a way for this to work with both upper case or lowercase suffix and prefix?

For example

  • if the imput was "a4e" the output would be "a5e"
  • if the imput was "A4e" the output would be "A5e"
  • if the imput was "a4E" the output would be "a5E"
  • if the imput was "A4E" the output would be "A5E"

and in addition if there could be a option for the script to still work even with a "-" seperator between the prefix and the number.

For example

  • if the imput was "A-4e" the output would be "A-5e"

Hopefully these aren't too much of a ask. Thank you again in advance!


Here's my take:

 

 

incrementNumberInTextFrame(selection[0]);

function incrementNumberInTextFrame(item) {
    if (item != undefined && item.typename == "TextFrame") {
        var copyOfItem = item.duplicate();
        copyOfItem.translate(0, -item.height);
        var regex = /\d+/;
        var numberString = item.contents.match(regex)[0];
        copyOfItem.contents = copyOfItem.contents.replace(regex, Number(numberString) + 1);
        selection = [copyOfItem];
        redraw();
    }
}

 

 

This will increment the first instance of any number in the textframe given to the function. It shouldn't matter what other characters are in the text frame.

- Mark

 

Edit: adjusted code for better meaningful variable names.

5 replies

CarlosCanto
Community Expert
Community Expert
January 6, 2021

thanks for the demo Ray!! it's awesome. I'm on Windows though.

A quick search gave me a list of similar programs for Win.

https://alternativeto.net/software/keyboard-maestro/?platform=windows#:~:text=Keyboard%20Maestro%20is%20not%20available,both%20free%20and%20Open%20Source.

rcraighead
Legend
January 6, 2021

Thanks Carlos. JavaScript and Keyboard Maestro have rather consumed me for the last couple years. By the time I feel comfortable with them it'll be time to retire. 🙂

CarlosCanto
Community Expert
Community Expert
January 9, 2021

LOL, yeah the more you learn the more you get sucked in 🙂

pixxxelschubser
Community Expert
Community Expert
January 6, 2021

The used file in the first post is an *.EXE file. (Windows only file type)

I'm also not sure how it works - but it is outside of Illustrator. It is not a Illustrator script and not a plugin.

CarlosCanto
Community Expert
Community Expert
January 5, 2021

I would love to know how it's made, it's not a plugin.

 

one way it could work with scripting is by adding or duplicating placeholder text frames first. Then a script could run an update them.

rcraighead
Legend
January 5, 2021

I'm sure it could be replicated using Keyboard Maestro. But no time to try it. 🙂

rcraighead
Legend
January 8, 2021

Hi rcraighead, thank you for the updated script. If it's not too much work, is it possible me to ask for last request? Is there a way for this to work with both upper case or lowercase suffix and prefix?

For example

  • if the imput was "a4e" the output would be "a5e"
  • if the imput was "A4e" the output would be "A5e"
  • if the imput was "a4E" the output would be "a5E"
  • if the imput was "A4E" the output would be "A5E"

and in addition if there could be a option for the script to still work even with a "-" seperator between the prefix and the number.

For example

  • if the imput was "A-4e" the output would be "A-5e"

Hopefully these aren't too much of a ask. Thank you again in advance!


Here's a try. I'm sure some of the gurus here will have a good laugh, but it's late and it's the best I could come up with tonight.

makeLabel()

function makeLabel() {
    var aDoc = app.activeDocument;
    var prefix, increment, suffix;
    var sel = selection[0];
    if (sel.typename == "TextFrame") {
        var label = sel.contents;
        var i = sel.contents.length;
        prefix = label.charAt (0);
        dash = /-/.exec(label);
        increment = /\d+/.exec(label);
        if (i > 2){
            suffix = label.charAt (i - 1);
        } 
    }
    if (increment == null) {
        increment = "-";
    } else {
        increment = (increment * 1) + 1;
    }

    function nextChar(c) {
        return String.fromCharCode(c.charCodeAt(0) + 1);
    }
    if (suffix != null && dash != null) {
        label = (prefix + dash + increment + suffix);
    }else if (suffix != null && dash == null) {
        label = (prefix + increment + suffix);
    }else if (suffix == null && dash == null) {
        label = (prefix + increment);
    }
    sel.duplicate();
    redraw();
    aDoc.selection[1].selected = false;
    selection[0].translate(0, (selection[0].height * -1));
    selection[0].contents = label;
}

 

 

m1b
Community Expert
Community Expert
January 5, 2021

Hi kenta0D4D, it looks like the video shows a plug-in, not a plain script. If a plain script solution won't work for your case, you might need to find a developer who can make plug-ins. Sorry, I can't help. It's out of my league! - Mark

rcraighead
Legend
January 5, 2021

I used your request as a learning exercise. The script does not have all the functionality you are looking for (I don't know how to trigger a script directly from a mouse click without using a 3rd-party app). It might help though.

 

/*
by Ray Craighead
Works in Adobe Illustrator CC 2015 (only one tested)
• Type the first label using "PrefixIncrementSuffix" format
• With the label selected rigger the script
    It will duplicate the selection and update the content
• Reposition the new label and trigger the script again, etc, etc,
• Assign a keyboard shortcut to the script for easier use
*/

makeLabel();

function makeLabel() {
    var aDoc = app.activeDocument;
    var prefix, increment, suffix, d;
    var sel = selection[0];

    if (sel.typename == "TextFrame") {
        var label = sel.contents;
        prefix = /\b\w/.exec(label);
        increment = /\d+/.exec(label);
        suffix = /\w\b/.exec(label);
    }
    // prefix = nextChar(prefix.toString());
    increment = (increment * 1) + 1;
    // suffix = nextChar(suffix.toString());

    function nextChar(c) {
        return String.fromCharCode(c.charCodeAt(0) + 1);
    }

    label = (prefix + increment + suffix);
    sel.duplicate();
    redraw();
    aDoc.selection[1].selected = false;
    selection[0].translate(0,(selection[0].height * -1));
    selection[0].contents = label;
}

 

 

 

I realized after creating the video you did not intend for the "prefix" and "suffix" to update sequencially. Those lines have been disabled in the script. It was great to learn how to do that though. 

m1b
Community Expert
Community Expert
January 5, 2021

Hi Ray, Hope it's ok, just for learning myself, I've made a couple of versions of your script to show different uses of regular expressions. Version 2 uses match and version 3 uses replace methods of String. Using the RegExp exec method has advantages better seen when used in a loop I think.

 

 

function makeLabel2() {
    var anItem = selection[0];
    var label;
    if (anItem != undefined && anItem.typename == "TextFrame") {
        label = anItem.duplicate();
        label.translate(0, -selection[0].height);
        // match returns the found strings, in an array
        var prefix = anItem.contents.match(/^[A-Z]/)[0];
        var index = anItem.contents.match(/\d+/)[0];
        var suffix = anItem.contents.match(/[a-z]/)[0];
        label.contents = nextChar(prefix) + (Number(index)+1) + nextChar(suffix);
        selection = [label];
        redraw();
    }
    function nextChar(c) {
        return String.fromCharCode(c.charCodeAt(0) + 1);
    }
}

function makeLabel3() {
    var anItem = selection[0];
    if (anItem != undefined && anItem.typename == "TextFrame") {
        var label = anItem.duplicate();
        label.translate(0, -anItem.height);
        // regex replace method can take a function as 2nd parameter!
        label.contents = anItem.contents.replace(/^([A-Z])(\d+)([a-z])/, function (all, prefix, index, suffix) {
            return nextChar(prefix) + (Number(index) + 1) + nextChar(suffix);
        })
        selection = [label];
        redraw();
    }
    function nextChar(c) {
        return String.fromCharCode(c.charCodeAt(0) + 1);
    }
}

 

 

Thanks for the post. It got me thinking! - Mark

rcraighead
Legend
January 5, 2021

Thanks @m1b . I was hoping someone would share a better idea. That's what I love about this forum. Best place to keep learning about scripting.