Highlighted

Illustrator Script to get the path item used fill color name

Participant ,
Oct 23, 2020

Copy link to clipboard

Copied

Hi guys,

Could any help me to get the illustrator image path item fill color used name through script.

 

Adobe Community Professional
Correct answer by Charu Rajput | Adobe Community Professional

Hi Raghav,

I have merged your code and code shared by @pixxxelschubser  which is written by @Silly-V to get the swatches name from Indesign when ai file is placed in the Indesign documnt.

 

#target indesign
#targetengine "session"

var illustratorImages = [];
var filePath

function ImageColors() {

    var myDoc = app.activeDocument;
    var mypages = myDoc.spreads;
    if (mypages.length > 0) {
        for (var k = 0; k < mypages.length; k++) {
            var myPageItems = mypages[k].allGraphics;
            for (var l = 0; l < myPageItems.length; l++) {
                if (myPageItems[l].parentPage != null) {
                    if (myPageItems[l].itemLayer.visible == true && myPageItems[l].visible == true) {
                        if (myPageItems[l].itemLink.name.indexOf(".ai") != -1) {
                            var bt = new BridgeTalk();
                            bt.target = "illustrator";

                            var myScript = aiScript.toString() + "\r";
                            filePath = myPageItems[l].itemLink.filePath;
                            myScript += "aiScript(\"" + filePath + "\")";
                            bt.body = myScript;



                            bt.onResult = function (resObj) {
                                //get a string sent back from AI
                                alert("Illustrator sent this message back to InDesign. " + resObj.body.toString());
                            }

                            //error handler
                            bt.onError = function (inBT) { alert(inBT.body); };

                            //send to Illustrator
                            bt.send();
                        }
                    }
                }
            }

        }
    }
    return filePath
}
ImageColors()


function aiScript(path) {

    /*******************************************************************
    Prototype for indexOf()
    ********************************************************************/
    if (!Array.prototype.indexOf) {
        Array.prototype.indexOf = function (searchElement /*, fromIndex */) {
            "use strict";
            if (this == null) {
                throw new TypeError();
            }
            var t = Object(this);
            var len = t.length >>> 0;
            if (len === 0) {
                return -1;
            }
            var n = 0;
            if (arguments.length > 0) {
                n = Number(arguments[1]);
                if (n != n) { // shortcut for verifying if its NaN  
                    n = 0;
                } else if (n != 0 && n != Infinity && n != -Infinity) {
                    n = (n > 0 || -1) * Math.floor(Math.abs(n));
                }
            }
            if (n >= len) {
                return -1;
            }
            var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
            for (; k < len; k++) {
                if (k in t && t[k] === searchElement) {
                    return k;
                }
            }
            return -1;
        }
    }
    /*******************************************************************
    ********************************************************************/

    var colorUsed = new Array();
    var finalPath = path.replace(":", "/", "g");
    var doc = app.open(new File(finalPath));
    doc.selection = null;
    var _pageItems = doc.pageItems;
    for (var i = 0; i < _pageItems.length; i++) {
        doc.selection = null;
        _pageItems[i].selected = true;
        var selectedSwatches = doc.swatches.getSelected();
        if (selectedSwatches.length) {
            if (colorUsed.indexOf(selectedSwatches[0].name) == -1)
                colorUsed.push(selectedSwatches[0].name);
        }
    }
    doc.close(SaveOptions.DONOTSAVECHANGES);
    return colorUsed;
}  

 

 

This will return all swatches name that is used in ai file that you have placed in the indesign document. Currently this script will return duplicate swatches name if it is used twice or more in ai file.  I have attached the sample Indesign and AI file as well on which I have run the script. Keep both files at any location, and make sure you update the link in Indesign file as per your location of ai file on your machine.

Illustrator File 

Indesign File 

 

Let us know how it works for you.

 

EDIT : Added handling of unique names using Array.indexOf()

TOPICS
Scripting

Views

107

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Illustrator Script to get the path item used fill color name

Participant ,
Oct 23, 2020

Copy link to clipboard

Copied

Hi guys,

Could any help me to get the illustrator image path item fill color used name through script.

 

Adobe Community Professional
Correct answer by Charu Rajput | Adobe Community Professional

Hi Raghav,

I have merged your code and code shared by @pixxxelschubser  which is written by @Silly-V to get the swatches name from Indesign when ai file is placed in the Indesign documnt.

 

#target indesign
#targetengine "session"

var illustratorImages = [];
var filePath

