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:
... but now, I'd like to type a letter according to the array number of swatches[i]. For example:
... 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.
Hi Ben, is this what's your trying to achieve?
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);
}
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
Copy link to clipboard
Copied
Hi Ben, is this what's your trying to achieve?
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);
}
Copy link to clipboard
Copied
Brilliant! You're a saint, Carlos. Can't thank you enough.