5 Replies Latest reply on May 13, 2016 8:53 AM by Silly-V

    How to Tell if Swatches in a Swatch Group are Selected


      Hello. I am trying to figure out how to get the selected swatches in a particular swatch group. Currently I have managed to get the swatches in the swatch group with:

      docRef.swatchGroups.getByName("Shirt Swatches").getAllSwatches()

      but from there I can't seem to only get the ones that are sorted. I tried a loop with docRef.swatchGroups.getByName("Shirt Swatches").[i].selected but every result came back as undefined. I don't want to use docRef.swatches.getSelected() because I only want the selected swatches in my swatch group "Shirt Swatches". Alternatively if there was a way to tell if any selected swatch was in my "Shirt Swatches" group I guess that would work but I don't want to write a function that compares each selected swatch to each swatch in the Shirt swatches group because it seems like there should be a more efficient way to do this but if I have to I will.


      This seems like a really easy problem with a basic solution but maybe it's not. Any suggestions?

        • 1. Re: How to Tell if Swatches in a Swatch Group are Selected
          CarlosCanto Adobe Community Professional & MVP

          there's no direct access to selected swatches in a swatch group, one way to workaround that limitation is to get the swatches in the Shirts group and turn the array into a string, then you can search each selected color against the shirts swatches string.


          function main () {
              var idoc = app.activeDocument;
              var selectedsw = idoc.swatches.getSelected();
              var shirtSwatches = idoc.swatchGroups['Shirt Swatches'].getAllSwatches().toString();
              for (var a=0; a<selectedsw.length; a++) {
                  idx = shirtSwatches.indexOf (selectedsw[a].name);
                  if (idx < 0) {
                      alert(selectedsw[a].name + '\nis NOT in Shirts SwatchGroup');
                  else {
                      alert(selectedsw[a].name + '\nIS in Shirts SwatchGroup');
          main ();
          • 2. Re: How to Tell if Swatches in a Swatch Group are Selected
            Silly-V Adobe Community Professional

            I happened to just be messing with this yesterday - you know what else I found out that I hate, is how removing a swatch group removes all the same-named swatches outside of the group.

            • 3. Re: How to Tell if Swatches in a Swatch Group are Selected
              CarlosCanto Adobe Community Professional & MVP

              oh no, perhaps you can rename all group swatches before removing?

              • 4. Re: How to Tell if Swatches in a Swatch Group are Selected
                mollyh20178705 Level 1

                That's what I was worried I would have to do but I figured I would see if anyone knew a more direct route. Thanks for the help.

                • 5. Re: How to Tell if Swatches in a Swatch Group are Selected
                  Silly-V Adobe Community Professional

                  Well, how about that. Check this out, the spot colors (global-process is just a spot that can't be Lab far as I can tell), if you rename or remove this swatch, it just gets globally removed or renamed. The swatches collection is quite weird, I think by how the collection is not like layers or pageItems, the swatch palette view is a UI-level representation of a whole different mechanism underneath.


                  For example, if I took my spot swatch and slid the tint down to 33%, and make a new swatch out of it, the name in the color display will still show the swatch name as original, but with the slider info updated to show the correct tint. Hover over the same swatch in the swatches panel and the preview name pops up with the "33%" added to the tail end. Do an alert of all the swatch names when iterating over the array which is the result of swatchGroup.getAllSwatches(), and it will simply show the original swatch name twice for the two different tint swatches.


                  2016-05-13 10_09_41-.png 2016-05-13 10_10_07-.png



                  So it appears that manipulating the swatch panel is probably much easier than Apparance panel, but at the same time is also accomplished by 'marionette-ing' related commands to do what we want.


                  In my case, I was working on a Swatch legend renderer, and I wanted to take advantage of Actions to create a swatch group from a selection of art (the resulting swatches stay selected, at least) and do my swatch legend from that group. In order to solve my problem of my clean-up cleaning too much up, I gathered the getAllSwatches() array to obtain the swatch objects with all their related color component objects (they stick around after what I do next) immediately after the actions created the new group and then quickly use a "Delete Selected Swatches" action to remove my entire temp group. (apparently doing other things on my document had caused this selection inside the swatch panel to disappear).


                  Does it work? It works great on spot colors when applying colors based on the swatch name, but does not flow so easy with pure process colors. However, I have a plan to deal with those.

                  Process colors do not really care about names, even though you may give them one, and when a swatch group is created from artwork which has a process color, it will be assigned a default-convention process color name such as "R=0 G=0 B=0". Even if you're using a  process swatch named "Black", it will create one called "R=0 G=0 B=0".


                  Oh well, I can live with that, everyone that needs such legends is using spot colors anyway. Plus, you can have strange situations where you can have two swatches which have the same name but all different compositions. Normally, you could not have any swatch with a duplicate name as it has a warning dialog that doesn't let you add a new swatch and name is an existing name. But you can go after the fact into the swatch options menu and rename any swatch to an existing Process swatch name. (You can't do this with any of the spot swatches though) So you can actually have an orange color and a purple color of either process or spot varieties, and they can both end up being named "Black".