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

Change Text to Specific Colour Swatch Illustrator Javascript

New Here ,
Aug 20, 2024 Aug 20, 2024

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.

Screenshot 2024-08-20 at 5.13.25 pm.png

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

 

TOPICS
Scripting
578
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

Community Expert , Aug 20, 2024 Aug 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
        || textF
...
Translate
Adobe
Community Expert ,
Aug 20, 2024 Aug 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;
    };

})();
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 ,
Aug 20, 2024 Aug 20, 2024

This works fantastically! 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
Community Expert ,
Aug 20, 2024 Aug 20, 2024
LATEST

Great! You're welcome. 

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