2009-10-13

Traverse a Menu Item to Build a Folder Structure

I've been way to much lately with user interfaces. One problem with ExtendScript and building custom menu items is that you can't make keyboard shortcuts stick. The menus tend to get built each time the script launches. And each time they get built the menu items get unique ids within the Adobe application. The problem with this is that the keyboard shortcuts are tied not to a name, but to the unique id. Thus, keyboard shortcuts won't last longer than a single launch. If you are development mode, they won't even last that long.
The solution it seems is to create a stand alone script which calls the same function as the menu. These stand alone scripts would be visible to the user in the Scripts panel of Adobe InDesign or Adobe InCopy. Since these scripts are static ( as opposed to the ever changing unique ids of menu items ) their keyboard shortcuts stick.
The function below is a function called by the function that I use to build menu items. And it can look at a menu item and determine the its name and the name of all the submenus and menus above it and create what amounts to be a folder structure to place the script into.
Well that requires a few other functions...

//
function buildMenuFolderStructure ( aMenu ) {
//-------------------------------------------------------------------------
//-- B U I L D M E N U F O L D E R S T R U C T U R E
//-------------------------------------------------------------------------
//-- Generic: Yes, for ExtendScript
//-------------------------------------------------------------------------
//-- Purpose: To traverse a passed menu item to determine its relationship
//-- to parent menus and build a string of menus from it that can be
//-- be used for a folder path. For example if passed a menu item as
//-- a submenu of a menu added to the menu bar with this type of
//-- structure: Utilities --> Apply Page Grid --> 7 column grid
//-- this function would return:
//-- Utilities/Apply Page Grid/7 column grid/
//-------------------------------------------------------------------------
//-- Arguments: aMenu -- a reference to a menu
//-------------------------------------------------------------------------
//-- Calls: itself.
//-------------------------------------------------------------------------
//-- Returns: a string described above
//-------------------------------------------------------------------------
//-- Sample Use: var r = buildMenuFolderStructure ( aMenu ) ;
//-------------------------------------------------------------------------
//-- Notes: very little error checking. The top menu is named 'Main'
//-------------------------------------------------------------------------
//-- Written: 2009.10.06 by Jon S. Winters of electronic publishing support
//-- eps@electronicpublishingsupport.com
//-------------------------------------------------------------------------
if ( aMenu.parent.name == 'Main' ) {
return aMenu.name + '/' ;
}
//-- implied else
return buildMenuFolderStructure ( aMenu.parent ) + aMenu.name + '/' ;
}
//

No comments:

Post a Comment