function ImageColors() {

    var myDoc = app.activeDocument;
    var mypages = myDoc.spreads;
    if (mypages.length > 0) {
        for (var k = 0; k < mypages.length; k++) {
            var myPageItems = mypages[k].allGraphics;
            for (var l = 0; l < myPageItems.length; l++) {
                if (myPageItems[l].parentPage != null) {
                    if (myPageItems[l].itemLayer.visible == true && myPageItems[l].visible == true) {
                        if (myPageItems[l].itemLink.name.indexOf(".ai") != -1) {
                            var bt = new BridgeTalk();
                            bt.target = "illustrator";

                            var myScript = aiScript.toString() + "\r";
                            filePath = myPageItems[l].itemLink.filePath;
                            myScript += "aiScript(\"" + filePath + "\")";
                            bt.body = myScript;



                            bt.onResult = function (resObj) {
                                //get a string sent back from AI
                                alert("Illustrator sent this message back to InDesign. " + resObj.body.toString());
                            }

                            //error handler
                            bt.onError = function (inBT) { alert(inBT.body); };

                            //send to Illustrator
                            bt.send();
                        }
                    }
                }
            }

        }
    }
    return filePath
}
ImageColors()


function aiScript(path) {

    /*******************************************************************
    Prototype for indexOf()
    ********************************************************************/
    if (!Array.prototype.indexOf) {
        Array.prototype.indexOf = function (searchElement /*, fromIndex */) {
            "use strict";
            if (this == null) {
                throw new TypeError();
            }
            var t = Object(this);
            var len = t.length >>> 0;
            if (len === 0) {
                return -1;
            }
            var n = 0;
            if (arguments.length > 0) {
                n = Number(arguments[1]);
                if (n != n) { // shortcut for verifying if its NaN  
                    n = 0;
                } else if (n != 0 && n != Infinity && n != -Infinity) {
                    n = (n > 0 || -1) * Math.floor(Math.abs(n));
                }
            }
            if (n >= len) {
                return -1;
            }
            var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
            for (; k < len; k++) {
                if (k in t && t[k] === searchElement) {
                    return k;
                }
            }
            return -1;
        }
    }
    /*******************************************************************
    ********************************************************************/

    var colorUsed = new Array();
    var finalPath = path.replace(":", "/", "g");
    var doc = app.open(new File(finalPath));
    doc.selection = null;
    var _pageItems = doc.pageItems;
    for (var i = 0; i < _pageItems.length; i++) {
        doc.selection = null;
        _pageItems[i].selected = true;
        var selectedSwatches = doc.swatches.getSelected();
        if (selectedSwatches.length) {
            if (colorUsed.indexOf(selectedSwatches[0].name) == -1)
                colorUsed.push(selectedSwatches[0].name);
        }
    }
    doc.close(SaveOptions.DONOTSAVECHANGES);
    return colorUsed;
}  

 

 

This will return all swatches name that is used in ai file that you have placed in the indesign document. Currently this script will return duplicate swatches name if it is used twice or more in ai file.  I have attached the sample Indesign and AI file as well on which I have run the script. Keep both files at any location, and make sure you update the link in Indesign file as per your location of ai file on your machine.

Illustrator File 

Indesign File 

 

Let us know how it works for you.

 

EDIT : Added handling of unique names using Array.indexOf()

TOPICS
Scripting

Views

108

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Oct 23, 2020 0
Adobe Community Professional ,
Oct 23, 2020

Copy link to clipboard

Copied

Hi Raghav,

Could you please explain more or share some screenshot what exactly you want to extract and from where. You can use fillColor property but, it depends whether color is process or spot. So to help you more more information will be beneficial.

 

Best regards

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 23, 2020 1
Most Valuable Participant ,
Oct 23, 2020

Copy link to clipboard

Copied

Additional to what @Charu Rajput wrote - are we talking about a single path, or multiple paths, or compound paths, or grouped paths?

Or about selected paths (or paths in a document without selection)?

 

Or - what not possible is - paths with multiple fill colors (applied via Appearance Panel)?

 

Please describe a bit more in detail what you want.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 23, 2020 1
Participant ,
Oct 23, 2020

Copy link to clipboard

Copied

Thanks for your replies,  if the illustrator image contains text, rectangle  and strokes. I need the script to pull the used color name in the images.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 23, 2020 0
Participant ,
Oct 23, 2020

Copy link to clipboard

Copied

Whatever if the color process or spot, but the script to pull the used color name in the text, stroke or rectangle object of the image

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 23, 2020 0
Adobe Community Professional ,
Oct 23, 2020

Copy link to clipboard

Copied

Could you please attach a sample file? By image you mean you place some png or jpeg inside the document or some vector image?

 

Best regards

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 23, 2020 0
Participant ,
Oct 23, 2020

Copy link to clipboard

Copied

Hi Charu,

This is my code, I have placed AI images(spot or process colors) in the indesign document and open the illustrator image through my code, but I need to get the color names fill in  the text, strokes and objects of images. After that, I ll return the color name to indesign.

