Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

Custom menu item to launch applescript.

Return to Member Forum

  • Author
    Posts
    • #59803
      dentistw
      Participant

      I'm yet another “copy and paste” programmer.

      From indiscripts.com, figured out how to create a menu item via JS in ID CS4. This custom menu item launches a dialog box.

      I have an applescript that performs a unique PDF export.

      I want the menu item to launch my script. Gotta be doable because I hacked another menu item by altering its applescript years ago. Can't find the code for that menu item though. Thanks for any guidance.

      #targetengine 'SampleScriptMenuAction'

      var smaTitle = “Sample Script Menu Action”;

      // Create a new Script Menu Action
      var sma = app.scriptMenuActions.add(smaTitle);

      // Add an Event Listener
      sma.addEventListener(
      /*event type*/ 'onInvoke',
      /*event handler*/ function(){alert('Hello World!');}
      );

      // Internal call
      sma.invoke();

    • #59815

      Here is an example:

      #targetengine “dentistw”
      var myScript1 = new File(app.filePath + “/Scripts/Scripts Panel/uniquePDFexport.scpt”);
      var myScriptAction1 = app.scriptMenuActions.add(“PDF export”);
      var myEventListener1 = myScriptAction1.eventListeners.add(“onInvoke”, myScript1, false);

      try{
      var myScriptMenu = app.menus.item(“$ID/Main”).submenus.item(“Scripts”);
      myScriptMenu.title;
      }
      catch (myError){
      var myScriptMenu = app.menus.item(“$ID/Main”).submenus.add(“Scripts”);
      }

      var myScriptMenuItem1 = myScriptMenu.menuItems.add(myScriptAction1);

      Save it to the “Scripts > Startup Scripts” folder and restart InDesign. A new “Scripts” menu will appear with a single “PDF export” item which will trigger the uniquePDFexport.scpt script (change the name to whatever your script's name is, of course).

      You can easily add more menu items by coping and pasting lines 2-4 and the last one and incrementing the number at the end of the variabels' names and making necessary changes to these lines.

      For example:

      var myScript2 = new File(app.filePath + “/Scripts/Scripts Panel/YourAnotherScriptName.scpt”);
      var myScriptAction2 = app.scriptMenuActions.add(“Do Something Else”);
      var myEventListener2 = myScriptAction2.eventListeners.add(“onInvoke”, myScript2, false);

      …..
      var myScriptMenuItem2 = myScriptMenu.menuItems.add(myScriptAction2);

      You can also insert separators like so:

      var myScriptMenuItem8 = myScriptMenu.menuItems.add(myScriptAction8);
      var myMenuSeparator3 = myScriptMenu.menuSeparators.add(LocationOptions.AFTER, myScriptMenuItem8);

      Hope this helps.

      Kas

    • #59822
      dentistw
      Participant

      Excellent work Kas. Thanks so very much for the code. It works exactly as you detailed. I'm so very pleased. BEST REGARDS, dennis

Viewing 2 reply threads
  • You must be logged in to reply to this topic.
Forum Ads