Home > HOT Posts!, InDesign Scripts > Create customized menu

Create customized menu

Two days ago on Adobe’s InDesign scripting forum I found interesting question about creating custom menu, so I decided to dig little deeper and see how to achieve desired effect. I went to Marc Autrets site, and post How to Create your Own InDesign Menus. I tried to create custom menu that will be inserted into InDesign’s main menu, with three menu items, and one separator.

If we want custom menus to be available every time we start InDesign, script must be in ‘Startup Scripts’. Menu items are calling three different scripts from ‘Scripts Panel’ over doScript(); command. Great thing is event listener ‘beforeDisplay’ that is also included here in script. This event occurs after we click on menu, and before menu item is displayed, so we can for example disable menu item if there are no enough documents opened like we have here in example. Also, menu item will be added after ‘Layout’ item, and menu item number two will be enabled if you have two and more documents opened.

#targetengine "myTestMenu"

var myFolder = Folder(app.activeScript.path);
myFolder = myFolder.parent + '/Scripts Panel/';

var menuItem1Handler = function(/*onInvoke*/){
  app.doScript(File(myFolder + 'MyTest1.jsx'));
};

var menuItem2Handler = function(/*onInvoke*/){
  app.doScript(File(myFolder + 'MyTest2.jsx'));
};

var menuItem2HandlerDisplay = function(/*beforeDisplay*/){
  subMenu2.enabled = (app.documents.length>1);
};

var menuItem3Handler = function(/*onInvoke*/){
  app.doScript(File(myFolder + 'MyTest3.jsx'));
};

var menuInstaller = menuInstaller||(function(){
  var menuItem1T = "My Menu Item 1",
       menuItem2T = "My Menu Item 2",
       menuItem3T = "My Menu Item 3",
       menuT = "MyTestMenu",
       subs = app.menus.item("$ID/Main").submenus, sma, mnu;
  var refItem = app.menus.item("$ID/Main").submenus.item("$ID/&Layout");

  subMenu1 = app.scriptMenuActions.add(menuItem1T);
  subMenu1.eventListeners.add("onInvoke", menuItem1Handler);

  subMenu2 = app.scriptMenuActions.add(menuItem2T);
  subMenu2.eventListeners.add("onInvoke", menuItem2Handler);
  subMenu2.eventListeners.add("beforeDisplay", menuItem2HandlerDisplay);

  subMenu3 = app.scriptMenuActions.add(menuItem3T);
  subMenu3.eventListeners.add("onInvoke", menuItem3Handler);

  mnu = subs.item(menuT);
  if( !mnu.isValid ) mnu = subs.add(menuT, LocationOptions.after, refItem);
  mnu.menuItems.add(subMenu1);
  mnu.menuItems.add(subMenu2);
  mnu.menuSeparators.add();
  mnu.menuItems.add(subMenu3);
  return true;
})();

For more details go to Marc Autrets post about menus.

That’s it, and have fun! 😀

  1. xx
    August 13, 2010 at 09:56

    How to disable the menu items in the indesign application using startup script.I need to disable the menu items in the indesign application.

  2. December 11, 2011 at 01:50

    Thanks for this explanation!
    I have a question about submenus though. I’ve tried the following code to add a submenu:

    subT = “Sub Menu”
    subsSubs = app.menus.item( ‘$ID/Main’ ).submenus.item( menuT ).submenus
    mnuSubMenu = subsSubs.item( subT )
    if( !mnuSubMenu.isValid ) mnuSubMenu = subsSubs.add( subT);

    If you add that before the return false at the end of your script, it creates the sub menu at the bottom of the test menu, which is what I’d expect.

    But there’s a problem when I relaunch InDesign. The sub menu moves from the bottom of the test menu to the top! How do I keep the menu in the original position? In this case it’s at the bottom, but I want to make a sub menu like this in a specific location of a menu and just can’t get it working.

    Thanks in advance for any insight you may have Tomaxxi.

  3. December 18, 2011 at 07:59

    In case you wanted to know (and for others out there), the submenus in this example move to the top of the menu when you relaunch InDesign. I wasn’t sure how to fix it, but Harbs provided a solution on the Adobe Forums. I hope it helps others as much as it did me!

    http://forums.adobe.com/thread/938644?tstart=0

  1. No trackbacks yet.

Leave a comment