#target indesign
#targetengine "session" 

var illustratorImages = [];
var filePath
function ImageColors(){

    var myDoc = app.activeDocument;
    var mypages = myDoc.spreads;
    if(mypages.length>0){
        for(var k=0;k<mypages.length;k++){
            var myPageItems = mypages[k].allGraphics;
            for(var l=0;l<myPageItems.length;l++){
                if(myPageItems[l].parentPage != null){
                    if (myPageItems[l].itemLayer.visible == true && myPageItems[l].visible == true){
                        if(myPageItems[l].itemLink.name.indexOf(".ai") != -1){
                            var bt = new BridgeTalk();  
                            bt.target = "illustrator";  
                            
                            var myScript = aiScript.toString() + "\r";
                            filePath = myPageItems[l].itemLink.filePath;
                            myScript += "aiScript(\"" + filePath + "\")";
                            bt.body = myScript;



                            bt.onResult = function(resObj) { 
                                //get a string sent back from AI
                                alert("Illustrator sent this message back to InDesign. " + resObj.body.toString());
                                }  

                            //error handler
                            bt.onError = function( inBT ) { alert(inBT.body); };  

                            //send to Illustrator
                            bt.send();  
                        }
                    }
                }
            }

        }
    }
    return filePath
}
ImageColors()


function aiScript(path) {  
    var finalPath = path.replace(":", "/", "g");
    var doc = app.open(new File(finalPath));
    var myPageItem = doc.swatches;
    for(i=0; i<myPageItem.length; i++){
        var colorName = myPageItem[i].name;
    }
    var returnColor = colorName;
    doc.close(SaveOptions.DONOTSAVECHANGES);
    return returnColor;

}  

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 23, 2020 0
Most Valuable Participant ,
Oct 23, 2020

Copy link to clipboard

Copied

Similar to your InDesign thread?

Indesign Script for get color name or values of illustrator images used in the document 

 

Why you do not try to get the colors of an image from Photoshop?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 23, 2020 1
Most Valuable Participant ,
Oct 24, 2020

Copy link to clipboard

Copied

Ragav@july2020 

Please use the blue Reply button. This prevents deeply nested answers.

 

Your code is an InDesign script which use a BridgeTalk function to get the Illustrator swatches? Why? I thought you want to get the used colors? Or not?

 

And please: However, an Illustrator artwork is made up of paths (vector-based) and is usually referred to as a graphic. An Illustrator document can contain images, but the Ai file itself is not referred to as an image.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 24, 2020 0
Participant ,
Oct 24, 2020

Copy link to clipboard

Copied

My requirement is to get the used colors name from the each page of the indesign document and updated in the slug table. I have a code to pull the colors used in the indesign document, and I need to took the colors from the AI images placed in the indesign document. 

That y I open the placed illustrator images via bridge talk and try to get the colors of AI file. But, I tried through swatches but it not get the all color names.

 

Sorry, Iam new to illustrator i cant mention the proper terms, it may get confusing.... SOrry for that.

 

 

Is not possible to pick the color name fill in the path of the AI file via script?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 24, 2020 0
Most Valuable Participant ,
Oct 24, 2020

Copy link to clipboard

Copied

Uff, exhausting.

 

Assumed:

You have an InDesign file.

In that InDesign file you have linked a few Illustrator files or graphics (NOT IMAGES!)

The Link Panel in ID shows the path to every linked file.

 

Every Illustrator file could contain several item/elements:

  • Paths
  • Compound Paths
  • IMAGES
  • which also could be nested in (possibly) nested Groups

and so on.

 

Further more the Illustrator file could contain swatches.

  • "normal" swatches
  • global swatches
  • spot color swatches

 

But not every path (in your Illustrator file) uses a swatch for it's fill or stroke color.

Therefore you can read only global or spot color swatch names.

 

Otherwise you have to check if the color is a RGB or CMYK or Greycolor (or others). And you have to loop through all layers, sublayers (possibly nested), groups (possibly nested), compound paths and paths. You will not receive any colors from placed elements such as IMAGES or meshed items or diagrams or the like. You will not get (all) the colors of paths with multiple fill colors (applied through Appearance Panel).

 

Can you see some of the problems?


In an Illustrator file, doing what you want is difficult. It's much more difficult to do what InDesign is supposed to do through BridgeTalk. But in addition to that:

Ragav@july2020 wrote: "… I cant mention the proper terms, it may get confusing …"

 

Yes. It is very very confusing.

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 24, 2020 1
Participant ,
Oct 24, 2020

Copy link to clipboard

Copied

Thanks for understanding. 

The only posibilites is to get the global color name in the swatches right?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Oct 24, 2020 0
Resources