24 Replies Latest reply on Jun 18, 2010 4:03 PM by [Jongware]

    Convert one swatch value to another

    Revolsnoj Level 1

      Hello,

       

      I have a number of InDesign CS3 documents that I will be changing the color profile on. There are numerous swatches in these documents that change values when opening the document and converting. For example: C=0 M=0 Y=100 K=0 changes to C=8 M=0 Y=82 K=0. I would like to create a script that I can input all the swatches new values and then change to what they should have been prior to converting the profile.

       

      So for example:

       

      Find C=8 M=0 Y=82 K=0 change to C=0 M=0 Y=100 K=0

      Find C=92 M=5 Y=0 K=0 change to C=100 M=0 Y=0 K=0

       

       

      etc.

       

      Thanks for any help.

        • 1. Re: Convert one swatch value to another
          AdobeScripts Level 3

          hi

           

          what platform - PC or Mac ?

           

          robin

           

          www.adobescripts.co.uk

          • 2. Re: Convert one swatch value to another
            Revolsnoj Level 1

            Hi Robin,

             

            I am using a Mac running OS 10.4 and InDesign CS3.

             

            Thanks,

             

            Jon

            • 3. Re: Convert one swatch value to another
              Kasyan Servetsky Level 5

              // List of color values to find
              var myFindArr =    [
                                          [8,0,82,0],
                                          [92,5,0,0]
                                          ];
              // List of color values to change                   
              var myChangeArr =    [
                                              [0,0,100,0],
                                              [100,0,0,0]
                                              ];
                                             
              var myDoc = app.activeDocument;

               

              for (i = myFindArr.length-1; i >= 0; i--) {
                  var myCurArray = myFindArr[i];
                  var myColorName = "C=" + myCurArray[0] + " M=" + myCurArray[1] +  " Y=" + myCurArray[2] + " K=" + myCurArray[3];
                  if (myDoc.colors.item(myColorName) != null){
                      var myColor = myDoc.colors.item(myColorName);
                      myColor.colorValue = myChangeArr[i];
                  }
              }

              • 4. Re: Convert one swatch value to another
                Danny Noonan Level 1

                I have been trying to do this for any black that isn't the system black. Is there a way to find 0,0,0,100 and replace with the ID [Black]?

                I was told when you search for 0,0,0,100, it will find [Black] and [Black] can't be replaced by itself.

                If anyone can help me with this, I would greatly appreciate it.

                 

                Thanks,

                 

                Danny

                • 5. Re: Convert one swatch value to another
                  Kasyan Servetsky Level 5

                  var myDoc = app.activeDocument;
                  var myColors = myDoc.colors;
                  var myBlack = myDoc.swatches.itemByName("Black");

                   

                  for (i = myColors.length-1; i >= 0; i--) {
                      var myColor = myColors[i];
                      if (myColor.colorValue == "0,0,0,100" && myColor.name != "Black") {
                          myColor.remove(myBlack);
                      }
                  }

                  • 6. Re: Convert one swatch value to another
                    Danny Noonan Level 1

                    Thanks Kaysan, it works perfect. You are the man!

                    • 7. Re: Convert one swatch value to another
                      Revolsnoj Level 1

                      Thank you Kasyan - it works great.

                      • 8. Re: Convert one swatch value to another
                        Ulrick R Mortensen Level 1

                        Could this be made to work on Swatches that have a name? ie Corporate Red.

                        • 9. Re: Convert one swatch value to another
                          Kasyan Servetsky Level 5

                          Yes, of course:

                          var myDoc = app.activeDocument;
                          var myBlack = myDoc.swatches.itemByName("Black");
                          var myCorporateRed = myDoc.swatches.itemByName("Corporate Red");
                          if (myCorporateRed != null) myCorporateRed.remove(myBlack);
                          
                          • 10. Re: Convert one swatch value to another
                            Ulrick R Mortensen Level 1

                            Cool. Thanks for the help.

                             

                            But how would the entire script look like if I had a whole bunch of Corporate Colors that I wanted to change the color values for?

                             

                            We have a lot of "old" InDesign documents with with alot of corporate colors in them. Recently there has been a change in most of these colors.

                            So a script to help us out would be nice.

                            I can make it work when the colors are named for example : C=70 M=0 Y=20 K=0 That first script works like a charm.

                            But when the colors are named something like: Corporate Blue Light (the same values as above) and I want to change that into lets say C=90 M=0 Y=20 K=0, then I can't make it work.

                             

                            Can it be done?

                            • 11. Re: Convert one swatch value to another
                              [Jongware] Most Valuable Participant

                              The color value-to-name only works in the UI itself. But, you can always do something like

                               

                               

                              var myCorporateRed = myDoc.swatches.itemByName("Corporate Red");
                              myCorporateRed.name = "C=70 M=0 Y=20 K=0";

                               

                               

                              and if you don't want to record the color values themselves, even something like

                               

                               

                              myCorporateRed.name = "C="+myCorporateRed.colorValue[0]+
                              "M="+myCorporateRed.colorValue[1]+
                              "Y="+myCorporateRed.colorValue[2]+
                              "K="+myCorporateRed.colorValue[3];
                              • 12. Re: Convert one swatch value to another
                                Ulrick R Mortensen Level 1

                                Thanks Jong. I will look into this tomorrow and see if I can get it to work.

                                • 13. Re: Convert one swatch value to another
                                  Ulrick R Mortensen Level 1

                                  Tried it and can't make it work. Still very much newbie in the scripting world. :-(

                                   

                                  So the task is: Trying to change the values of "Corporate Red" (and around 25 other colors) from 70,0,20,0 to 90,0,20,0.

                                   

                                  So do I have to rename the Corporate colors before I can run the script.?

                                  I expect that whats the last part is for, or am I far off here.?

                                  • 14. Re: Convert one swatch value to another
                                    Ulrick R Mortensen Level 1

                                    Just had a breakthrough I think.

                                     

                                    I think I got the hang of it.

                                    • 15. Re: Convert one swatch value to another
                                      [Jongware] Most Valuable Participant

                                      Ah OK. You want the values changed as well. Well, if your understanding is that you can write the values immediately into the colorValue array, yes, that's it.

                                       

                                      (I was assuming you only wanted to change the name, not the values. But it'll work, it'll work. You might want to build in a precaution: check if the colorSpace is CMYK before continuing.)

                                      • 16. Re: Convert one swatch value to another
                                        Ulrick R Mortensen Level 1

                                        Okay status report:

                                         

                                        As I wrote in an earlier reply, I'm very newbie in this scripting business.

                                        But so far I managed this: I made 3 scripts. One that renames the Swtaches from Corporate z to c=x m=x y=x k=x

                                        One that changes the values from c=x m=x y=x k=x to c=y m=y y=y k=y.

                                        And the the 3rd script renames the swatches back to Corporate z.

                                         

                                        Now I bet this can be done in 1 script. If yes, then how?

                                         

                                        And I've discovered that not all my documents have the same colors. Some have less, some have more. Especially those with less colors, the scripts fail, because it cant find ie Corporate Red. Is there any way to get around this? So check the colors in the script, but ignore everything else. And check the colors in script, and move on, if the color doesn't exist in the document.

                                         

                                        Hope you can help.

                                        BTW im on a Mac, 10.6.3, CS4.

                                        • 17. Re: Convert one swatch value to another
                                          John Hawkinson Level 5

                                          Best practice in this forum is for you to post your scripts so that others can take advantage of them.

                                           

                                          Now I bet this can be done in 1 script. If yes, then how?

                                           

                                          Err...you do understand that you can simply concatenate scripts together...paste them one after another into the same file.

                                           

                                          There might be fancier or more subbtle ways to combine scripts (if they all have the same outer loop, etc.), but it's really impossible to know without looking at your implementation. So post the scripts! (Use >> and Java Syntax Highlighting, please, so they are easy to read.)

                                          • 18. Re: Convert one swatch value to another
                                            Ulrick R Mortensen Level 1

                                            Sorry about that.

                                             

                                            But all the scripts are here in this thread. Everywhere I look, I can only find pieces of scripts that I have to together.

                                             

                                            Here are my scripts though.

                                             

                                             

                                            Script 1

                                             

                                            var myDoc = app.activeDocument;

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("LE Blue Light");

                                            myCorporateRed.name = "C=50 M=0 Y=10 K=0";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("LE Blue");

                                            myCorporateRed.name = "C=70 M=0 Y=15 K=0";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("LE Blue Dark");

                                            myCorporateRed.name = "C=90 M=0 Y=20 K=0";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("LE Blue Extra Dark");

                                            myCorporateRed.name = "C=100 M=0 Y=20 K=10";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("LE Orange Extra Light");

                                            myCorporateRed.name = "C=0 M=15 Y=100 K=0";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("LE Orange Light");

                                            myCorporateRed.name = "C=0 M=30 Y=100 K=0";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("LE Orange");

                                            myCorporateRed.name = "C=0 M=50 Y=100 K=0";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("LE Orange Dark");

                                            myCorporateRed.name = "C=0 M=70 Y=100 K=0";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("LE Green Extra Light");

                                            myCorporateRed.name = "C=10 M=0 Y=50 K=0";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("LE Green Light");

                                            myCorporateRed.name = "C=20 M=0 Y=80 K=0";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("LE Green");

                                            myCorporateRed.name = "C=30 M=0 Y=100 K=0";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("LE Green Dark");

                                            myCorporateRed.name = "C=50 M=0 Y=100 K=0";

                                             

                                             

                                             

                                             

                                            Script 2

                                             

                                            var myDoc = app.activeDocument;

                                             

                                             

                                             

                                            // List of color values to find

                                             

                                            var myFindArr = [

                                             

                                            [10,0,50,0],

                                             

                                            [20,0,80,0],

                                             

                                            [30,0,100,0],

                                             

                                            [50,0,100,10],

                                             

                                            [0,15,100,0],

                                             

                                            [0,30,100,0],

                                             

                                            [0,50,100,0],

                                             

                                            [0,70,100,0],

                                             

                                            [50,0,10,0],

                                             

                                            [70,0,15,0],

                                             

                                            [90,0,20,0],

                                             

                                            [100,0,20,10]

                                             

                                            ];

                                             

                                            // List of color values to change

                                             

                                            var myChangeArr = [

                                             

                                            [10,0,50,0],

                                             

                                            [20,0,80,0],

                                             

                                            [40,0,100,0],

                                             

                                            [50,0,100,10],

                                             

                                            [0,15,100,0],

                                             

                                            [0,30,100,0],

                                             

                                            [0,50,100,0],

                                             

                                            [0,63,100,0],

                                             

                                            [70,0,20,0],

                                             

                                            [90,0,20,0],

                                             

                                            [100,5,25,10],

                                             

                                            [100,10,27,20]

                                             

                                            ];

                                             

                                            var myDoc = app.activeDocument;

                                             

                                             

                                             

                                            for (i = myFindArr.length-1; i >= 0; i--) {

                                             

                                            var myCurArray = myFindArr[i];

                                             

                                            var myColorName = "C=" + myCurArray[0] + " M=" + myCurArray[1] +  " Y=" + myCurArray[2] + " K=" + myCurArray[3];

                                             

                                            if (myDoc.colors.item(myColorName) != null){

                                             

                                            var myColor = myDoc.colors.item(myColorName);

                                             

                                            myColor.colorValue = myChangeArr[i];

                                             

                                            }

                                             

                                            }

                                             

                                             

                                             

                                            Script 3

                                             

                                            var myDoc = app.activeDocument;

                                             

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("C=70 M=0 Y=20 K=0");

                                            myCorporateRed.name = "Corporate Turquoise light";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("C=90 M=0 Y=20 K=0");

                                            myCorporateRed.name = "Corporate Turqouise";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("C=100 M=5 Y=25 K=10");

                                            myCorporateRed.name = "Corporate Turqouise Dark";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("C=100 M=10 Y=27 K=20");

                                            myCorporateRed.name = "Corporate Turqouise Extra Dark";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("C=0 M=15 Y=100 K=0");

                                            myCorporateRed.name = "Corporate Yellow";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("C=0 M=30 Y=100 K=0");

                                            myCorporateRed.name = "Corporate Orange Light";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("C=0 M=50 Y=100 K=0");

                                            myCorporateRed.name = "Corporate Orange Medium";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("C=0 M=63 Y=100 K=0");

                                            myCorporateRed.name = "Corporate Orange";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("C=10 M=0 Y=50 K=0");

                                            myCorporateRed.name = "Corporate Lime Green Extra Light";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("C=20 M=0 Y=80 K=0");

                                            myCorporateRed.name = "Corporate Lime Green Light";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("C=40 M=0 Y=100 K=0");

                                            myCorporateRed.name = "Corporate Lime Green";

                                             

                                            var myCorporateRed = myDoc.swatches.itemByName("C=50 M=0 Y=100 K=0");

                                            myCorporateRed.name = "Corporate Lime Green Dark";

                                             

                                            I tried to combine them, only with the result that I go an error on the if-string.

                                             

                                             

                                             

                                            edit:

                                            But now of course it works. Typical! :-)

                                             

                                            Still need some help on the colors in the script that might not appear in the InDesign doc.

                                             

                                            Message was edited by: Ulrick R Mortensen

                                            • 19. Re: Convert one swatch value to another
                                              John Hawkinson Level 5

                                              So much for the plea for syntax highlighting. I guess I need to include the screenshot...

                                               

                                              You realize you could write each of these:

                                               

                                               

                                              var myCorporateRed = myDoc.swatches.itemByName("LE Blue Light");
                                              myCorporateRed.name = "C=50 M=0 Y=10 K=0";
                                              

                                               

                                              as simply:

                                               

                                              myDoc.swatches.item("LE Blue Light").name = "C=50 M=0 Y=10 K=0";
                                              

                                               

                                               

                                              I tried to combine them, only with the result that I go an error on the if-string.

                                               

                                              Are you trying to be deeply mysterious here? If you tell us you get an error but don't tell us what it is, it really doesn't give us much to go on. "Did you try throwing your InDesign distribution CDs out the window like a frisbee? It might solve the error!"

                                               

                                              Also, which if statement? This one?:

                                               

                                              if (myDoc.colors.item(myColorName) != null){
                                                var myColor = myDoc.colors.item(myColorName);
                                                myColor.colorValue = myChangeArr[i];
                                              }
                                              

                                               

                                              looks ok to me, but if we had any idea what the error was...

                                              • 20. Re: Convert one swatch value to another
                                                [Jongware] Most Valuable Participant

                                                Welcome to the Wonderful World of Scripting! Tinkering with existing scripts, then combining them (or at least trying to ), is a good way of getting to grips with various Javascript constructions and the Adobe Document Object Model.

                                                 

                                                I see lots of repeating lines; well, let me introduce you to my good friend function. Rather than writing the same rename-line again and again, with different parameters, you can add this at the end of your first script:

                                                 

                                                function renameSwatch (from, to)
                                                {
                                                
                                                   var anyOldSwatch = myDoc.swatches.itemByName(from);
                                                   if (anyOldSwatch != null)
                                                     anyOldSwatch.name = to;
                                                }
                                                

                                                 

                                                 

                                                so all you have to do is repeat this line, with the correct values:

                                                 

                                                renameSwatch ("Corporate Red", "C=10 M=100 Y=100 K=0");

                                                 

                                                You can even combine the find-assign-set new name new values stuff:

                                                 

                                                 

                                                function renameSwatch (fromC, fromM, fromY, fromK, toC, toM, toY, toK, toName)
                                                {
                                                   var i;
                                                   for (i=2; i<myDoc.colors.length; i++)
                                                  {
                                                    if (myDoc.colors[i].colorValue[0] == fromC &&
                                                      myDoc.colors[i].colorValue[1] == fromM &&
                                                      myDoc.colors[i].colorValue[2] == fromY &&
                                                      myDoc.colors[i].colorValue[3] == fromK)
                                                   {
                                                
                                                     myDoc.colors[i].name = toName;
                                                     myDoc.colors[i].colorValue = [ toC, toM, toY, toK ];
                                                     return;
                                                   }
                                                // .. not found -- perhaps issue an alert?
                                                }
                                                

                                                 

                                                 

                                                and call it like this (again, with your correct values of course):

                                                 

                                                renameSwatch (50, 0, 10, 0, 10,100,100,0, "Ruby Red");

                                                 

                                                Your Script #2 also shows you how can build an array of change from/change to values and loop over that, which might be preferable if you have lots and lots of change pairs -- it's slightly easier to maintain.

                                                 

                                                [Ed.: Oops. Spotted a typo in my code. Okay, fair warning: written from memory and untested!)

                                                • 21. Re: Convert one swatch value to another
                                                  Ulrick R Mortensen Level 1

                                                  Sorry John.

                                                   

                                                  But as you've probably guessed I'm VERY new at this.

                                                  Trying to come grips with it all. Don't know anything about scripting. I'm just trying to find my way around in here, and finding the scripts/help that can get me moving on with my project.

                                                   

                                                  You gave me a couple of lines that I can go forward with. Thank you for that.

                                                   

                                                  And hey. Everybody starts as a newbie! :-)

                                                   

                                                  Are you trying to be deeply mysterious here? If you tell us you get an error but don't tell us what it is, it really doesn't give us much to go on. "Did you try throwing your InDesign distribution CDs out the window like a frisbee? It might solve the error!"

                                                   

                                                  Also, which if statement? This one?:

                                                   

                                                  I did make an edit about 2 minutes later stating that it was ok. Was that not the correct way to do it?

                                                   

                                                  Sorry if I upset you.

                                                   

                                                   

                                                  @Jong.

                                                  Thanks again man.

                                                   

                                                  I'll clean the script up, and try again.

                                                  • 22. Re: Convert one swatch value to another
                                                    Ulrick R Mortensen Level 1

                                                    Thank you Jong.

                                                     

                                                    Very helpful.

                                                     

                                                    U

                                                    • 23. Re: Convert one swatch value to another
                                                      John Hawkinson Level 5
                                                      I did make an edit about 2 minutes later stating that it was ok. Was that not the correct way to do it?

                                                      My response was composed before I saw your edit.

                                                       

                                                      But seriously, just because you're new doesn't mean you can succesfully carry off the "deeply mysterious" mode...in general, when asking questions, regardless of your level of experience or the forum, try to think about whether you've given the people you are asking sufficient information to answer your question.

                                                       


                                                       

                                                      Incidently, in re Jongware's comment about script #2 and maintainability, instead of building a Find array and a Change array and changing Find[i] to Change[i], you are much better off building a single array, e.g. FC:

                                                       

                                                      var FC = [ [[10,0,50,0], [10,0,50,0]],
                                                           [[30,0,100,0], [40,0,100,0]],
                                                           ...
                                                      

                                                       

                                                      and then iterating over it and changing FC[i][0] to FC[i][1].

                                                       

                                                      Otherwise you run the serious risk of dropping/adding a line along the way and having the arrays fall out of sync. Addtionally, it becomes clear on inspection what is changing to what, without giving your neck whiplash and having to count carefully.

                                                       

                                                      p.s.: I'm a card carrying member of the Campaign to Stamp Out "my" from Variables.

                                                      • 24. Re: Convert one swatch value to another
                                                        [Jongware] Most Valuable Participant

                                                        Yes, agreed on the array-inside-arrays construction. It's ever so slightly more readable, and you cannot forget a closing ] as that will immediately signal a syntax error. It's much easier to check for missing numbers as well; if you have an array yourArray = [ 1, 2, 3 ] and you try to read yourArray[3], you get null as a return value, which is illegal in most operations.

                                                         

                                                        p.s.: I'm a card carriying member of theCampaign to Stamp Out "my" from Variables.

                                                         

                                                        I think it's to ensure beginners don't accidentally use reserved words for variables or functions. I started out like that as well, but it'll wear off after a while.

                                                        (I once found, after a lot of pointless debugging, a mysteriously failing script of mine contained a variable named 'delete'. It's not as if that's even used in Javascript!)