Menus
about to be displayed). Among other things, the script can then change the menu names and/or set the
enabled/checked status.
In the following sample script, we add an
current selection. If there is no selection, the script in the
item is selected, the menu item is enabled, and choosing the menu item displays the type of the first item
in the selection. (For the complete script, see BeforeDisplay.)
var mySampleScriptAction = app.scriptMenuActions.add("Display Message");
var myEventListener = mySampleScriptAction.eventListeners.add("onInvoke",
myOnInvokeHandler, false);
var mySampleScriptMenu = app.menus.item("$ID/Main").submenus.add("Script Menu
Action");
var mySampleScriptMenuItem = mySampleScriptMenu.menuItems.add(mySampleScriptAction);
mySampleScriptMenu.eventListeners.add("beforeDisplay", myBeforeDisplayHandler,
false);
//JavaScript function to run before the menu item is drawn.
function myBeforeDisplayHandler(myEvent){
var mySampleScriptAction = app.scriptMenuActions.item("Display Message");
if(app.documents.length > 0){
if(app.selection.length > 0){
}
else{
}
}
else{
mySampleScriptAction.enabled = false;
}
}
//JavaScript function to run when the menu item is selected.
function myOnInvokeHandler(myEvent){
myString = app.selection[0].constructor.name;
alert("The first item in the selection is a " + myString + ".");
}
mySampleScriptAction.enabled = true;
mySampleScriptAction.enabled = false;
to the
eventListener
eventListener
Working with script menu actions 69
beforeDisplay event
disables the menu item. If an
that checks the