Skip to main content
Participant
August 20, 2024
Answered

Change Text to Specific Colour Swatch Illustrator Javascript

  • August 20, 2024
  • 1 reply
  • 621 views

Hi,

 

I have found a script online that takes all of the swatches that are used in an illustrator document and outputs each swatch name on its own line of text.

This works well, however I would like each line of text to also inherit the specific swatch colour.

 

So for example "1_Base_Hilite_70%" I would like this line of text to be coloured with the appropriate spot colour swatch with the same name and so on.

 

Can anyone please let me know what I need to add/amend to the below code to achieve this?

 

I am new to coding so please let me know if this is not the best place to post.

 

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 != 'C=0 M=0 Y=0 K=100' &&
            grpList[j].name != 'Black') {

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

          }

        }

      }

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

  }

}

swatchNamesToText();

 

This topic has been closed for replies.
Correct answer m1b

Hi @Jono36042432dhqz, see if this code gets you on the right track. I've fiddled with a few things I didn't need to—just for the sake of tidiness—and you can go back and alter your existing to code with any new parts you learn if you prefer.

- Mark

(function () {

    if (0 === app.documents.length)
        return alert('Please open a document and try again.');

    var docRef = app.activeDocument;
    var textFrame = docRef.selection[0];

    if (
        undefined == textFrame
        || textFrame.typename !== 'TextFrame'
    )
        return alert('Please select a text frame and try again.');

    var swatchGroups = docRef.swatchGroups;

    for (var i = 0; i < swatchGroups.length; i++) {

        var swatches = swatchGroups[i].getAllSwatches();

        swatchesLoop:
        for (var j = 0; j < swatches.length; j++) {

            switch (swatches[j].name) {

                case '[None]':
                case '[Registration]':
                case 'BRIGHT PINK - Poly 775':
                case 'White':
                case 'C=0 M=0 Y=0 K=100':
                case 'Black':
                    // ignore these
                    continue swatchesLoop;

                default:
                    addSwatchNameToText(textFrame, swatches[j]);
                    break;

            }

        }

    }

    function addSwatchNameToText(text, swatch) {
        var col = text.textRange.characters.add('\r' + swatch.name);
        col.fillColor = swatch.color;
    };

})();

1 reply

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
August 20, 2024

Hi @Jono36042432dhqz, see if this code gets you on the right track. I've fiddled with a few things I didn't need to—just for the sake of tidiness—and you can go back and alter your existing to code with any new parts you learn if you prefer.

- Mark

(function () {

    if (0 === app.documents.length)
        return alert('Please open a document and try again.');

    var docRef = app.activeDocument;
    var textFrame = docRef.selection[0];

    if (
        undefined == textFrame
        || textFrame.typename !== 'TextFrame'
    )
        return alert('Please select a text frame and try again.');

    var swatchGroups = docRef.swatchGroups;

    for (var i = 0; i < swatchGroups.length; i++) {

        var swatches = swatchGroups[i].getAllSwatches();

        swatchesLoop:
        for (var j = 0; j < swatches.length; j++) {

            switch (swatches[j].name) {

                case '[None]':
                case '[Registration]':
                case 'BRIGHT PINK - Poly 775':
                case 'White':
                case 'C=0 M=0 Y=0 K=100':
                case 'Black':
                    // ignore these
                    continue swatchesLoop;

                default:
                    addSwatchNameToText(textFrame, swatches[j]);
                    break;

            }

        }

    }

    function addSwatchNameToText(text, swatch) {
        var col = text.textRange.characters.add('\r' + swatch.name);
        col.fillColor = swatch.color;
    };

})();
Participant
August 20, 2024

This works fantastically! Thank you so much!

m1b
Community Expert
Community Expert
August 21, 2024

Great! You're welcome.