• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Illustrator script to write sequential letters based on array

New Here ,
Apr 27, 2020 Apr 27, 2020

Copy link to clipboard

Copied

Hello everyone,

 

I am having difficulty piecing together a script that will allow me to type out a sequence of letters (A, B, C, etc) based on an array gotten from a for loop. Here's a snippet of the code scaled back for easier reading.

#target Illustrator
var doc = activeDocument;
var swatches = doc.swatches;
var textSize = 10;

for(var i=2, len=swatches.length; i<len; i++) {
    var swatchGroup = doc.groupItems.add();
    swatchGroup.name = swatches[i].name;
    var sLen = swatches[i].length;
   
    
    textRef = doc.textFrames.add();
    textRef.contents = nextChar(sLen);
    textRef.textRange.size = textSize;
}


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

I'm assuming that textRef.contents = nextChar(sLen); 'sLen' needs to be a letter, but I can't seem to change swatches[i].length into a letter. 

 

Essentially, I've succeeded in:

  1. cycling through the swatches
  2. drawing a rectangle for each one present
  3. spacing them one next to another
  4. creating a textRef in which to place text that aligns with the rectangle

... but now, I'd like to type a letter according to the array number of swatches[i]. For example:

  • swatches[0] = A
  • swatches[1] = B
  • swatches[3] = C

... and so on. The number of swatches will never go above 12 or so.

 

Any help will be greatly appreciated. This one is really griding me down.

 

Cheers.

TOPICS
Scripting

Views

1.2K

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

Community Expert , Apr 27, 2020 Apr 27, 2020

Hi Ben, is this what's your trying to achieve?

addLetterPerSwatch.PNG

 

var doc = activeDocument;
var swatches = doc.swatches;
var textSize = 10;

// ASCII 65 = A

for(var i=2, charCode=65, len=swatches.length; i<len; i++, charCode++) {
    var swatchGroup = doc.groupItems.add();
    swatchGroup.name = swatches[i].name;
   
    
    textRef = doc.textFrames.add();
    textRef.contents = nextChar(charCode);
    textRef.textRange.size = textSize;
}


function nextChar(c) {
    return String.fromCharCode(c);
}

Votes

Translate

Translate
Adobe
Community Expert ,
Apr 27, 2020 Apr 27, 2020

Copy link to clipboard

Copied

Hello Ben,
Could you please what you are trying to achieve. If you can share the screenshot or file of what you want exactly, people can help you easily.

 

Thanks

Charu

 

Best regards

Votes

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 Expert ,
Apr 27, 2020 Apr 27, 2020

Copy link to clipboard

Copied

Hi Ben, is this what's your trying to achieve?

addLetterPerSwatch.PNG

 

var doc = activeDocument;
var swatches = doc.swatches;
var textSize = 10;

// ASCII 65 = A

for(var i=2, charCode=65, len=swatches.length; i<len; i++, charCode++) {
    var swatchGroup = doc.groupItems.add();
    swatchGroup.name = swatches[i].name;
   
    
    textRef = doc.textFrames.add();
    textRef.contents = nextChar(charCode);
    textRef.textRange.size = textSize;
}


function nextChar(c) {
    return String.fromCharCode(c);
}

Votes

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
New Here ,
Apr 28, 2020 Apr 28, 2020

Copy link to clipboard

Copied

LATEST

Brilliant! You're a saint, Carlos. Can't thank you enough.

Votes

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