InDesign Tips & Tricks

Customizing InDesign Context Menu: Adding 'Insert Cross-Reference' to Right-Click

Published by coflynn on 2011-12-03

My previous blog post mentioned how much faster it would be to just right-click and have an 'insert cross-reference'. It took a little more than planned, but here is a script to accomplish just that:

( function () {
/* ADD items to CONTEXT MENU
    The locale-independent name (aka "key string") for the context menus:
    - Layout menu: $ID/RtMouseLayout - when an image is selected
    - Default menu: $ID/RtMouseDefault - when nothing is seleced
    - Text menu: $ID/RtMouseText - when the mouse is in text, or text is selected
*/
var contextMenu = app.menus.itemByName("$ID/RtMouseText");

var xrefMenu = app.menus.item("$ID/Main").submenus.item("$ID/&Type").submenus.item("$ID/XRefSubMenu");
var refItem = xrefMenu.menuItems.item("$ID/New XRef...");

     try {
          contextMenu.menuItems.itemByName(refItem.name).remove();
     } catch( ex ) {};
     contextMenu.menuItems.add(refItem.associatedMenuAction,LocationOptions.AFTER,contextMenu.menuItems.itemByName("$ID/InsertSpecial Footnote"));
})();


You can save the above file as AddCrossRefToContext.jsx and save in your InDesign Startup Script folder, for me that meant in C:\Program Files\Adobe\Adobe InDesign CS5.5\Scripts\startup scripts . Next time you start in-design, if you right-click in a text box you should see the following:
Image

Hurrah! I'll also document how to modify the script to get other menu items instead of cross-references.

First, you need to understand how to get the InDesign locale-unspecific 'key'. To do so fire up "Adobe ExtendScript Toolkit CS5.5", and also open "InDesign CS5.5". Select InDesign as the linked application:
Image

Find the name of the menu item you want to add, for example I'll demonstrate adding the "Odd Page Break" item. This item is located in the following menu structure: Type -> Insert Break Character -> Insert Odd Page Break. We need to find the name of each of those menus/submenus/items.

You also need to decide where in the context menu to go - I'll put it after 'Show hidden characters'.

Some methods for discovering them are discussed in http://forums.adobe.com/message/4061217 . The easiest is running the following command in the Javascript Console (found by John Hawkinson):

app.findKeyStrings(app.menuActions.itemByName("Insert Cross-Reference...").title)


Which gives you the "$ID/New XRef..." you see in the code. For some reason it doesn't seem to work with the break character menu. Using the other method (with 'alt' key) shown in that thread, you can discover the following:

Menu NameCommand to run in JS ConsoleID Result
Type app.findKeyStrings("&Type") $ID/&Type
Insert Break Character app.findKeyStrings("Insert Brea&k Character") $ID/Insert Break Menu
Odd Page Break app.findKeyStrings("&Odd Page Break")$ID/Odd Page Break


Also figure out the location in the context menu, here I use the other method to find the key:

Menu NameCommand to run in JS ConsoleID Result
Show Hidden Charactersapp.findKeyStrings(app.menuActions.itemByName("Show Hidden Characters").title)$ID/Show Hidden Characters


Finally:

( function () {
var contextMenu = app.menus.itemByName("$ID/RtMouseText");

var xrefMenu = app.menus.item("$ID/Main").submenus.item("$ID/&Type").submenus.item("$ID/Insert Break Menu");
var refItem = xrefMenu.menuItems.item("$ID/Odd Page Break");
 

    try {
          contextMenu.menuItems.itemByName(refItem.name).remove();
     } catch( ex ) {};
     contextMenu.menuItems.add(refItem.associatedMenuAction,LocationOptions.AFTER,contextMenu.menuItems.itemByName("$ID/Show Hidden Characters"));
})();