1 Reply Latest reply on Jun 28, 2017 11:58 PM by Silly-V

    Unwelcome surprise: "Find Style menu item" app.executeMenuCommand() vs Action

    Silly-V Adobe Community Professional

      I was having some challenges involving graphic styles and found one of these amazing curious features of AI scripting.

      Believe it or not, the action and the scripting app.executeMenuCommand methods of doing the same thing "Find Style menu item" do not produce the same exact result. This is the second time I have observed the scripting command perform inferior to Actions.

       

      Here is what happens:

      With the Action you get the same exact result as with UI operation. If there are paths with a graphic style on them and they are grouped in a group, only the paths will get the selection on their circles in the layers panel. The parent group has no style on it, so its circle is left alone.

      Screen Shot 2017-06-28 at 2.02.40 AM.png

      When you use app.executeMenuCommand, you don't have to load an action and bother with all that junk, but how inconvenient, the un-styled parent group ends up selected as well! This is a big problem because when you're doing graphic style replacement by applying a new style to a selection based on an old style, this will end up applying the style to the group and not the contents.

      Screen Shot 2017-06-28 at 2.03.07 AM.png

      Rather unfortunate, so I'm going to use Actions for my task.

       

      Of course, working with graphic styles is a special level of it's own in AI scripting: you can't tell which style is applied to anything via any properties. No way to see which style is currently selected in the graphic styles panel. But styles can be applied to a pageItem, and the selection of the graphic styles panel can be set by applying a style to a pageItem. Also it's possible to detect what style is applied to an item by either duplicating a style (via action) which is applied to a pageItem or even pasting the pageItem into a new document without styles in order to record the newly appeared style.
      If all that wasn't already a mess, the same-selection issue is a nice new banana peel to slip on.

        • 1. Re: Unwelcome surprise: "Find Style menu item" app.executeMenuCommand() vs Action
          Silly-V Adobe Community Professional

          Continuing this work, I have found the hard way that I was jumping out of the frying pan and into the fire when trying to use those Actions! Used within the script, the action-selected selection would be extremely unreliable and would disappear most of the time.

          Then, I discovered this difference between graphic styles on paths and graphic styles on groups. It appears that Select same Graphic Style will select paths with a certain style, but never the groups. To select groups which have a graphic style applied, the item Select same Appearance works. Therefore, if I used "Find Style menu item" to get the same style art, the only time the parent group would be part of the selection is when all of the items inside have the style. This makes it much better: now I get an art object array from the selection using a recursive function and just use style.applyTo on the items which are not groups. Next step would be to select and replace the same style on all the groups. Using "Find Appearance menu item", it selects the groups which will have the graphic style applied to it which I want to replace, and as such all of the group's members are selected too. What if a member of such a group was also a group with the style on it? The document selection in this case goes to the topmost hierarchy, producing an array of non-nested groups which have the style on them. Since my goal is replacing a style, I can use a while loop checking for document selection being at null or 0 length and using Appearance menu item command to check for styled groups. As the styles get replaced, the menu item command will find nested styled groups, if any, all the way down.

          This is how I now achieve graphic style replacement in a document: first get a selection of the Style menu item and apply the new style to all items; second, get a selection of Appearance menu item and keep replacing the style of selection's top-level groups and re-selecting via Appearance menu item until there is no selection.

           

          I take back almost everything I said about app.executeMenuCommand, may it please have mercy on my transgressions.