10 Replies Latest reply on Oct 31, 2015 2:34 PM by MicrobrewMedia

    Find Color Space of Document & Find Swatches that contain contents of text frame.

    MicrobrewMedia

      Hi everyone,

       

      I've been trying to figure this out for the past three days. I've tried everything I can think of and search all over the internet (GitHub, Forums etc). I'm not a javascript wizard and definitely an Extendscript Newb.

       

      Trying to determine color space of the document. Last attempt was...

      alert(theDoc.properties.ColorSpace);
      

       

      Background on this:

      I have a datamerge that creates the attached fields from a CSV. It is an internal reference code to a color. I'm able to cycle try using the for loop an grab the color code. But I can't seem to figure a way to search the Swatches. A thought was to put them into an Array and sift through them. The color code provided is not the entire swatch name which makes things interesting. Example name is 2297R - 19995, where 2297r is the color code from the text frame. Possible?

       

      main();
      // this is the function
      function main(){
      // setup some variables
      var theDoc = app.activeDocument; // active doc
      var thePage = theDoc.pages.item(0); // first page
      var theSwatches = theDoc.swatches;
      var list_of_swatches = theDoc.swatches.everyItem().getElements();
      var n = theDoc.swatches.length;
      var color;
      var rgbValues;
      var i;
      var     t;
      
      
      //Get all groups for this document:
      var theGroups = theDoc.groups;
      var theLayers = theDoc.layers;
      
      
      // Color Codes
      var primaryColor = thePage.textFrames.item("Primary Color Code"); 
      var secondayColor = thePage.textFrames.item("Secondary Color Code"); 
      var accentColor = thePage.textFrames.item("Accent Color Code"); 
      var matchGarmentColor = thePage.textFrames.item("Match to Garment Color Code"); 
      var lightTonalColor = thePage.textFrames.item("Light Tonal Color Code"); 
      var darkTonalColor = thePage.textFrames.item("Dark Tonal Color Code"); 
      var neutralColor = thePage.textFrames.item("Neutral Color Code"); 
      var greenColor = thePage.textFrames.item("Green Color Code"); 
      
      
      
      // Code Codes
      var primaryColorCode        =   primaryColor.contents;
      // var secondaryColorCode      =   secondaryColor.contents;
      // var accentColorCode         =   accentColor.contents;
      // var matchGarmentColorCode   =   matchGarmentColor.contents;
      // var lightTonalColorCode     =   lightTonalColor.contents;
      // var darkTonalColorCode      =   darkTonalColor.contents;
      // var neutralColorCode        =   neutralColor.contents;
      // var greenColorCode          =   greenColor.contents;
      

       

      Any help or guidence is greatly appreciated.

       

      Thank you.

        • 1. Re: Find Color Space of Document & Find Swatches that contain contents of text frame.
          Peter Spier Most Valuable Participant (Moderator)

          I've moved your question into the InDesign Scripting forum...

          • 2. Re: Find Color Space of Document & Find Swatches that contain contents of text frame.
            Jump_Over Level 5

            Hi,

             

            You could use a function to detect a swatch based on part of its name, like:

            function findSwatch (PartOfName) {
            var
                 cSwatches = app.activeDocument.swatches.everyItem().name.join("@$@"),
                   cIndex =  cSwatches.search (PartOfName), cSwatch;
            
              if (cIndex == -1) return false;
              cIndex = cSwatches.slice( 0, cIndex ).split ("@$@").length - 1;
              cSwatch = app.activeDocument.swatches[cIndex];
              return cSwatch;
              }
            
            
            
            
            

             

            So looking for a swatch marked by code from:

            var primaryColorCode        =   primaryColor.contents;


            you could call it like:

            mSwatch = findSwatch (primaryColorCode);


            It returns false if not found

            (case sensitive)


            Jarek

            • 3. Re: Find Color Space of Document & Find Swatches that contain contents of text frame.
              Laubender Adobe Community Professional & MVP

              Hi MicrobrewMedia,

              have a look at the code of this wonderful script here:

               

              Color Script — Roland Dreger, Grafikdesign

               

              Downside: It's UI is in German.

               

              EDIT: On this page is the downloadable file:

              Farbmanagement in Adobe InDesign — Roland Dreger, Grafikdesign

               

              Uwe

              • 4. Re: Find Color Space of Document & Find Swatches that contain contents of text frame.
                MicrobrewMedia Level 1

                Jump_Over - Thank you first for the reply. It helped me get closer to my final rough draft if you will.

                 

                When I hard code the Variable into the function you provided it works and my code was executing.

                 

                Weird thing is, the of PrimaryColorCode is passed in as 2297r to the findSwatch function and I write int the log and it displays. But it doesn't run through the function as it keeps returning false. But when I have "2297r" hard coded to a variable inside the function and I assign it, it gets the index value (as shown in LOG FILE 2).

                 

                Any direction is greatly appreciated to solve this little hiccup.

                 

                Cheers

                 

                LOG FILE 1

                Swatch Length  : 4709
                primaryColorCode  : 2297r
                secondaryColorCode  : 1219s
                accentColorCode  : 1220s
                matchGarmentColorCode  : 2296r
                lightTonalColorCode  : 1219s
                darkTonalColorCode  : 1220s
                neutralColorCode  : 1062m
                Start Layer : 0
                Cur Layer: [object Layer]
                Primary Swatch: false
                
                


                LOG FILE 2 (Hard Coded Variable)

                Swatch Length  : 4709
                primaryColorCode  : 2297r
                secondaryColorCode  : 1219s
                accentColorCode  : 1220s
                matchGarmentColorCode  : 2296r
                lightTonalColorCode  : 1219s
                darkTonalColorCode  : 1220s
                neutralColorCode  : 1062m
                Start Layer : 0
                Cur Layer: [object Layer]
                Primary Swatch: 3826
                
                


                Here is how it lives currently.

                #target indesign 
                //this runs the function
                main();
                // this is the function
                function main(){
                // setup some variables
                var theDoc = app.activeDocument; // active doc
                var thePage = theDoc.pages.item(0); // first page
                var totalPages = theDoc.pages.length;
                var theSwatches = theDoc.swatches;
                var cSwatches = theSwatches.everyItem().name.join("@$@");
                var n = theSwatches.length;
                $.writeln("Swatch Length  : " + n);
                //Get all groups for this document:
                var theGroups = theDoc.groups;
                var theLayers = theDoc.layers;
                var layersTotal = theDoc.layers.length;
                var layerName = theDoc.activeLayer.name;
                var layerId = theDoc.activeLayer.index;
                var workLayer = theDoc.layers.firstItem(); 
                var startLayer = theDoc.layers.item(0).index;
                
                
                // Layer IDs
                // MTS : 0 -- Disabled for Now
                // Green : 1 -- Disabled for Now
                // Neutral : 2
                // Dark Tonal: 3
                // Light Tonal : 4
                // MTG : 5
                // Accent : 6
                // Secondary : 7
                // Primary : 8
                // Main: 9 (Unused for Coloring)
                
                
                // Color Codes
                var primaryColor = thePage.textFrames.item("Primary Color Code");
                var secondaryColor = thePage.textFrames.item("Secondary Color Code");
                var accentColor = thePage.textFrames.item("Accent Color Code");
                var matchGarmentColor = thePage.textFrames.item("Match to Garment Color Code");
                var lightTonalColor = thePage.textFrames.item("Light Tonal Color Code");
                var darkTonalColor = thePage.textFrames.item("Dark Tonal Color Code");
                var neutralColor = thePage.textFrames.item("Neutral Color Code");
                var greenColor = thePage.textFrames.item("Green Color Code");
                
                
                
                
                // Code Codes
                var primaryColorCode        =  primaryColor.contents.toLowerCase();
                  $.writeln("primaryColorCode  : " + primaryColorCode);
                var secondaryColorCode      =  secondaryColor.contents.toLowerCase();
                  $.writeln("secondaryColorCode  : " + secondaryColorCode);
                var accentColorCode        =  accentColor.contents.toLowerCase();
                  $.writeln("accentColorCode  : " + accentColorCode);
                var matchGarmentColorCode  =  matchGarmentColor.contents.toLowerCase();
                  $.writeln("matchGarmentColorCode  : " + matchGarmentColorCode);
                var lightTonalColorCode    =  lightTonalColor.contents.toLowerCase();
                  $.writeln("lightTonalColorCode  : " + lightTonalColorCode);
                var darkTonalColorCode      =  darkTonalColor.contents.toLowerCase();
                  $.writeln("darkTonalColorCode  : " + darkTonalColorCode);
                var neutralColorCode        =  neutralColor.contents.toLowerCase();
                  $.writeln("neutralColorCode  : " + neutralColorCode);
                // var greenColorCode          =  greenColor.contents,
                // greenColorCode      =  greenColorCode.toLowerCase();
                // $.writeln("greenColorCode  : " + greenColorCode);
                
                
                function findSwatch (PartOfName) {
                  var part = ("2297r");
                  // Even thought PartOfName & part are the same they aren't working right below in the search... Currently Returns False for PartOfName while part works just fine. Variable types are both strings checked using typeof().
                  var  cIndex =  cSwatches.search(PartOfName), cSwatch; 
                  if (cIndex == -1) return false; 
                  cIndex = cSwatches.slice( 0, cIndex ).split ("@$@").length - 1; 
                  cSwatch = app.activeDocument.swatches[cIndex]; 
                  return cIndex;
                  // return cSwatch; 
                  } 
                
                
                var startLayer = 0;
                
                
                // Start the Loop
                for(var myCounter = 0; myCounter < totalPages; myCounter++) {
                      $.writeln("Start Layer : " + startLayer);
                      for(var i = 0; i < layersTotal ; i++) {
                    $.writeln("Current Layer : " + theDoc.layers[i].name);
                  if (theDoc.activeLayer.name == "Primary" ){
                      curLayer = theLayers.item("Primary");
                      $.writeln("Cur Layer: " + curLayer);
                  primarySwatch = findSwatch(primaryColorCode); 
                  $.writeln("Primary Swatch: " + primarySwatch);
                  primaryShape = curLayer.ovals.item(0);
                      primaryShape.fillColor = app.activeDocument.swatches[primarySwatch].index;
                  }
                  else if (theDoc.layers[i].name == "Secondary"){
                      curLayer = theLayers.item("Secondary");
                      $.writeln("Cur Layer: " + curLayer);
                  secondarySwatch = findSwatch(secondaryColorCode); 
                  $.writeln("Secondary Swatch: " + secondarySwatch);
                  secondaryShape = curLayer.ovals.item(0);
                      secondaryShape.fillColor = app.activeDocument.swatches[secondarySwatch];
                    } else if (theDoc.layers[i].name == "Accent" ){
                      curLayer = theLayers.item("Accent");
                      $.writeln("Cur Layer: " + curLayer);
                  accentSwatch = findSwatch(accentColorCode); 
                  $.writeln("Accent Swatch: " + accentSwatch);
                  accentShape = curLayer.ovals.item(0);
                      accentShape.fillColor = app.activeDocument.swatches[accentSwatch];
                    } else if (theDoc.layers[i].name == "Match to Garment"){
                  curLayer = theLayers.item("Match to Garment");
                  $.writeln("Cur Layer: " + curLayer);
                  matchToGarmentSwatch = findSwatch(matchGarmentColorCode); 
                  $.writeln("Match to Garment Swatch: " + matchToGarmentSwatch);
                  matchToGarmentShape = curLayer.ovals.item(0);
                  matchToGarmentShape.fillColor = app.activeDocument.swatches[matchToGarmentSwatch];
                    } else if (theDoc.layers[i].name == "Light Tonal"){
                  curLayer = theLayers.item("Light Tonal");
                  $.writeln("Cur Layer: " + curLayer);
                  lightTonalSwatch = findSwatch(lightTonalColorCode); 
                  $.writeln("Light Tonal Swatch: " + lightTonalSwatch);
                  lightTonalShape = curLayer.ovals.item(0);
                  lightTonalShape.fillColor = app.activeDocument.swatches[lightTonalSwatch];
                    } else if (theDoc.layers[i].name == "Dark Tonal"){
                  curLayer = theLayers.item("Dark Tonal");
                  $.writeln("Cur Layer: " + curLayer);
                  darkTonalSwatch = findSwatch(darkTonalColorCode); 
                  $.writeln("Dark Tonal Swatch: " + darkTonalSwatch);
                  darkTonalShape = curLayer.ovals.item(0);
                  darkTonalShape.fillColor = app.activeDocument.swatches.item[darkTonalSwatch];
                    } else if (theDoc.layers[i].name == "Neutral"){
                  curLayer = theLayers.item("Neutral");
                  $.writeln("Cur Layer: " + curLayer);
                  neutralSwatch = findSwatch(neutralColorCode); 
                  $.writeln("Neutral Swatch: " + neutralSwatch);
                  $.writeln("Neutral Swatch: " + neutralSwatch.name);
                  neutralShape = curLayer.ovals.item(0);
                  neutralShape.fillColor = app.activeDocument.colors(neutralSwatch);
                  neutralShape.fillColor = app..colors(neutralSwatch);
                  //  } else if (theDoc.layers[i].name == "Green"){
                  // curLayer = theLayers.item("Green");
                  // $.writeln("Cur Layer: " + curLayer);
                  // greenSwatch = findSwatch(greenColorCode); 
                  // $.writeln("Green Swatch: " + greenSwatch);
                  // greenShape = curLayer.ovals.item(0);
                  // greenShape.fillColor = app.activeDocument.swatches[greenSwatch];
                    } else {
                      $.writeln("Not a Matching Layer");
                    }
                      } // Layers
                  var startLayer = startLayer + 1 ;
                    } // Pages 
                }
                
                
                • 5. Re: Find Color Space of Document & Find Swatches that contain contents of text frame.
                  MicrobrewMedia Level 1

                  Jump_Over

                   

                  If necessary, I'd be open to taking this to a freelance operation with you. I feel I am very close after your help.

                  • 6. Re: Find Color Space of Document & Find Swatches that contain contents of text frame.
                    Jump_Over Level 5

                    Hi,

                     

                    Did you try to debug code running step by step and watching variable's values?

                    You need to open "Data Browser" window in ESTK and set some breakpoint(s) within a code.

                     

                    I suggest to place a breakpoint inside function "findSwatch()" and exam variables running step by step.

                     

                    Jarek

                    • 7. Re: Find Color Space of Document & Find Swatches that contain contents of text frame.
                      MicrobrewMedia Level 1

                      Thanks Jump_Over. I hadn't ever noticed that little tab!

                       

                      I see that the argument PartOfName is being passed through with the expected value of 2297r. But when using that inside the function it seems to go up in flames and cause issue.

                      Seems cIndex is the culprit as it repeatedly comes up with a value of -1.

                      • 8. Re: Find Color Space of Document & Find Swatches that contain contents of text frame.
                        Jump_Over Level 5

                        Hi,

                         

                        So we have 2 possible error sources:

                        1. var cSwatches badly collected swatches names or is unavailable

                        2. there is no such  swatch in this doc with "2297r" as a part of its name

                         

                        When breakpoint is set inside a function - check external variable (cSwatches} by expanding "this" on "Data Browser" list

                         

                        Jarek

                        • 9. Re: Find Color Space of Document & Find Swatches that contain contents of text frame.
                          Jump_Over Level 5

                          Hi,

                           

                          ... there is a third source possibly:

                          - exam "PartOfName" contents if there are any whitespaces inside - in this case search() method would return "-1" as well.

                           

                          (i.e. alert (PartOfName.length) ==> is it == 5?)

                           

                          Jarek

                          • 10. Re: Find Color Space of Document & Find Swatches that contain contents of text frame.
                            MicrobrewMedia Level 1

                            Jump_Over it is the PartOfName.length. Good call! There were two Unicode characters being put in front and trailing the value for a total length of 7!

                             

                            I used this to filter the unicode characters out within the function.

                                 rtrim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;    
                                 PartOfName = PartOfName.replace(rtrim, '');
                            

                             

                            Seems to pass everything through and gets correct value!