10 Replies Latest reply on Jun 4, 2010 5:16 AM by PaulR72

    [CS4] [JS] Which item does a panel context menu action work on?

    PaulR72

      Hi there,

       

      I'm trying to write a script that operates on a paragraph style. But rather than go through InDesign's objects to find the paragraph style, I want the user to select it by right clicking it in the styles panel.

       

      This (working) code adds a menu item to the Paragraph Styles Context Menu (thanks Jongware, Steven and Harbs for your help with this last week):

       

         #targetengine "session"

         aScriptAction = app.scriptMenuActions.add("Browse");
         aEventListener = aScriptAction.eventListeners.add("onInvoke", aEventHandler, false); 
         aMenu = app.menus.item("$ID/RtMenuStyleListItem");   // = "Text Style List Context Menu" menu text in InDesign English version.
         aCustomMenuItem = aMenu.menuItems.add(aScriptAction);

       

      aEventHandler() is called properly when I right-click on a paragraph style in the panel, and then select the "Browse" option created above. So far so good.

       

      But the problem now is that I can't find the index or id of the paragraph style in the panel that was right-clicked on. An event object is passed to aEventHandler(), but I can't find any variables or methods in this event, or within any of the objects it points to (say, with Event.currentTarget or Event.target), that can give this information. Attempted many searches on the net too, no result.

       

      Hope someone can give me a pointer!

       

      Bye, Paul

        • 1. Re: [CS4] [JS] Which item does a panel context menu action work on?
          Steven.. Level 3

          I don't know if it is available. In C++ it would be in the "this" property, because the event calling the menu is attached to listbox item, but I don't know how it would appear in Javascript

          • 2. Re: [CS4] [JS] Which item does a panel context menu action work on?
            [Jongware] Most Valuable Participant

            Probably a ScriptMenuAction ("A script menu action. Base Class: MenuAction").

             

            A MenuItem has a MenuAction, with which it is "associated". The parent of a MenuAction is the Application itself, so I think this means it has been implemented as an internal "list of functions" which can then be activated by a menu item. In other words: the Item is not the Action, you have to associate them. (That's not un-like the hot key system; in fact, it might be more than superficially similar.)

             

            Checking MenuAction in my own version of the Help, I immediately noticed it's a superclass of "ScriptMenuAction", and since that has a list of properties that smell like meta-scripting (event listeners and such), I guess that's what one ought to use.

            • 3. Re: [CS4] [JS] Which item does a panel context menu action work on?
              Harbs. Level 6

              I don't believe panels are scriptable in this way.

               

              Make a feature request...

               

              Harbs

              1 person found this helpful
              • 4. Re: [CS4] [JS] Which item does a panel context menu action work on?
                [Jongware] Most Valuable Participant

                Fascinating. It seems there are more than 3,200 "menu actions" in InDesign! That's 73 letter size pages ...

                 

                (Although your number may vary: a large part of the list seems to be the full font list, including all its bold and italic etc. variants. Still an impressive list, tho'.)

                 

                This quick hack puts them into a selected text frame:

                 

                res = [];
                for (a=0; a<app.menuActions.length; a++)
                 res.push (app.menuActions[a].name+" ("+app.menuActions[a].area+")");
                app.selection[0].contents = res.join("\r");
                
                • 5. Re: [CS4] [JS] Which item does a panel context menu action work on?
                  PaulR72 Level 1

                  Thanks Steven, Harbs and Jongware. Harbs -- I may well make a scripting engine suggestion, that panel context menus be able to identify the item that was right-clicked on: it would be incredibly useful for scripts that operate on styles and swatches.

                  • 6. Re: [CS4] [JS] Which item does a panel context menu action work on?
                    [Jongware] Most Valuable Participant

                    Pfff. It took half an hour and now I have 10 "Browse" items in my Paragraph panel, but I think I got it.

                     

                    This event handler will show the name of the current selected paragraph. Oh sure, it's a dirty trick, but hey! it works!

                     

                    function aEventHandler (parent)
                    {
                     m = app.menus.item("Text Style List Context Menu");
                     alert (m.menuItems[0].name.match(/"(.+)"/)[1] );
                    }

                     

                    I noticed too late I could have used the $ID/xxx code but I'm not running it again. How in the world do I get rid of all those "Browse" items ...

                    • 7. Re: [CS4] [JS] Which item does a panel context menu action work on?
                      Harbs. Level 6

                      Hey Jong,

                       

                      Nice thinking! It'll work as long as all the style names are unique...

                       

                      Your extra menu items should disappear when you restart InDesign twice.

                       


                      Harbs

                      • 8. Re: [CS4] [JS] Which item does a panel context menu action work on?
                        PaulR72 Level 1

                        Hiya Jongware,

                         

                        That is absolutely brilliant. Who cares if it's cheating, so long as it works?

                         

                        Thanks a lot.

                         

                        Paul

                        • 9. Re: [CS4] [JS] Which item does a panel context menu action work on?
                          [Jongware] Most Valuable Participant

                          As a dirty trick, it comes with a cave constructor ("watch out, programmer").

                           

                          As Harbs pointed out, it'll not work if you have the same style name in another group ... I never use groups so I didn't realize it, but it seems to be perfectly possible.

                          Another one is, for some reason it's all out of whack if you right-click a group header. It simply repeats the name of the last style you right-clicked.

                           

                          Oh -- and, quite unexpectedly really, I tested with the paragraph styles but it appears the character style panel uses the exact same menu!

                          • 10. Re: [CS4] [JS] Which item does a panel context menu action work on?
                            PaulR72 Level 1

                            Jongware -- it's not the sort of get around you could use for scripts meant for the public. But for private use, it's perfect.

                             

                            I don't use style groups. Don't know about CS4, but with CS3, if you cut and paste something from another doc in the same book (something I need to do a lot) or import more text from Word, the new styles won't match up with the existing ones in the groups: they'd be duplicated as single styles in no group. So it made the feature useless, for me, because of the fuss required to delete them and replace them with their twins.

                             

                            I really like your work around. The character styles thing doesn't matter. I just did a try {} catch to see if the style was para or char.

                             

                            The script I wrote is a simple one. It just copies the style to the find dialog box settings, then zooms to the first instance of it. Not much in itself, but great when you first import Word text that has 40 styles and you need to get an idea of how each style is used without scrolling painfully through the long list in the find dialog.

                             

                            ... so thanks for your help.