Adobe 65009333 Scripting Guide - Page 69

current selection. If there is no selection, the script in the, disables the menu item. If an

Page 69 highlights

Menus Working with script menu actions 69 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 eventListener to the beforeDisplay event that checks the current selection. If there is no selection, the script in the eventListener disables the menu item. If an 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){ mySampleScriptAction.enabled = true; } else{ mySampleScriptAction.enabled = false; } } 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 + "."); }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88

Menus
Working with script menu actions
69
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
eventListener
to the
beforeDisplay
event
that checks the
current selection. If there is no selection, the script in the
eventListener
disables the menu item. If an
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){
mySampleScriptAction.enabled = true;
}
else{
mySampleScriptAction.enabled = false;
}
}
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 + ".");
}