13 Replies Latest reply on Jun 11, 2017 11:04 PM by Laubender

    invoking menu - language

    Pickory Level 3

      Hello,

       

      Does invoking a menu with the menu string work in any language? Example.

      $ID/Override All Master Page Items

       

      P.

        • 1. Re: invoking menu - language
          Laubender Adobe Community Professional & MVP

          Hi Pickory,

          it should, if the menu is available in the situation you want to use it.
          E.g "Override All Master Page Items" is not available if no document is open.

           

          See for example:

           

          var menuAction = app.menuActions.itemByName("$ID/Override All Master Page Items");
          
          for(x in menuAction)
          {
              $.writeln(x+"\t"+menuAction[x]);
             
          };
          
          // CASE 1 No document open with my German InDesign
          
          /*
             
             
              name    Alle Musterseitenobjekte übergehen
              title    Alle Musterseitenobjekte übergehen
              area    Bedienfeldmenüs:Seiten
             
              enabled    false
             
              checked    false
              id    6164
              label   
              isValid    true
              parent    [object Application]
              index    101
              properties    [object Object]
              events    [object Events]
              eventListeners    [object EventListeners]
              isValid    true
             
          */
          
          // CASE 2 Document open with my German InDesign.
          /*
             
              name    Alle Musterseitenobjekte übergehen
              title    Alle Musterseitenobjekte übergehen
              area    Bedienfeldmenüs:Seiten
             
              enabled    true
             
              checked    false
              id    6164
              label   
              isValid    true
              parent    [object Application]
              index    101
              properties    [object Object]
              events    [object Events]
              eventListeners    [object EventListeners]
              isValid    true
             
          */
          

           

          So before using it you could look after the value of property enabled.

          Maybe also isValid if you are not sure if a specific InDesign version does provide the menu.

           

          var menuAction = app.menuActions.itemByName("$ID/Override All Master Page Items");
          if(menuAction.enabled){menuAction.invoke()};
          

           

          ( Just for lurkers: InDesign Server does not support menuActions )

           

          Regards,
          Uwe

          1 person found this helpful
          • 2. Re: invoking menu - language
            tpk1982 Level 4

            I hope you asking the menu action work in all scripting language?

            • 3. Re: invoking menu - language
              Laubender Adobe Community Professional & MVP

              I should have added:


              It should work as long as you provide the locale-independent form of the string.

               

              Sometimes simply adding $ID/ in front of the English menu name does not work or is not enough.
              Better look for the title string of a menu and check with:

               

              app.findKeyStrings("menu-title-name");
              

               

              for the "universal" form(s) of the string.

               

              Regards,
              Uwe

              • 4. Re: invoking menu - language
                robinfredericf

                Hi, I have some trouble trying to use universal menu strings:

                 

                Some confrere submitted me a script idea, showing me a manual trick that he had designed to selectively get rid of specific unwanted style replacements in paragraphs while preserving other replacements:
                http://robinfredericf.free.fr/misc/wadym-s_Idea.avi.mp4
                http://robinfredericf.free.fr/misc/wadym-s_idea.png

                I was interested in automating it in a script but his method uses the command "Redefine style" that doesn't exists in the InDesigns ExtendScript API, so I tried to «invoke()» it, following explanations that I read on adobe forum topics such as
                https://forums.adobe.com/thread/1961958
                http://kasyan.ho.com.ua/open_menu_item.html
                https://forums.adobe.com/thread/892886

                 

                To get the required command to redefine a paragraph style from the selection, in a international reliable way, I decided at once to avoid to get the menuAction by its id or index because it seemed hazardous, those numbers could be assigned to totally different actions in different InDesign version, so i tried:

                var redefineParagraphStyle = app.menus.item('$ID/ParaStylePanelPopup').menuItems.item('$ID/Redefine Style'); 
                redefineParagraphStyle.associatedMenuAction.invoke();

                [including jsx and demo idml file:]
                http://robinfredericf.free.fr/misc/wadyms-paragraphstyle-trick_indd002.zip
                http://robinfredericf.free.fr/misc/wadyms-paragraphstyle-trick_indd002-en.zip

                I expected that the use of "$ID/" english strings would prevent the localization problems but unfortunately I was surprised when my colleague told me that he got the error «ReferenceError: Object is invalid»

                So then I tried:

                var redefineParagraphStyle = app.menus.item('$ID/ParaStylePanelPopup').menuItems.item('$ID/RedefineStyle'); 
                var redefineParagraphStyle = app.menus.item('$ID/ParaStylePanelPopup').menuItems.itemByID(8458);
                var redefineParagraphStyle = app.menus[113].menuItems[3];

                http://robinfredericf.free.fr/misc/wadyms-paragraphstyle-trick_indd002a.zip

                All methods worked on my side, tested on French localized versions of InDesign CS5 & CC 2015.4 under MacOSX 10.10.5 & Windows 7

                But still the same "Object is invalid" error on my Ukrainian colleague's side, who tested the scripts on Russian localized versions of CS5.5 &  CC 2015.2 (11.2.0.100х64) under Windows 7.

                • 5. Re: invoking menu - language
                  TᴀW Adobe Community Professional & MVP

                  I haven't tested anything, but IIRC some menu items are only valid when the relevant panel is open. So if the paragraph styles panel is closed on your friend's computer, he may get an error.

                   

                  Ariel

                  • 6. Re: invoking menu - language
                    Laubender Adobe Community Professional & MVP

                    Hi,

                    try this:

                     

                    var menuItem =
                    app.menus.itemByName("$ID/ParaStylePanelPopup").
                    menuItems.itemByName("$ID/Redefine Style");
                    
                    if(menuItem.enabled)
                    {
                        menuItem.associatedMenuAction.invoke();
                    };
                    

                     

                    Regards,
                    Uwe

                    • 7. Re: invoking menu - language
                      Laubender Adobe Community Professional & MVP

                      Hi Ariel,

                      in this case the associatedMenuAction should also work, if the panel is not open.

                      However it can only work, if it is enabled.
                      And it is only enabled if:

                       

                      1. Text is selected
                      &&

                      2. Text is showing overrides on the applied paragraph style

                       

                      Regards,
                      Uwe

                      • 8. Re: invoking menu - language
                        TᴀW Adobe Community Professional & MVP

                        Laubender  wrote

                         

                        in this case the associatedMenuAction should also work, if the panel is not open.

                        However it can only work, if it is enabled.
                        And it is only enabled if:

                         

                        1. Text is selected
                        &&

                        2. Text is showing overrides on the applied paragraph style

                         

                        Okay, sounds right! I didn't go into the specific details of the case as you have (thanks for that!) -- but the principle is important: There is such a thing as a menuAction not being enabled depending on the UI configuration (either a closed panel, or as you rightly point out in this case, no text selected, etc.). So this could be Robin's problem.

                         

                        Ariel

                        • 9. Re: invoking menu - language
                          Laubender Adobe Community Professional & MVP

                          Some other details:

                           

                          // My German string for "Redefine Style":
                          var localeKeyString = "Format neu definieren";
                          var foundKeyStrings = app.findKeyStrings(localeKeyString);
                          
                          /*
                              $ID/Redefine Style
                              $ID/RedefineStyle
                              $ID/RedefineObjectStyles
                              $ID/ObjStylesContextRedefine
                          */
                          
                          // My German InDesign CS6 lists 9 entries of a menu action named "$ID/Redefine Style".
                          // The same 9 entries are also listed with: "$ID/RedefineStyle", "$ID/RedefineObjectStyles" and "$ID/ObjStylesContextRedefine"
                          
                          // Results listed below when a document is open and nothing is selected:
                          var menuActions = app.menuActions.itemByName( foundKeyStrings[0] ).getElements();
                          
                          for(var n=0;n<menuActions.length;n++)
                          {
                              $.writeln(menuActions[n].enabled+"\t"+"\t"+menuActions[n].id+"\t"+menuActions[n].area+"\t"+menuActions[n].title);
                          };
                          
                          /*
                              false        8458    Bedienfeldmenüs:Absatzformate    Format neu definieren
                              false        8459    Bedienfeldmenüs:Zeichenformate    Format neu definieren
                              false        8486    Bedienfeldmenüs:Absatzformate    Format neu definieren
                              true        113158    Bedienfeldmenüs:Objektformate    Format neu definieren
                              false        113176    Bedienfeldmenüs:Objektformate    Format neu definieren
                              false        132102    Bedienfeldmenüs:Tabellenformate    Format neu definieren
                              false        132126    Bedienfeldmenüs:Zellenformate    Format neu definieren
                              false        132151    Bedienfeldmenüs:Tabellenformate    Format neu definieren
                              false        132152    Bedienfeldmenüs:Zellenformate    Format neu definieren
                              
                          
                              Bedienfeldmenüs > panel menus
                              Format neu definieren > Redefine Style
                              
                              Absatzformate > paragraph styles
                              Zeichenformate > character styles
                              Objektformate > object styles
                              Tabellenformate > table styles
                              Zellenformate > cell styles
                          */
                          

                           

                          Hope, that helps…

                           

                          Uwe

                          • 10. Re: invoking menu - language
                            robinfredericf Level 1

                            Thank you to answer so quickly,

                            Checking if the menuItem is enabled before invoking it is basically what I already did in my first attempt http://robinfredericf.free.fr/misc/wadyms-paragraphstyle-trick_indd002.zip but I copied on this post only the most important line, assuming that people would check the link to see all the script, so if not the case here it is:

                            #target "InDesign";
                            
                            
                            $.localize = true;
                            $.locale = null;
                            //$.locale = "en";
                            
                            
                            
                            
                            function main() {
                                
                                //var redefineParagraphStyle = app.menuActions.itemByID(132102);
                                /* « redefineParagraphStyle.invoke() » => Error « Script not activated. » */
                            
                            
                                /* cf. https://forums.adobe.com/thread/1961958 */
                                //var redefineParagraphStyle = app.menus[113].menuItems[3];
                                /* Marche dans InDesign CC 2015 mais dangereux car peut correspondre à une toute autre commande dans une autre version. */
                                
                                /* cf. http://kasyan.ho.com.ua/open_menu_item.html https://forums.adobe.com/thread/892886 */
                                var redefineParagraphStyle = app.menus.item('$ID/ParaStylePanelPopup').menuItems.item('$ID/Redefine Style');
                                
                                if (! redefineParagraphStyle.enabled) { throw new Error({
                                        en: "This script is intended to be used when selecting (or placing the cursor in) some text that has a paragraph style applied "
                                            +"and some unwanted overriden attributes that will be reverted to the style's values "
                                            +"for all occurrences where the same overrides affect the same style in the document.",
                                        fr: "The script est prévu pour être utilisé en sélectionnant (ou plaçant le curseur dans) du texte ayant un style de paragraphe appliqué "
                                            +"et certains attributs affectés par des remplacements indésirables qui seront rétablis à la valeur du style "
                                            +"pour toutes les occurrences où les mêmes remplacements affectent le même style dans le document."
                                }); }
                                
                                var myStyle = app.activeDocument.selection[0].insertionPoints[0].appliedParagraphStyle;
                                var myStyleName = myStyle.name;
                                var myStyleCopy = myStyle.duplicate();
                                redefineParagraphStyle.associatedMenuAction.invoke();
                                
                                myStyle.remove(myStyleCopy);
                                myStyleCopy.name = myStyleName;
                            }
                            
                            
                            
                            
                            
                            
                            try { 
                                app.doScript(
                                    function() { main(); },
                                    ScriptLanguage.JAVASCRIPT,
                                    undefined,
                                    UndoModes.ENTIRE_SCRIPT,
                                    File($.fileName).name
                                );
                            } catch(e) { alert(e); }
                            

                            and I also already tried app.findKeyStrings() to get other variant of the locale-independent string, so in http://robinfredericf.free.fr/misc/wadyms-paragraphstyle-trick_indd002a.zip I tried to replace '$ID/Redefine Style' with '$ID/RedefineStyle' — expecting that it might be better without space.

                            I also presume my colleague knows that he has to select some text that is showing overrides on the applied paragraph style, before running the script, because it was first his own idea and because I explained how to use the script on the demo idml file included with the jsx in the zip. From that screenshot he sent me he seems to be correctly attempting to use the script when the error message occurs:

                            object-is-invalid.jpg

                            I wonder if other users would get the same error when trying the script wadyms-paragraphstyle-trick_indd002.jsx on the document wadyms-paragraphstyle-trick_indd002.idml, for example on a German InDesign version.

                            • 11. Re: invoking menu - language
                              robinfredericf Level 1

                              On my side it appears to work as well when the paragraph styles panel is closed.

                              • 12. Re: invoking menu - language
                                Laubender Adobe Community Professional & MVP

                                Hi Frédéric,

                                 

                                tested your script:
                                wadyms-paragraphstyle-trick_indd002.jsx

                                on:

                                wadyms-paragraphstyle-trick_indd002.idml

                                Same selection as shown in your screenshot with the error message.

                                 

                                No problem with my German versions of InDesign CS5.5 and CC on OSX.
                                The script ran as intended.

                                 

                                I observed one difference in error handling of CS5.5 and CC v 9.3.0.

                                You do not ask if a document is open or if text is selected.

                                 

                                If you provoke such an error CS5.5 is alerting a simple: "undefined" statement.

                                And that's ok, I think, because you wrapped the try/catch around the doScript statement and did no error handling on the two cases I mentioned. Surprisingly InDesign CC is alerting your full error message regardless of the individual error caught. Just picking up the message…

                                 

                                For individual error handling I'd suggest something like that:

                                 

                                // indd002-MinimumVersion-2.jsx
                                
                                #targetengine reformatToAppliedStyle
                                
                                app.doScript
                                (
                                    reformatToAppliedStyle,
                                    ScriptLanguage.JAVASCRIPT,
                                    undefined,
                                    UndoModes.ENTIRE_SCRIPT,
                                    "Script | Undo Action"
                                );
                                
                                function reformatToAppliedStyle()
                                {
                                
                                    if(app.documents.length == 0){ return }; // Do we need an alert here? No.
                                    if(app.selection.length != 1){return}; // Do we need an alert here? No.
                                   
                                    if(!app.selection[0].hasOwnProperty("baselineShift"))
                                    { alert("Error: No text selected."); return };
                                
                                    var menuItem = app.menus.item("$ID/ParaStylePanelPopup").menuItems.item("$ID/Redefine Style");
                                
                                    if(menuItem.isValid == false)
                                    { alert("Error: menuItem Redefine Style cannot be found."); return };
                                
                                    if(menuItem.enabled == false)
                                    { alert("Error: menuItem Redefine Style is not enabled."); return };
                                
                                    var myStyle = app.activeDocument.selection[0].insertionPoints[0].appliedParagraphStyle;
                                   
                                    /*
                                        // [ No Paragraph Style ] used.
                                        NOTE: The following if statement should never be executed
                                        if [ No Paragraph Style ] is really used on selected text,
                                        because "Redefine Style" is never enabled with [ No Paragraph Style ]
                                        and we already have an error condition for that case.
                                
                                        Nevertheless:
                                        You cannot duplicate [ No Paragraph Style ]
                                    */
                                
                                    if(myStyle.index == 0)
                                    { alert("Error: No custom paragraph style applied: [ No Paragraph Style ]"); return };
                                   
                                    // [ Basic Paragraph Style ] used.
                                    // You can duplicate that style, but you cannot remove the original.
                                    // You cannot rename the duplicate to that style.
                                    if(myStyle.index == 1)
                                    { alert("Error: No custom paragraph style applied: [ Basic Paragraph Style ]"); return };
                                
                                    // FWIW: Styles cannot be named with [ or ] in their names.
                                    // [ and ] are restricted to the names of the base styles.
                                    // Maybe there are other special characters that are not working?!
                                
                                    var myStyleName = myStyle.name;
                                    var myStyleCopy = myStyle.duplicate();
                                
                                    menuItem.associatedMenuAction.invoke();
                                
                                    myStyle.remove(myStyleCopy);
                                    myStyleCopy.name = myStyleName;
                                
                                };
                                

                                 

                                Regards,
                                Uwe

                                • 13. Re: invoking menu - language
                                  Laubender Adobe Community Professional & MVP

                                  Additionally you could do a test for overrides in selected text after line 22:

                                   

                                  if(app.selection[0].styleOverridden == false)
                                  { alert("Error: No style override detected."); return };
                                  

                                   

                                  Regards,
                                  Uwe