12 Replies Latest reply on Jun 17, 2016 1:32 AM by Johnwhite

    How to define my color?

    Johnwhite Level 1

      Hi experts,

       

      I have a script:

       

      app.findObjectPreferences = null;
      app.findObjectPreferences.fillColor = "MARKUP";
      app.changeObjectPreferences.fillColor = "Black";
      app.changeObject();
      app.findObjectPreferences = null;
      app.findObjectPreferences.strokeColor = "MARKUP";
      app.changeObjectPreferences.strokeColor = "Black";
      app.changeObject();
      

       

      for find and change object color, but if the docs which has no such as color name in the swatch, the script will run into error, so, how can I define if no such as color, script continue? how can make the script as below working?

       

      var myDoc = app.activeDocument;
      myColor = myDoc.colors.name("MARKUP");
      if (!cmyColor) continue;
      
      app.findObjectPreferences = null;
      app.findObjectPreferences.fillColor = myColor;
      app.changeObjectPreferences.fillColor = "Black;
      app.changeObject();
      
      app.findObjectPreferences = null;
      app.findObjectPreferences.strokeColor = myColor;
      app.changeObjectPreferences.strokeColor = "Black";
      app.changeObject();
      

       

      thanks

      Regard

      John

        • 1. Re: How to define my color?
          milligramme Level 3

          Hi

           

          you can fix some part of this script.

           

          var myDoc = app.activeDocument;  
          var myColor = myDoc.swatches.item("MARKUP");
          if (!myColor.isValid){
            alert("NO SWATCH");
            exit();
          }
          

           

          now you can't exit if the document don't have 'MARKUP' swatch.

          but this code wont work in my InDesign CC, I found same issue

           

          [JS CS4] findObjectPreferences.fillColor error |Adobe Community https://forums.adobe.com/thread/583184

           

          I very much regret to say I don't have any idea to solve this problem.

           

          thank you

          mg

          • 2. Re: How to define my color?
            Johnwhite Level 1

            Thank you mg

             

            so I changed it into like this:

             

            var myDoc = app.activeDocument;
            var myColor = myDoc.swatches.item("MARKUP");
            if (!myColor.isValid){
            alert("NO SWATCH");
            exit();
            }
            app.findObjectPreferences = null;
            app.findObjectPreferences.fillColor = myColor;
            app.changeObjectPreferences.fillColor = "Black";
            app.changeObject();
            app.findObjectPreferences = null;
            app.findObjectPreferences.strokeColor = myColor;
            app.changeObjectPreferences.strokeColor = "Red";
            app.changeObject();
            

             

             

             

            but I got error.

             

            How can I fix?

             

            thanks

            regard

            John

            • 3. Re: How to define my color?
              Ravindra_KCS Level 1

              Hi John,

               

              You can use try catch for this as follows..

               

              try{

                  app.findObjectPreferences = null; 

                  app.findObjectPreferences.fillColor = "Markup1"; 

                  app.changeObjectPreferences.fillColor = "Test"; 

                  app.changeObject(); 

                  }

                 catch(err)

                 {

                      app.findObjectPreferences = null; 

                      app.findObjectPreferences.strokeColor = "Markup"; 

                      app.changeObjectPreferences.strokeColor = "Black"; 

                      app.changeObject(); 

                  }

               

              Thanks and Regards

              Ravindra

              • 4. Re: How to define my color?
                Johnwhite Level 1

                Thank you Ravindra

                 

                I have tried, but not work.

                 

                thank you so much.

                 

                regard

                John

                • 5. Re: How to define my color?
                  Kai Rübsamen Adobe Community Professional

                  Does anybody get here a result?

                   

                  app.findObjectPreferences.strokeColor = "Markup";
                  

                   

                  I tried it with swatch (not just the name) or color and get always an error!

                  It doesn’t also work, if I use the swatch by its index, instead of the name.

                   

                  There were some posts in the forum about some bugs in CS 4/5, but it seems, that is broken in CC too?

                   

                  Kai

                  • 6. Re: How to define my color?
                    milligramme Level 3

                    Hi

                    I tested with following test-script on InDesign CS5/CC

                     

                    CC partly works, CS5 not work for me.

                    findObjectPreferences.fillColor/strokeColor should be passed by `string`

                     

                     

                    var doc = app.activeDocument;
                    var c = doc.swatches.item("MARKUP");
                    
                    
                      if (!c.isValid){
                        alert("NO SWATCH");
                        exit()
                      }
                      
                    // swatch index
                    var m = doc.swatches.itemByName("MARKUP").index;
                    var b = doc.swatches.item("Black").index;
                    
                    
                    $.writeln(app.version);
                    var props = ['strokeColor', 'fillColor'];
                    
                    
                    for (var i=0, len=props.length; i < len ; i++) {
                      app.findObjectPreferences = NothingEnum.nothing;
                      var prop = props[i];
                      
                      $.writeln(prop);
                      
                      // work for me in CC by string
                      // not work in CS5 by string
                      try {
                        $.writeln(">> " + "MARKUP".constructor.name);
                        app.findObjectPreferences[prop]   = "MARKUP";
                        app.changeObjectPreferences[prop] = "Black";
                        $.writeln("DONE!!");
                      }
                      catch(x_x){
                        $.writeln([x_x.message,x_x.line,$.stack].join("\n"));
                      }
                    
                    
                      // not work by swatch index
                      try {
                        $.writeln(">> " + doc.swatches[m]);
                        $.writeln(">> " + doc.swatches[m].constructor.name);
                        app.findObjectPreferences[prop] = doc.swatches[m];
                        app.changeObjectPreferences[prop] = doc.swatches[b];
                        $.writeln("DONE!!");
                      }
                      catch(x_x){
                        $.writeln([x_x.message,x_x.line,$.stack].join("\n"));
                      }
                    
                    
                      // not work by swatch name
                      try {
                        $.writeln(">> " + doc.swatches.item("MARKUP"));
                        $.writeln(">> " + doc.swatches.item("MARKUP").constructor.name);
                        app.findObjectPreferences[prop] = doc.swatches.item("MARKUP");
                        app.changeObjectPreferences[prop] = doc.swatches.item("Black");
                        $.writeln("DONE!!");
                      }
                      catch(x_x){
                        $.writeln([x_x.message,x_x.line,$.stack].join("\n"));
                      }
                      doc.changeObject();
                    };
                    

                     

                    thank you

                    mg.

                    • 7. Re: How to define my color?
                      Kai Rübsamen Adobe Community Professional

                      Hm! Doesn’t work here: 11.2.0.99, OS 10.9.5

                       

                      Kai

                      • 8. Re: How to define my color?
                        Johnwhite Level 1

                        yes! not work.

                         

                        John

                        • 9. Re: How to define my color?
                          milligramme Level 3

                          oh sorry!

                          tried again, doesn't work.

                           

                          9.3.0.106

                          osx 10.9.5

                           

                          I guess it worked after find object operating in GUI.

                          • 10. Re: How to define my color?
                            Kai Rübsamen Adobe Community Professional

                            John, what’s the goal? Only "MARKUP" or, "Mark-up", "Mark_UP" … which kind of objects? Example!

                            If FC does’t work, loop through all objects and check, if your color = your Color.

                             

                            Kai

                            • 11. Re: How to define my color?
                              Kai Rübsamen Adobe Community Professional

                              Here is an idea, that includes also the previous question "MARKUP, markup, MARK-UP, MARK_UP":

                               

                              app.doScript(main, ScriptLanguage.JAVASCRIPT , [], UndoModes.ENTIRE_SCRIPT, "Replace Colors");
                               
                              
                              
                              function main() {
                              
                              var curDoc = app.activeDocument;
                              var allSwatches = curDoc.swatches;
                              var allPItems = curDoc.allPageItems;
                              
                              var colorsToRemove = [];
                              
                              for (var i = 0; i < allSwatches.length; i++) {
                                var curSwatch = allSwatches[i];
                                var curSwatchName = curSwatch.name;
                                var colorToFind = curSwatchName.search(/Mark[-_]?UP/i);
                                if (colorToFind != -1) {
                              
                              colorsToRemove.push(curSwatch.name);
                              
                                }
                              }
                              alert("Found color names:\r" + colorsToRemove.join("\r"));
                              
                              for (i = 0; i < allPItems.length; i++) {
                                var curItem = allPItems[i];
                                var fColorName = curItem.fillColor.name;
                                var sColorName = curItem.strokeColor.name;
                                for (var j = 0; j < colorsToRemove.length; j++) {
                              
                              var curColorName = colorsToRemove[j];
                              
                              if (fColorName == curColorName) {
                                
                              curItem.fillColor = "Black";
                              
                              }
                              
                              if (sColorName == curColorName) {
                                
                              curItem.strokeColor = "Black";
                              
                              
                              
                                }
                              }
                              }
                              

                               

                              btw: the syntax highlighting destroyed the code

                               

                              Kai

                              • 12. Re: How to define my color?
                                Johnwhite Level 1

                                great!

                                thanks Kai

                                 

                                thank so much.

                                regard

                                John