1 Reply Latest reply on Feb 13, 2012 12:41 AM by John Hawkinson

    How to create separate menus

    still_und_leise Level 1

      Hi,

       

      I am trying to create menus for my scripts. When I'm adding only one script, this works fine:

       

       

      #targetengine "adobe"
      
      getPath = function() {
              try {
              var script = app.activeScript;
          } catch(e) {
              var script = File(e.fileName);
          }
          return script.path + '/../';
      }
      
      
      readscript = function(scriptname){
          var path = getPath();
          var file = File(path + scriptname);
          var script = undefined;
          if (file.exists) {
              file.open();
              script = file.read();
              file.close();
          }
          return script;
      }
      
      var myXMLExportScriptAction = app.scriptMenuActions.add("Export Images");
      var myXMLEventListener = myXMLExportScriptAction.eventListeners.add("onInvoke", function(){
          var script = readscript("exportImages.jsx");
          eval(script);
          });
      
      var myXMLExportScriptAction1 = app.scriptMenuActions.add("delete Menu");
      var myXMLEventListener = myXMLExportScriptAction1.eventListeners.add("onInvoke", function(){
          var script = readscript("deletes2.jsx");
          eval(script);
          });
      
      try{
          var myXMLExportScriptMenu = app.menus.item("$ID/Main").submenus.item("XML-Export");
          myXMLExportScriptMenu.title;
      }
      catch (ex){
          var myXMLExportScriptMenu = app.menus.item("$ID/Main").submenus.add("XML-Export");
      }
      
      myXMLExportScriptMenu.menuItems.add(myXMLExportScriptAction);
      myXMLExportScriptMenu.menuItems.add(myXMLExportScriptAction1);     
      

          

      I now have a second script-combination, which should be used seperately (so some user will install the first script-set, some the second and there are also users needing both scripts). Yet when I use the same code for the second menu, I get an error and nothing works anymore. I changed the names of all varibles but this also does not help. Is it not possible to create two seperate menus or where could be my mistake?

       

      Thanks for your help.

        • 1. Re: How to create separate menus
          John Hawkinson Level 5

          Yet when I use the same code for the second menu, I get an error and nothing works anymore. I changed the names of all varibles but this also does not help. Is it not possible to create two seperate menus or where could be my mistake?

          Your mistake is telling us you are getting an error without giving us the full precise text of the error and its context.

          How could we possibly help without that information?

           


          but anyhow, while your problem isn't obvious, one can comment on your script:

           

          You are using a persistent engine, but you are using plenty of global variables. (Almost all of them!) This is inadvisible, try wraping the whole thing inside a function.

           

              var script = undefined;

           

          This is never necessary, variables in JavaScript default to undefined. Just use var script;.

           

           

          var myXMLEventListener = myXMLExportScriptAction.eventListeners.add("onInvoke", function(){
              var script = readscript("exportImages.jsx");
              eval(script);
              });

           

          This is rather odd, why do you apply this strange circumlocution? Why not simply make the script file the event listener? Or use app.doScript()? Why do you read in the script by hand and eval() it? Usually if your code is using eval() without a very good reason, it is wrong.

           

          try{
              var myXMLExportScriptMenu = app.menus.item("$ID/Main").submenus.item("XML-Export");
              myXMLExportScriptMenu.title;
          }
          catch (ex){
              var myXMLExportScriptMenu = app.menus.item("$ID/Main").submenus.add("XML-Export");
          }

           

          JavaScript has function scope, not block scope, so these are both the same "myXMLExportScriptMenu" variable.

           

          You should not declare a variable with 'var' in two places within the same function, because in all cases it is treated like the declaration is hoised to the top of the function, and each varable is an assignment. That is, the above is equivalent to:

           

           

          var myXMLExportScriptMenu;

          try{
              myXMLExportScriptMenu = app.menus.item("$ID/Main").submenus.item("XML-Export");
              myXMLExportScriptMenu.title;
          }
          catch (ex){
               myXMLExportScriptMenu = app.menus.item("$ID/Main").submenus.add("XML-Export");
          }

           

           

          Why bother to change it? Clarity. When you declare a variable like that within a block, human nature is to treat it like it has block scope. This invariably leads to bugs and confusing code.

           

           

          Anyhow, your script is probably too complicated to debug. Reduce it to the smallest simplest case that still demonstrates the problem. And please include the error message.

           

          I'm definitely not convinced from inspection that your getPath() and readscript() functions work as intended.