9 Replies Latest reply on May 18, 2017 12:28 PM by lf.corullon

    add script menu item

    bsf2017

      Hi,

       

      I am adding some scripts to the menu. But it seems I am using the even listener wrong. I want a script to get associated with the menu item "pre-processing" and only get executed when someone clicks on this menu subitem.

       

      Here is my code:

       

      myScriptPath = app.activeScript.path;

       

      //alert(myScriptPath);

      var myPreScriptAction = app.scriptMenuActions.add("Pre-translation");

      var myPostScriptAction = app.scriptMenuActions.add("Post-translation");

      var myFilePath = myScriptPath + "/Pre_translation05.js";

      //alert(myFilePath);

      var myEventListener1 = myPreScriptAction.eventListeners.add("onInvoke", app.doScript (File(myScriptPath + "/Pre_translation05.js")));

      var myEventListener2 = myPostScriptAction.eventListeners.add("onInvoke", app.doScript (File(myScriptPath + "/Post_translation04.js")));


      //If the submenu "Beate's scripts Rev10" does not already exist, create it.

      try{

      var myScriptMenu = app.menus.item("$ID/Main").submenus.item("Beate's scripts Rev10");

      myScriptMenu.title;

      }

      catch (myError){

      var myScriptMenu = app.menus.item("$ID/Main").submenus.add("Beate's scripts Rev10");

      }

      var mPreScriptMenuItem = myScriptMenu.menuItems.add(myPreScriptAction);

      var mPostScriptMenuItem = myScriptMenu.menuItems.add(myPostScriptAction);

       

       

      Here is the error I get:

      error.png

       

       

        • 1. Re: add script menu item
          Laubender Adobe Community Professional & MVP

          Hi,

          maybe something's wrong with the script files you are calling with doScript() ?

           

          FWIW: If you are recieving an error message after running the main script from InDesign's Scripts panel do a restart of InDesign to clear everything in memory before trying the next thing. Do not test your main script with the ESTK.

           

          Do all three scripts run in the same engine?
          Is it main ? Or do you use different engine statements somewhere in the calling scripts?

           

          As first thing after a restart of InDesign I would try without doScript() and provide the code in the main script as functions first. Call the functions with your add() method. If that would run ok you could do several strategies:

           

          1. Make sure that the files you are pointing at exist and try to read out their contents.

          The recieved "nothing" could point to a problem here. Do both files lie in the same location with the main script? Is the name exactly the same in the file system compared with your call in the script? ( A blank could sneaked in at the end of the file name perhaps?)

           

          It's a wild guess and usually that's not necessary, but open the files in the ESTK (ExtendScript Toolkit) and save them perhaps with a jsx instead of a js suffix.

          Also: Maybe something's wrong with their read/write rights or encoding ?

           

          2. Provide at least one more argument with doScript().
          Start with the language parameter, that comes after the file:

           

          ScriptLanguage.JAVASCRIPT
          

           

          3. Construct the file before using it with doScript()

           

          var Pre_translation05 = File(myScriptPath + "/"+"Pre_translation05.jsx");
          
          if(Pre_translation05.exists)
          {
              var myEventListener1 =
              myPreScriptAction.eventListeners.add
              (
                  "onInvoke",
                  app.doScript
                  (
                      Pre_translation05,
                      ScriptLanguage.JAVASCRIPT
                  )
              );
          }
          else
          {
              // Throw an error here, stop the script.
          }
          

           

          4. Or abandon doScript() and use the #include statement at the beginning of your main script to bring in the code of your external scripts.
          In this example the two scripts are in the same folder with the calling main script:

           

          #include Pre_translation04.jsx
          #include Pre_translation05.jsx
          

           

          Make sure that everything in both included files is wrapped in a unique named function and call that function in your add() method with the listener.

           

          Regards,
          Uwe

          • 2. Re: add script menu item
            bsf2017 Level 1

            Hi,

             

            I rebooted InDesign, same error 30477.

            Both scripts exist and run just fine by themselves.

            tried saving them as JSX but that did not seem to make a difference.

             

            DoScript(): tried # 2 and 3 and still get error 30477.

                 tried #4 include statements, I get the error below:

             

             

            when I try pasting the whole subscripts in as functions, I get the following error2:

            error2.png

            I also get error2 when I remove the sub-scripts altogether and just put an alert statement after onInvoke.

             

            Thanks for the debugging help!

            • 3. Re: add script menu item
              bsf2017 Level 1

              I don't use any engine statements anywhere. Should I be??

              • 4. Re: add script menu item
                bsf2017 Level 1

                #include Pre_translation08.jsx;

                var myEventListener1 = myPreScriptAction.eventListeners.add("onInvoke", Pre_translation08);

                 

                runs ok but when I click on menu item "pre-translation",  I get error 2 (see screen shot above)
                #include Pre_translation08.jsx;

                var myEventListener1 = myPreScriptAction.eventListeners.add("onInvoke", Pre_translation08()); // should there be brackets here??

                 

                returns error1 (30477)

                • 5. Re: add script menu item
                  bsf2017 Level 1

                  I went back to the bare bones example from the manual p.149:

                  http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/indesign/sdk/cs6/scripting/InDesign _ScriptingGuide_JS.pdf

                   

                  var mySampleScriptAction = app.scriptMenuActions.add("Display Message");

                  var myEventListener = mySampleScriptAction.eventListeners.add("onInvoke",

                  function(){alert("This menu item was added by a script.");});

                  //If the submenu "Script Menu Action" does not already exist, create it.

                  try{

                  var mySampleScriptMenu = app.menus.item("$ID/Main").submenus.item(

                  "Script Menu Action");

                  mySampleScriptMenu.title;

                  }

                  catch (myError){

                  var mySampleScriptMenu = app.menus.item("$ID/Main").submenus.add

                  ("Script Menu Action");

                  }

                  var mySampleScriptMenuItem = mySampleScriptMenu.menuItems.add(mySampleScriptAction);

                   

                  I saved it as sample.js

                  It runs and makes the menu item.

                  when clicking on "Display Message", I get error2:

                  error2.png

                  • 6. Re: add script menu item
                    Laubender Adobe Community Professional & MVP

                    bsf2017  wrote

                    I don't use any engine statements anywhere. Should I be??

                    Hi,

                    yes. Do it with a #targetengine statement at the very beginning of your main script.

                     

                    #targetengine "uniqueName-1"
                    

                     

                    To debug run the script from the ESTK and set some break points and use the console to retrieve information of objects you made, variables you declared and ask of their values every time you hit a break point and see if all is there and defined how you want it.

                     

                    Maybe this blog post gives you a hint what is missing:

                    Werner Perplies ( all in German ):
                    Skript mit Dialogführung in das Hauptmenu einbinden - Adobe® InDesign® Automatisierung

                     

                    BTW: To format code here in the forum do Use Advanced Editor and format with Insert >> Syntax Highlighting > javascript

                     

                    Regards,
                    Uwe

                    1 person found this helpful
                    • 7. Re: add script menu item
                      bsf2017 Level 1

                      Guten Morgen Uwe!

                       

                      The targetengine statement did the trick!!

                      I'm making progress, but I'm not quite there yet.

                      My script now runs and installs a "Beate" menu with two sub-menus (pre and post translation).

                      They execute great when being clicked! Yeah!

                       

                      However:

                      When I shut down InDesign and reopen it, the Beate menu is still there, but the two sub-menus disappeared. I don't want the user to have to run my install script every time they open InDesign. Is there a way to make the sub-menus permanent?

                       

                      Thanks so much!!

                      Beate

                      • 8. Re: add script menu item
                        bsf2017 Level 1

                        Or is there a way to run my script every time InDesign opens?

                        • 9. Re: add script menu item
                          lf.corullon Level 2

                          Just put it into the startup script folder (in the instalation folder).

                          1 person found this helpful