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

Illustrator Script to get the path item used fill color name

Participant ,
Oct 23, 2020 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.

 

TOPICS
Scripting

Views

2.6K

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
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 24, 2020 Oct 24, 2020

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].allGraphi
...

Votes

Translate

Translate
Adobe
Community Expert ,
Oct 23, 2020 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

Votes

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
community guidelines
Community Expert ,
Oct 23, 2020 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.

Votes

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
community guidelines
Participant ,
Oct 23, 2020 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.

Votes

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
community guidelines
Participant ,
Oct 23, 2020 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

Votes

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
community guidelines
Community Expert ,
Oct 23, 2020 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

Votes

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
community guidelines
Participant ,
Oct 23, 2020 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;

}  

Votes

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
community guidelines
Community Expert ,
Oct 23, 2020 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?

Votes

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
community guidelines
Community Expert ,
Oct 24, 2020 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.

Votes

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
community guidelines
Participant ,
Oct 24, 2020 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?

Votes

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
community guidelines
Community Expert ,
Oct 24, 2020 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.

 

Votes

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
community guidelines
Participant ,
Oct 24, 2020 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?

Votes

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
community guidelines
Community Expert ,
Oct 24, 2020 Oct 24, 2020

Copy link to clipboard

Copied

Sorry, I made a mistake.

 

The behaviour has changed in the last versions. If a (swatch) color is applied to a pathItem and the pathItem is selected - you always will see the associated swatch (if a associated swatch exists) in Swatches Panel.

 

One possibility:

pathItem color name 

 

  • select one path item with a spot color and run the script snippet written by @Silly-V 
  • change the color to a global swatch and run the snippet again
  • change the color to a "normal" swatch and run the snippet again
  • change the color to a color from Colors Panel and run the snippet again

 

Votes

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
community guidelines
Community Expert ,
Oct 24, 2020 Oct 24, 2020

Copy link to clipboard

Copied

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

Best regards

Votes

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
community guidelines
Participant ,
Oct 26, 2020 Oct 26, 2020

Copy link to clipboard

Copied

Hi Charu,

I can't able to unique the same color names pushed in this array "colorUsed" using with below unique function why?

function aiScript(path) {
    var colorUsed = [];
    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) {
            colorUsed.push(selectedSwatches[0].name);
        }
        var finalArray = unique(colorUsed)
        alert(finalArray)
    }
    doc.close(SaveOptions.DONOTSAVECHANGES);
    return colorUsed;
} 

    function unique(array) {
        var o = {};
        r = [];
        for (i = 0; i < array.length; i++) {
            o[array[i]] = array[i];
        }
        for (i in o) {
            r.push(o[i]);
        }
        return r;
    };

Votes

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
community guidelines
Community Expert ,
Oct 26, 2020 Oct 26, 2020

Copy link to clipboard

Copied

Hi Ragav@july2020 ,

I have updated the above answer that will handle the unique values as well now. I have used Array.indexOf() method to make sure colorUsed variable will have only unique names.

 

I hope this helps you.

Best regards

Votes

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
community guidelines
Participant ,
Oct 26, 2020 Oct 26, 2020

Copy link to clipboard

Copied

Thanks Charu! I ll use the code and let u know

Votes

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
community guidelines
Participant ,
Oct 27, 2020 Oct 27, 2020

Copy link to clipboard

Copied

Hi Charu,

 

Its working fine! thanks for ur help.

 

Any idea to add the used colors in the AI files to the swatches like in indesign below

app.menuActions.itemByName("$ID/Add All Unnamed Colors").invoke()

Votes

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
community guidelines
Community Expert ,
Oct 27, 2020 Oct 27, 2020

Copy link to clipboard

Copied

That's great!.

I think you have already open a new thread for the same lets continue there.. 🙂

Best regards

Votes

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
community guidelines
Participant ,
Oct 27, 2020 Oct 27, 2020

Copy link to clipboard

Copied

 

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;
}  

 

I having some issue in the above code Charu. The code was not execute with hidden layers, locked and also its not ignoring the [none], [black] & [registration] color.

Is it possible to get the used colors without go through the swatches?

 

 

Votes

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
community guidelines
Community Expert ,
Oct 27, 2020 Oct 27, 2020

Copy link to clipboard

Copied

Yes of course that can be done, did you try to manipulate the code yourself once?

In this way you are able to learn as well. Try manipulating and if you face any issues we are here to help. Getting readymade code may help in your work or may be you can finish faster but you will not learn yourself.

 

Let us know where you stuck.

Best regards

