14 Replies Latest reply on Jan 25, 2013 2:07 AM by Pickory

    [JS] MenuItem Life Cycle in CS6

    Marc Autret Level 4

      Hi guys,

       

      I have a set of scripts that offer a mechanism to populate a dedicated menu so that the user can run these scripts from the menu bar.

       

      The system is based on a startup script—say menuloader.jsx—which is responsible for checking and feeding the desired MenuItem instances within that submenu. Each related script menu action is based on a onInvoke listener connected to a File—as already discussed in another thread: http://forums.adobe.com/message/4089126#4089126

       

      While my menuloader seems to properly work from ID CS4 to CS6, a user reported a strange fact in Mac OS 10.7.5 / InDesign 8.0.1. After some tests we discovered that the whole menu stuff (i.e. the dedicated Submenu and all included MenuItems) is entirely restored by InDesign during startup, making my menuloader simply useless! (At the moment this even makes it fail, as I had not anticipated such a situation can occur.)

       

      This issue is not technically severe in that I can adapt my code without great difficulty. Anyway this seems to raise a serious question.

      To date I always assumed that:

      1. Custom submenus were application-persistent (which is still true here, of course!)

      but

      2. Custom menuitems were only session-persistent.

       

      From what I've experienced clause #2 is generally true (and that's the reason why we are used to implement menuloaders). But I don't understand under which condition scripted menuitems may become app-persistent. Did I miss some obvious ID preference settings or something?

       

      Thanks for any hint or clarification.

       

      @+

      Marc

        • 1. Re: [JS] MenuItem Life Cycle in CS6
          absqua Level 4

          I don't know whether InDesign uses the application state-saving and resuming feature introduced in Lion. I can't test this for you myself, but a little googling makes me think it might be a fruitful avenue for troubleshooting.

           

          Jeff

          1 person found this helpful
          • 2. Re: [JS] MenuItem Life Cycle in CS6
            Marc Autret Level 4

            Hi Jeff,

             

            Thanks for the advice. It sounds very relevant indeed.

            I'll post details if I get sth on this side.

             

            @+

            Marc

            • 4. Re: [JS] MenuItem Life Cycle in CS6
              Marc Autret Level 4

              The main problem is that I cannot manage to produce the issue on my own OS 10.7 platform, whatever the system settings.

               

              Very intriguing.

               

              @+

              Marc

              • 5. Re: [JS] MenuItem Life Cycle in CS6
                Larry G. Schneider Adobe Community Professional & MVP

                Then it may well be just in OSX 10.8 if you can't make it fail in 10.7.

                • 6. Re: [JS] MenuItem Life Cycle in CS6
                  Marc Autret Level 4

                  But the user reported that bug in Mac OS 10.7.5 / InDesign 8.0.1...

                  • 7. Re: [JS] MenuItem Life Cycle in CS6
                    Larry G. Schneider Adobe Community Professional & MVP

                    Are they running as an Administrator? Or are you?

                    • 8. Re: [JS] MenuItem Life Cycle in CS6
                      Marc Autret Level 4

                      Larry G. Schneider wrote:

                       

                      Are they running as an Administrator? Or are you?

                       

                      Good question! I do run as an admin, but maybe the user does not.

                      Gonna ask.

                       

                      Thanks!

                       

                      @+

                      Marc

                       

                      EDIT: For those who are interested in testing the issue, the script in question is HurryCover PRO 2.017, but the TRY version is based on the same menu-loading mechanism:

                      http://www.indiscripts.com/blog/public/scripts/HurryCoverTry.zip (ZIP)

                      http://www.indiscripts.com/blog/public/scripts/en_HurryCover-Manual.pdf (User's guide PDF)

                      http://www.indiscripts.com/category/projects/HurryCover (Overview)

                      • 9. Re: [JS] MenuItem Life Cycle in CS6
                        Pickory Level 3

                        FYI.

                         

                        I just had to compile one of our plugins for ID Server. For IDS we remove all UI, including menus. I do my intital testing in ID desktop. I was supprised to find our menu items were still visible, although when selected did nothing. I had to cmd + shift + alt +ctrl launch InDesign before the menue items dissappered.

                         

                        Tested in Win XP, InDesign CS6 Desktop ( not patched ).

                         

                        Edited to say. Oviously the plugin had been loaded previousely with full UI.

                         

                        P.

                        • 10. Re: [JS] MenuItem Life Cycle in CS6
                          Marc Autret Level 4

                          Hi Pickory,

                           

                          Thanks for your input. I don't know how custom menus and plug-ins are built for IDS. In ID, any Menu object—in fact, Submenu—that a script creates is application-persistent. This is a pretty confusing for beginners, because by contrast the MenuItem objects are (usually!) not application-persistent. In other words, ID remembers menus and submenus, but it doesn't remember the menuitems which belong to them. So, without a specific mechanism to restore menuitems, ID would starts up with residual empty menus that only behave as placeholders. (Of course if the user reset the app preferences by cmd shift alt during startup, any custom menu itself will be removed.)

                           

                          My reasoning is that the structure of the custom menus is cached in the InDesign SavedData file, so that ID can restore positions and titles during startup. But menuitems cannot be registered this way, as they require links to menu actions which are basically volatile, either script files or session-scoped functions. This is the reason, I suppose, why menuitems must be restored—in fact, entirely re-built—by our own means when the app starts up. That's the purpose of menu loaders.

                           

                          My problem here is that my menuitems are restored from the outside of my menu loader. (Or at least, things happen as if this were the case...)

                           

                          @+

                          Marc

                          • 11. Re: [JS] MenuItem Life Cycle in CS6
                            Pickory Level 3

                            Hello Marc,

                             

                            The two menu items our plugin added were the standard about and a preference menu item that appeares when you select Edit->preferences, in Win ID desktop. As far as plugins are concerned this might be becasue when I removed all of the UI the plugin was left without a resource, so it might be a ODFRC problem.

                             

                            When I replaced the plugins in the plugins folder I expected the menus to appear or dissapear depending on which plugin was loaded.

                             

                            Of course, I might have completely broken our plugin.

                             

                            I will need to do some more testing.

                             

                            P.

                            • 12. Re: [JS] MenuItem Life Cycle in CS6
                              Dirk Becker Level 4

                              Script Menu Actions should survive a restart so the user can apply a keyboard shortcut.

                              You'd either bind a script file to them or find and reattach the onInvoke methods during startup.

                              At least that's my theory, I do more server scripting than UI.

                               

                              If the menu action does not exist after restart, you might watch out for an interfering startup script which uses something like app.scriptMenuActions.everyItem().remove() to "clean up" beyond its scope. Let the user give you the output of app.scriptMenuActions.everyItem().name, that may be a hint. Then track down by temporarily disabling the other scripts.

                               

                              Dirk

                              1 person found this helpful
                              • 13. Re: [JS] MenuItem Life Cycle in CS6
                                Marc Autret Level 4

                                Thanks a lot, Dirk!

                                 

                                > Script Menu Actions should survive a restart [...]

                                 

                                Your theory is correct! Never realized that before: InDesign seem to keep in cache the script menu actions themselves. And during the startup sequence, a kind of "garbage collection" is processed so that menu actions which are not bound anymore are finally destroyed. I don't know the exact order how this operates. Testing with a simple startup script showMenuActions.jsx:

                                 

                                alert( app.scriptMenuActions.everyItem().name );

                                 

                                we can at least identify the following steps during a regular startup:

                                 

                                1. Custom menus/submenus are restored.

                                2. Script menu actions are temporarily restored too, as shown by showMenuActions.jsx.

                                3. Unless a menuloader is processed (that rebuild menu items and bind them to actions), custom menu items are lost.

                                4. Then, menu actions which are not bound anymore are finally removed.

                                 

                                To evidence step #4, I manually re-run showMenuActions.jsx after the startup is completed, and it appears that menu actions shown at step #2 are gone (except those which are still connected, of course).

                                 

                                > If the menu action does not exist after restart, you might watch out for an interfering

                                > startup script which uses something like app.scriptMenuActions.everyItem().remove()

                                > to "clean up" beyond its scope

                                 

                                Yes, I now seriously suspect something like this, and/or interference with menu items themselves. In both cases the crucial point is that we cannot manage this from our own menuloader! If a third-party has a startup script that kill everything before it installs its own stuff, and if that startup script is processed after mine, there is no way to guarantee users my own menu items are properly managed.

                                 

                                So, wouldn't it be useful to draw up a Code of Good Startup-Script Practices between script/plug-in producers?

                                 

                                @+

                                Marc

                                • 14. Re: [JS] MenuItem Life Cycle in CS6
                                  Pickory Level 3

                                  I think some thing is broken.

                                   

                                  I have just compiled the BasicDialog sample plugin and loaded it. Two menus appear, one under the SDK menu and the other in the about.

                                   

                                  I then loaded the plugin with the MenuDef and ActionDef commented out. The menus still appeared and still worked.

                                   

                                  I then commented out the ClassDescriptionTable. The menus appeared but did not work.

                                   

                                  I did this on Win XP.

                                   

                                  Odd.

                                   

                                  P.