2009-10-20

Build Menu Item

This is both some simple and some complex code to construct a menu item in a menu.

One advantage of what this does is that it creates a stand alone script in the Scripts Panel folder that can be used to Assign a Keyboard Shortcut to a custom menu item -- something normally not possible.


function buildMenuItem (aMenu, menuItemName, eventFunction , checkItem , addShortcutScript , forceUpdateShortcut ) {
//-------------------------------------------------------------------------
//-- B U I L D M E N U I T E M
//-------------------------------------------------------------------------
//-- Generic: Yes, but calls an external function for creating the
//-- shortcut scripts.
//-------------------------------------------------------------------------
//-- Purpose: To add items and event listners to a passed menu in the
//-- menu bar.
//-------------------------------------------------------------------------
//-- Parameters: 6
//-- aMenu: A menu object created with the 'makeMainMenu' function.
//-- menuItemName: A string for the text as it should appear in the
//-- menu.
//-- eventFunction: This is the name of the function that should be
//-- invoked when the menu item is accessed
//-- checkItem: OPTIONAL. A boolean to indicate that the item should
//-- be checked. The default is false.
//-- addShortcutScript: OPTIONAL. a boolean to indicate that an
//-- auxilary script should be added which could be used as
//-- the recipitant of a keyboard shortcut. The default is false.
//-- forceUpdateShortcut: OPTIONAL, a boolean that when true will
//-- rewrite the shortcut script regarless of if it already exists
//-- we don't want to do this all the time because of the speed
//-- and the chance that it would trigger the keyboard shortcut
//-- to get lost.
//-------------------------------------------------------------------------
//-- Calls:
//-- findScriptsPanelFolder()
//-- buildMenuFolderStructure()
//-------------------------------------------------------------------------
//-- Returns: Nothing.
//-------------------------------------------------------------------------
//-- Written by Jon S. Winters.
//-- eps@electronicpublishingsupport.com
//-------------------------------------------------------------------------
//-- Edited: 2009.01.23 to add the option to check the menu item.
//-- Edited: 2009.10.06 to start the process of making stand alone scripts
//-- that can be used for shortcuts for each menu item added.
//-- Verify that the checked item has been sent, else assume false
if ( checkItem == undefined ) {
checkItem = false ;
}
//-- Build the pat menu by constructing the action and then the menu item
var mMenuItem = aMenu.menuItems.item( menuItemName ) ;
if ( mMenuItem == null ) {
var mAction = app.scriptMenuActions.add( menuItemName ) ;

//-- Version 2r, add check
mAction.checked = checkItem ;
//alert ("Making menu: " + menuItemName + " has Action ID: " + mAction.id ) ;
var mListener = mAction.eventListeners.add( "onInvoke", eventFunction , false ) ;
var mMenuItem = aMenu.menuItems.add( mAction , LocationOptions.AT_END ) ;
//-- 2009.10.06 start the process of adding keyboard shortcut scripts.
//-- 2009.10.11 Some sub menus use '-' to create separators
//-- 2009.10.11 Also skip when the name doesn't exist
if ( addShortcutScript && ( menuItemName != '-' ) && ( menuItemName != '' ) ) {
try {
//-- Call an external function to determine the location of the
//-- application's Scripts Panel Folder. Then get the name
//-- of the parent menu of the menu item being created.
var shortcutDestination = Folder ( findScriptsPanelFolder() + '/' + buildMenuFolderStructure ( aMenu ) ) ;
//-- Verify that the folder exists because ExtendScript will
//-- lie about the file if the folder isn't there.
if ( shortcutDestination.verify ( ) ) {
//-- Add to that the name of the menu and create a file
//-- Object.
//-- Version 2.67 remove any / in name
var f = File ( shortcutDestination + '/' + menuItemName.replace(new RegExp ( "/","gm"),encodeURIComponent ('⁄')) + '.jsx' ) ;
//-- Test that file object. If it already exists and the function
//-- wasn't asked to overwrite it, skip the process.
if ( ( ! f.exists ) || forceUpdateShortcut ) {
//-- Create a string which will be written to a file with a
//-- .jsx file extension. The string will include a
//-- preprocessor directive to match the running target
//-- engine. No need to match the host applicaiton.
//-- Create a fake event object that can be passed for when
//-- the menu item is created in an loop of an array.
//-- To the reciving function it will look like:
//-- event.target.name
//-- Write a commented header
var fileContents = '//-- Created: ' + new Date () + ' by the ' + arguments.callee.name +' function.\r' ;
//-- if there is a target engine active, include this.
if ( $.engineName != undefined ) {
fileContents += '#targetengine ' + $.engineName + '\r'
}
//-- Now call the passed function and supply it with an object
//-- literal of the menu name to be used when the function
//-- is called from within a loop of an array.
//-- Version 2.67 escape any quotes in the menu item name.
fileContents += eventFunction.name + "({target:{name:\'" + menuItemName.replace (new RegExp ( "([\'\"“”‘’])",'gm'), "\\$1" ) + "\'}})" ;
//-- write the actual file.
f.open ('w') ;
f.write (fileContents) ;
f.close ();
}
}
}
catch ( err ) {
var errorObject = err ;
try {
f.close() ;
}
catch ( err ) {
var errorObject = err ;
}
}
}
}
}
//

No comments:

Post a Comment