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

Write used color swatch names as text on the artboard

Explorer ,
Jan 31, 2011 Jan 31, 2011

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.

TOPICS
Scripting
9.0K
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

Guide , Mar 30, 2011 Mar 30, 2011

#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++) {    

...
Translate
Adobe
Guide ,
Jan 31, 2011 Jan 31, 2011

Are your files already saved as Illustrator '.ai'? or 'pdf'

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
Explorer ,
Jan 31, 2011 Jan 31, 2011

The script would just be ran on the active document (and the active arboard).  They are AI files.

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
Explorer ,
Feb 02, 2011 Feb 02, 2011

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?

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
Guide ,
Feb 03, 2011 Feb 03, 2011

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'

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 ,
Feb 04, 2011 Feb 04, 2011

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.

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
Guide ,
Feb 04, 2011 Feb 04, 2011

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…

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 ,
Feb 04, 2011 Feb 04, 2011

So simple... ... tahnks!

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
Explorer ,
Mar 30, 2011 Mar 30, 2011

Is there anyway to update this script to do the following:

  1. Paste these swatch names into the active text frame, if a text box isn't selected, script would fail to continue.
  2. Exclude/ignore any swatches that exist in the groups called "Fabrics", "Default Elements", or "Universal Fabrics"

Thanks so much!

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
Guide ,
Mar 30, 2011 Mar 30, 2011

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…

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
Explorer ,
Mar 30, 2011 Mar 30, 2011

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();

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
Guide ,
Mar 30, 2011 Mar 30, 2011

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();

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
Explorer ,
Mar 30, 2011 Mar 30, 2011

Yes - exactly.  Just need to dump that info into the selected text frame, one color swatch per line.

Thank you so much!

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
Guide ,
Mar 30, 2011 Mar 30, 2011

#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.name != '[None]'                                                              && grpList.name != '[Registration]') {                                                                        nameList.push(grpList.name);                                                              }                          }                                          }                           }                      sel[0].contents = nameList.join('\r');                      }             } }; swatchNamesToText();

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
Explorer ,
Mar 30, 2011 Mar 30, 2011

Mark - thanks a ton!  This is perfect.

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 Beginner ,
Dec 05, 2017 Dec 05, 2017

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.name != '[BRIGHT PINK - Poly 775]'

&& grpList.name != '[White]'

&& grpList.name != '[Black]')

but it didn't remove those. Any help would be much appreciated.

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 ,
Dec 06, 2017 Dec 06, 2017

Please create a new post and we can help you there. It behooves everyone to keep the topics separate.

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 Beginner ,
Dec 06, 2017 Dec 06, 2017

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

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 ,
Dec 06, 2017 Dec 06, 2017

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

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 ,
Dec 06, 2017 Dec 06, 2017

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.

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
Explorer ,
Jan 09, 2021 Jan 09, 2021

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.

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 ,
Jan 10, 2021 Jan 10, 2021
LATEST

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

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 Beginner ,
Dec 04, 2017 Dec 04, 2017

this is exactly what I need but I have no idea how to use this. Can someone make it into a downloadable script? HELP!

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 ,
Dec 05, 2017 Dec 05, 2017

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.

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 ,
Dec 05, 2017 Dec 05, 2017

Make sure that you save as PLAIN text and not RICH text (.txt not .rtf).

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