Skip to main content
Participating Frequently
December 6, 2017
Answered

Write used color swatch names as text on the artboard

  • December 6, 2017
  • 3 replies
  • 1245 views

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 has been closed for replies.
Correct answer CarlosCanto

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

3 replies

Known Participant
July 2, 2024

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

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

 

Known Participant
July 2, 2024

Figured it out 

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

 

Disposition_Dev
Legend
December 6, 2017

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.

Participating Frequently
December 7, 2017

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.

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
December 7, 2017

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

pixxxelschubser
Community Expert
Community Expert
December 6, 2017

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