3 Replies Latest reply on Apr 9, 2009 1:53 AM by [Jongware]

    Script to select a color swatch?


      What is the JavaScript code to select a color swatch in InDesign CS4, assuming I have the zero based index of the color swatch I want to select?




        • 1. Re: Script to select a color swatch?
          [Jongware] Most Valuable Participant

          There is no such thing.


          JavaScript does not control or mimick the user interface; rather, it works directly with objects in the documents and application itself. Instead of selecting an object and applying a swatch, you can apply any swatch directly to any object. The swatches themselves are objects, but not selectable ones -- you can only select document objects.


          However: if you have the reference to a swatch (for example, by index), you can manipulate it: duplicate, remove, change its color, etc.


          I assume your goal is not to select a swatch as such, but rather do something to/with one. If you explain a bit more, there might be a more conventional way to do what you intend.

          • 2. Re: Script to select a color swatch?
            plug_in_guy Level 1



            Thanks for your post. I understand what you are saying and perhaps I wasn't clear enough in explaining what I needed. I wanted to select a color swatch so that the next object the user drew would be the color my script selected. I discovered and settled on the following after much pain:


            var myDoc=app.activeDocument;

            myDoc.selection = NothingEnum.nothing;            // deselect all objects to prevent replacing the color of an object that might be selected already
            myDoc.pageItemDefaults.strokeColor = "None";   // set document default stroke color to "None"
            myDoc.pageItemDefaults.fillColor = "red";            // set document default fill color to "red" where "red" is the name of the color I wanted

            This might "encourage" (or make it easier for) the user to draw objects in the color I've left him/her auto-selected to.


            Thanks again,


            • 3. Re: Script to select a color swatch?
              [Jongware] Most Valuable Participant

              Yes, I see what you intend.

              Setting the default fill and stroke does the trick -- that's entirely similar to what happens in the UI as well.


              You could add this a refinement: if the user has one or more objects selected, you could apply the color. Something like


              if (app.selection.length > 0)
              for (var i=0; i<app.selection.length; i++)
                app.selection[i].fillColor = app.activeDocument.swatches[myFill];
                app.selection[i].strokeColor = app.activeDocument.swatches[myStroke];