Skip to main content
Participating Frequently
Dec 6, 2017 1:41 AM UTC
Answered

Write used color swatch names as text on the artboard

Below is a a script that I found in hopes that it would be able to take all of the "used swatches" in the file and write a list of their swatch names in a text box. I work in the Sublimation apparel industry and it would be very useful for art approval documents. The script I found below does not work. I have little to no knowledge on how to edit or customize it to work the way I'd like it to.

 

Any advice, editing, creating of the script so I can download it... would be much appreciated.

 

Thanks in advance!

 

// https://community.adobe.com/t5/illustrator-discussions/write-used-color-swatch-names-as-text-on-the-artboard/m-p/9487666#M73128

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[i].name != '[None]' && sL[i].name != '[Registration]') {

        text += sL[i].name + '\r';

      }

    }

    var aP = docRef.pathItems.rectangle(docH, 0, docW / 2, docH, true);

    var tF = docRef.textFrames.areaText(aP);

    tF.contents = text;

  }

}

usedSwatches();
This topic is closed to new replies. Start a new post to keep the conversation going.
CarlosCanto
CarlosCantoCorrect answer
Community Expert
Dec 7, 2017 7:24 AM UTC

Swatch names don't need to be enclosed between brackets [ ], except for [None] and [Registration]. Write your swatch names as 'White', 'Black', etc

8 replies

Known Participant
Jul 2, 2024 8:15 PM UTC

Is there a way to edit the below to make the name list stacked instead of on one line? 

sel[0].contents = nameList.join(', ');

 

Translate
Known Participant
Jul 2, 2024 8:23 PM UTC

Figured it out 

sel[0].contents = nameList.join('\n ');

 

Translate
Disposition_Dev
Legend
Dec 6, 2017 4:40 PM UTC

When you run the script, do you get any error messages? What is the result of the script? Is it simply printing the wrong swatches into the text frame? Is a text frame being created?

Can you share an example file so we know exactly how to navigate the file and give the desired result?

The more information you can share about what your needs are, the better we can help.

Translate
Participating Frequently
Dec 7, 2017 12:51 AM UTC

Below is the file. For example, this file has a wrestling singlet in it that uses customer colors. The script that I posted above I ended up getting to work but I want to modify it so that it doesn't list 'Bright Pink', 'White' and 'Black' too.

 

 

The script already is made to exclude '[Registration]' and 'None' so I tried to copy that for 'BRIGHT PINK', 'White' and 'Black' but it didn't work. As you can see it is still listing them.  Below is the exact script I have in there now...

 

// https://community.adobe.com/t5/illustrator-discussions/write-used-color-swatch-names-as-text-on-the-artboard/m-p/9487666#M73128

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

        var grpList = swatGrps[i].getAllSwatches();

        for (var j = 0; j < grpList.length; j++) {

          if (grpList[j].name != '[None]'

            &&
            grpList[j].name != '[Registration]'

            &&
            grpList[j].name != 'BRIGHT PINK - Poly 775' &&
            grpList[j].name != 'White' &&
            grpList[j].name != 'Black') {

            nameList.push(grpList[j].name);

          }

        }

      }

    }

    sel[0].contents = nameList.join(', ');

  }

}

swatchNamesToText();

 

Does it look like I wrote anything wrong or do I need to add something else?

 

Thank you in advance for the help.

Translate
CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
Dec 7, 2017 7:24 AM UTC

Swatch names don't need to be enclosed between brackets [ ], except for [None] and [Registration]. Write your swatch names as 'White', 'Black', etc

Translate
pixxxelschubser
Community Expert
Community Expert
Dec 6, 2017 12:35 PM UTC

This script only shows the current available swatches in the active document.

Translate