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

Illustrator script to write sequential letters based on array

New Here ,
Apr 27, 2020 Apr 27, 2020

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
1.3K
Translate
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.PNGexpand image

 

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);
}
Translate
Adobe
Community Expert ,
Apr 27, 2020 Apr 27, 2020

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
Translate
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

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

addLetterPerSwatch.PNGexpand image

 

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);
}
Translate
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
LATEST

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

Translate
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