3 Replies Latest reply on Sep 23, 2016 12:25 AM by Peter Kahrel

    Set global variables using #targetengine

    Skemicle Level 3

      When i write a script i would like to be able to use global variable so that i don't have to set the variables in each script. (eg. I use var doc = app.activeDocument; and a few others, in basically every script I write) I tried to set the variables as a startup script using #targetengine "mine" but that runs before a document opens (even with the "afterOpen" eventListener) invoking an error: "No documents are open." I also tried to create a menu button to set the variables :

       

      #targetengine "mine"
      
      var myMenuItemName = "Set Variables";
       // Create the Script Menu Action (myMenuItemAction)
      var myMenuItemAction = app.scriptMenuActions.add(myMenuItemName);
       // Add an Event Listener
      myMenuItemAction.addEventListener(
          /*event type*/   'onInvoke',
          /*event handler*/ setVariablesFunction
          );
       // Create a new menu item in the Help submenu
      var fileMenu = app.menus.item("$ID/Main").submenus.item("$ID/&Help");
      fileMenu.menuItems.add(myMenuItemAction);
      
      function setVariablesFunction(){
      var doc = app.activeDocument;
      var test = "Test ";
      alert(test + "Success")
      }
      

      but the second script would cause an error: "test is undefined"

      #targetengine "mine"
      
      alert(test + "failed")
      

      I am able to set the variables if i run the script manually as

      #targetengine "mine"
      var test = "success";
      

      and then

      #targetengine "mine"
      alert(test);
      

      and i know i can set a shortcut to this (which i may have to do anyway) but I don't really want to have to remember to run the script each time i open indesign.

       

      Hopefully I haven't complicated the question too much. Can anyone provide guidance as to what I need to do to make this work preferably as a startup but any other suggestions will be accepted.