Copy link to clipboard
Copied
Anyone know if it is possible to write a script that will take all of your "used swatches" and write a list of their swatch names on the artboard? Would be very useful for art approval documents.
#target illustrator function swatchNamesToText() { if (app.documents.length = 0) { return; } else { var docRef = app.activeDocument; var sel = docRef.selection; if (sel.length == 1 && sel[0].typename == 'TextFrame') { var nameList = Array(); var swatGrps = docRef.swatchGroups; for (var i = 0; i < swatGrps.length; i++) {
...Copy link to clipboard
Copied
Are your files already saved as Illustrator '.ai'? or 'pdf'
Copy link to clipboard
Copied
The script would just be ran on the active document (and the active arboard). They are AI files.
Copy link to clipboard
Copied
Anyone know if this is possible...doesn't have to be "used swatches" I suppose since I can just remove unused swatches prior to running the script. Is it possible to have javascript write the swatch names as text in a certain the active artboard?
Copy link to clipboard
Copied
Yes this is very doable… There is an 'Action' for removing unused palette items (although I find I need to run twice?). You can't run this action from JavaScript but you can from VB & AS. If your files are ones that are already saved then you could read the colours from the file before opening it.
This should write you a list of swatches down the left half of an open doc… Its just default text for now… Remember that 'swatches' are an overall group and include patterns, spots, gradients etc.
#target illustrator function usedSwatches() { if (app.documents.length == 0) { return; } else { var docRef = app.activeDocument; var docW = docRef.width; var docH = docRef.height; var sL = docRef.swatches; var text = ''; for (var i = 0; i < sL.length; i++) { if (sL.name != '[None]' && sL.name != '[Registration]') { text += sL.name + '\r'; } } var aP = docRef.pathItems.rectangle(docH,0,docW/2,docH,true); var tF = docRef.textFrames.areaText(aP); tF.contents = text; } } usedSwatches();
As is it does NOT list 'None' & 'Registration'
Copy link to clipboard
Copied
Mark,
that's very interesting for me:
If your files are ones that are already saved then you could read the colours from the file before opening it.
Can you share your knowledge how I can read the colours from the saved file (PDF or AI - doesn't matter) please?
Thanks a lot.
Copy link to clipboard
Copied
Illustrator writes somewhere around 30 lines of 'comments' into the files it saves. There are several strings in that data that I like to make use of it helps me in deciding which files I should even open from a given list. Data such as Mode, used CMYK seps and used Spot Inks. How you parse that data is down to you but heres an example of getting the colour Mode:
var f = new File('~/Desktop/SomeFile.ai'); alert(aiMode(f)); function aiMode(f) { var mode = null; f.open('r'); do { var line = f.readln(); } while (!/^%AI\d_ColorModel: [1-2]/.test(line)); var m = line.match(/\d$/g); m == 1 ? mode = 'RGB' : mode = 'CMYK'; f.close(); return mode; }
This should give you which CMYK seps will be used:
var f = new File('~/Desktop/CMYK_Doc.ai'); alert(aiCMYK(f)); function aiCMYK(f) { var cmyk = null; f.open('r'); do { var line = f.readln(); } while (!/^%%DocumentProcessColors:/.test(line)); var process = line.replace('%%DocumentProcessColors:',''); // This expression is for english words var cmyk = process.match(/\w{4,7}/g); f.close(); return cmyk; }
This should give you which Spots are used (in the document NOT in swatches palette ) Its a bit of untidy code but works…
var aiGraphic = new File('~/Desktop/CMYK_Doc_PMS.ai'); var aiSpots = graphicSpots(aiGraphic); alert(arrayToString(aiSpots,'\r')); function graphicSpots(f) { var colorList = Array(); f.open('r'); while (!f.eof) { var line = f.readln(); if (/%%DocumentCustomColors:/.test(line)) { colorList.push(line.substring(25, line.length-1)); var nextLine = f.readln(); while (!/^%%CMYKCustomColor:/.test(nextLine)) { colorList.push(nextLine.substring(5, nextLine.length-1)); nextLine = f.readln(); } } } f.close(); return colorList; } function arrayToString(arr,d) { return arr.join(d); }
Open an AI file in your text editor and have a poke about…
Copy link to clipboard
Copied
So simple... ... tahnks!
Copy link to clipboard
Copied
Is there anyway to update this script to do the following:
Thanks so much!
Copy link to clipboard
Copied
I would need to know more about your colors and what you wanted to do with them. Putting text in some frame is easy enough…
Copy link to clipboard
Copied
Yes - that is basically it. I just need to throw the text of all the swatch names (regardless of spot/cmyk/rgb) that aren't in the groups listed above (and of course the registration swatch), into the current text frame. If a text frame isn't selected prior to running the script, the script should stop. Here is the script:
#target illustrator
function usedSwatches() {
if (app.documents.length == 0) {
return;
} else {
var docRef = app.activeDocument;
var docW = docRef.width;
var docH = docRef.height;
var sL = docRef.swatches;
var text = '';
for (var i = 0; i < sL.length; i++) {
if (sL.name != '[None]' && sL.name != '[Registration]') {
text += sL.name + '\r';
}
}
var aP = docRef.pathItems.rectangle(docH,0,docW/2,docH,true);
var tF = docRef.textFrames.areaText(aP);
tF.contents = text;
}
}
usedSwatches();
Copy link to clipboard
Copied
Tim give this a test and see if what's listed in the dialog is what you want…?
(All the swatch names outside the listed groups excluding the default 2)…
#target illustrator function swatchNamesToText() { if (app.documents.length = 0) { return; } else { var docRef = app.activeDocument; var nameList = Array(); var swatGrps = docRef.swatchGroups; for (var i = 0; i < swatGrps.length; i++) { if (swatGrps.name != 'Fabrics' && swatGrps.name != 'Default Elements' && swatGrps.name != 'Universal Fabrics') { var grpList = swatGrps.getAllSwatches(); for (var j = 0; j < grpList.length; j++) { if (grpList
.name != '[None]' && grpList .name != '[Registration]') { nameList.push(grpList .name); } } } } alert(nameList.join('\r')); } }; swatchNamesToText();
Copy link to clipboard
Copied
Yes - exactly. Just need to dump that info into the selected text frame, one color swatch per line.
Thank you so much!
Copy link to clipboard
Copied
#target illustrator
function swatchNamesToText() {
if (app.documents.length = 0) {
return;
} else {
var docRef = app.activeDocument;
var sel = docRef.selection;
if (sel.length == 1 && sel[0].typename == 'TextFrame') {
var nameList = Array();
var swatGrps = docRef.swatchGroups;
for (var i = 0; i < swatGrps.length; i++) {
if (swatGrps.name != 'Fabrics'
&& swatGrps.name != 'Default Elements'
&& swatGrps.name != 'Universal Fabrics') {
var grpList = swatGrps.getAllSwatches();
for (var j = 0; j < grpList.length; j++) {
if (grpList
Copy link to clipboard
Copied
Mark - thanks a ton! This is perfect.
Copy link to clipboard
Copied
I'm sorry to post here again, but I managed to get it to work for me but I need it to also remove a few other colors so...
I added in
&& grpList
&& grpList
&& grpList
but it didn't remove those. Any help would be much appreciated.
Copy link to clipboard
Copied
Please create a new post and we can help you there. It behooves everyone to keep the topics separate.
Copy link to clipboard
Copied
yes, i know. i did that but i just thought id give it a shot to get some help here, sorry ill take my questions elsewhere
Copy link to clipboard
Copied
Hi Jessica, please don't take it wrong. It's just useful to users to find answers if topics are within their own threads. It's also helpful for us to provide help, if we see a post marked with a correct answer, we most likely move to another thread that has not been answered. There are just too many questions and volunteers are here a few minutes at a time trying to help people in need.
thanks
Carlos
Copy link to clipboard
Copied
Sorry Jessica,
I certainly didn't intend to belittle or chastise. It's just like carlos said, it helps keep things organized, that's all. I'm glad to help, i just wanted to keep it all together in one location.
Copy link to clipboard
Copied
Hi
None of the scripts here work for me.
All I have to do is have the names of the colors displayed on the artboard.
Copy link to clipboard
Copied
the old forum was moved to a new platform breaking all scripts in the process.
which script are you trying to use? I'll fix it for you
Copy link to clipboard
Copied
this is exactly what I need but I have no idea how to use this. Can someone make it into a downloadable script? HELP!
Copy link to clipboard
Copied
copy that code. paste into a text editor. save the file as [name of file].jsx
then open illustrator. go to File > Scripts > Other Script...
Navigate to the location where you saved the script and double click the file.
Copy link to clipboard
Copied
Make sure that you save as PLAIN text and not RICH text (.txt not .rtf).