Votes

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
community guidelines
Participant ,
Oct 27, 2020 Oct 27, 2020

Copy link to clipboard

Copied

 

Thanks Charu ! I have tried and run the action in different way its working fine. And  I got issue in  "_pageItems[i].selected = true;"  if any item is locked or hidden and please share api to get the colors name directly from  the AI files.

 

 

 

 

   var colorUsed = new Array();
   var finalPath = path.replace(":", "/", "g");
   var doc = app.open(new File(finalPath));

   var set = 'Colors';  
   // Location of your saved Actions.aia file  
   var aia =File('~/Documents/Scripts/scriptResources/Colors.aia');//  
   //  
   try{  
   app.unloadAction(set,'');  
   }catch(e){}  
   app.loadAction(aia);
   app.doScript ('add_used_colors', set);
   doc.selection = null;
   var _pageItems = doc.pageItems;

   for (var i = 0; i < _pageItems.length; i++) {
       doc.selection = null;
       _pageItems[i].locked == true ?  _pageItems[i].locked = false : '';
      _pageItems[i].selected = true;
       var selectedSwatches = doc.swatches.getSelected();
       if (selectedSwatches.length) {
           if(selectedSwatches[0].name.indexOf("[") == -1){
               if (colorUsed.indexOf(selectedSwatches[0].name) == -1){
                   colorUsed.push(selectedSwatches[0].name);
               }
           }
       }
   }
   doc.close(SaveOptions.DONOTSAVECHANGES);
   return colorUsed;
}  

 

 

 

 

 

Votes

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
community guidelines
Community Expert ,
Oct 27, 2020 Oct 27, 2020

Copy link to clipboard

Copied

Hi Raghav,

I think you are mixing up the two threads. Please post relevant data to the appropriate thread. So that this will be useful for the future user as well.

Best regards

Votes

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
community guidelines
Participant ,
Oct 27, 2020 Oct 27, 2020

Copy link to clipboard

Copied

I integrated both code for clear explaination, sorry, if it confused.

I was stuck to getting the color names in the AI file from the below code. To get the color names through swatches only in illustrator? fillColor.name is not working here.  Please share your thoughts.

 

for (var i = 0; i < _pageItems.length; i++) {
       doc.selection = null;
       _pageItems[i].locked == true ?  _pageItems[i].locked = false : '';
      _pageItems[i].selected = true;
       var selectedSwatches = doc.swatches.getSelected();
       if (selectedSwatches.length) {
           if(selectedSwatches[0].name.indexOf("[") == -1){
               if (colorUsed.indexOf(selectedSwatches[0].name) == -1){
                   colorUsed.push(selectedSwatches[0].name);
               }
           }
       }
   }
   doc.close(SaveOptions.DONOTSAVECHANGES);
   return colorUsed;
}  

 

 

Votes

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
community guidelines
Community Expert ,
Oct 27, 2020 Oct 27, 2020

Copy link to clipboard

Copied

Ragav@july2020 

Please try this

 {
    var colorUsed = new Array();
    var finalPath = path.replace(":", "/", "g");
    var doc = app.open(new File(finalPath));

    var set = 'Colors';
    // Location of your saved Actions.aia file  
    var aia = File('~/Documents/Scripts/scriptResources/Colors.aia');//  
    //  
    try {
        app.unloadAction(set, '');
    } catch (e) { }
    app.loadAction(aia);
    doc.selection = null;

    var _pageItems = doc.pageItems;
    var isLocked = false;
    var isHidden = false
    
    for (var i = 0; i < _pageItems.length; i++) {
        doc.selection = null;
        isLocked = false;
        isHidden = false;
        if (_pageItems[i].locked) {
            _pageItems[i].locked = false;
            isLocked = true
        }
        if(_pageItems[i].hidden){
            _pageItems[i].hidden = false;
            isHidden = true;
        }
        _pageItems[i].selected = true;
        app.doScript('add_used_colors', 'set');
        var selectedSwatches = doc.swatches.getSelected();
        if (selectedSwatches.length) {
            if (selectedSwatches[0].name.indexOf("[") == -1) {
                if (colorUsed.indexOf(selectedSwatches[0].name) == -1) {
                    colorUsed.push(selectedSwatches[0].name);
                }
            }
        }
        if(isLocked)
            _pageItems[i].locked = true;
        if(isHidden){
            _pageItems[i].hidden = true;
        }
    }
    doc.close(SaveOptions.DONOTSAVECHANGES);
   return colorUsed;
}  
Best regards

Votes

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
community guidelines