Skip to main content
Participant
July 30, 2021
Answered

Extendscript Add Used Colors / Extract used colors from the command line

  • July 30, 2021
  • 2 replies
  • 1442 views

I'm attempting to programmatically extract colors used in an AI vector file. I see Illustrator has the Add Used Colors menu option on the swatches panel. 

 

I'd either like to recreate this functionality or call it directly from extendscript.

Short of looping through all the pathItems in the document is there a hidden function to call this directly? 

 

Or if there is an alternative to extracting colors used in a vector file outside of extendscript. Been searching for a fews days on this and cannot find any good solutions.

This topic has been closed for replies.
Correct answer Charu Rajput

Hi,

You can do this via actions.

Try following snippet that will add used colors without traversing each path item.

addUsedColors()

function addUsedColors() {
    if (app.documents.length = 0) {
        return;
    }
    var ActionSet = "colors"
    var Action1Name = "add used colors"

    var ActionString = '/version 3\n/name [ 6\n' + Hexit(ActionSet) + '\n]\n/isOpen 0\n/actionCount 1\n/action-1 {\n /name [ 15\n'+ Hexit(Action1Name) + '\n ]\n /keyIndex 0\n /colorIndex 0\n /isOpen 1\n /eventCount 1\n /event-1 {\n /useRulersIn1stQuadrant 0\n /internalName (ai_plugin_swatches)\n /localizedName [ 8\n 5377617463686573\n ]\n /isOpen 0\n /isOn 1\n /hasDialog 0\n /parameterCount 2\n /parameter-1 {\n /key 1835363957\n /showInPalette 4294967295\n /type (enumerated)\n /name [ 15\n 416464205573656420436f6c6f7273\n ]\n /value 9\n }\n /parameter-2 {\n /key 1634495605\n /showInPalette 4294967295\n /type (boolean)\n /value 1\n }\n }\n}';

    createAction(ActionString, ActionSet);
    ActionString = null;
    app.doScript(Action1Name, ActionSet, false);
    app.unloadAction(ActionSet, "");

    function createAction(str, act) {
        var f = new File("~/Desktop" + act + '.aia');
        f.open('w');
        f.write(str);
        f.close();
        app.loadAction(f);
        f.remove();
    }

    function Hexit(str) {
        var hex = '';
        for (var i = 0; i < str.length; i++) {
            hex += '' + str.charCodeAt(i).toString(16);
        }
        return hex;
    }
}

2 replies

Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
July 30, 2021

Hi,

You can do this via actions.

Try following snippet that will add used colors without traversing each path item.

addUsedColors()

function addUsedColors() {
    if (app.documents.length = 0) {
        return;
    }
    var ActionSet = "colors"
    var Action1Name = "add used colors"

    var ActionString = '/version 3\n/name [ 6\n' + Hexit(ActionSet) + '\n]\n/isOpen 0\n/actionCount 1\n/action-1 {\n /name [ 15\n'+ Hexit(Action1Name) + '\n ]\n /keyIndex 0\n /colorIndex 0\n /isOpen 1\n /eventCount 1\n /event-1 {\n /useRulersIn1stQuadrant 0\n /internalName (ai_plugin_swatches)\n /localizedName [ 8\n 5377617463686573\n ]\n /isOpen 0\n /isOn 1\n /hasDialog 0\n /parameterCount 2\n /parameter-1 {\n /key 1835363957\n /showInPalette 4294967295\n /type (enumerated)\n /name [ 15\n 416464205573656420436f6c6f7273\n ]\n /value 9\n }\n /parameter-2 {\n /key 1634495605\n /showInPalette 4294967295\n /type (boolean)\n /value 1\n }\n }\n}';

    createAction(ActionString, ActionSet);
    ActionString = null;
    app.doScript(Action1Name, ActionSet, false);
    app.unloadAction(ActionSet, "");

    function createAction(str, act) {
        var f = new File("~/Desktop" + act + '.aia');
        f.open('w');
        f.write(str);
        f.close();
        app.loadAction(f);
        f.remove();
    }

    function Hexit(str) {
        var hex = '';
        for (var i = 0; i < str.length; i++) {
            hex += '' + str.charCodeAt(i).toString(16);
        }
        return hex;
    }
}
Best regards
Inspiring
June 22, 2022

Hi @Charu Rajput 

I have not the knowledge to write the code myself...
Can you post a snippet for Select Same Fill color ?

Best regards

Inspiring
June 22, 2022

This script fit my needs 🙂

https://community.adobe.com/t5/illustrator-discussions/app-executemenucommand-quot-find-fill-color-menu-item-quot-not-working/td-p/12001930 

#target illustrator
function test(){
	var changeColor = function (doc) {
		var starterColor = doc.selection[0].fillColor;
    doc.selection = null;
    doc.defaultFillColor = starterColor;
    app.executeMenuCommand("Find Fill Color menu item");
	}
	var doc = app.activeDocument;
	changeColor(doc);
};
test();

 

CarlosCanto
Community Expert
Community Expert
July 30, 2021

you can record an Action to Add Used Colors, New Color Group, then play that action in your script.

Participating Frequently
October 25, 2023

Hello, sir. I have recorded an action. How should I play this action in my script?  

Thank you

CarlosCanto
Community Expert
Community Expert
October 25, 2023

check @Charu Rajput script, 

app.doScript(Action1Name, ActionSet, false);