Let me follow up on this…
Automatic enabling/disabling of menu items is usually controlled by the target implementing the method validateMenuItem:
. If this returns true, the menu item will be enabled. It normally calls it’s superclass version (continue
in AppleScript) if the item doesn’t match one the object doesn’t have a target for.
You can actually see this in the following code. The validation handler is simply returning true for testing:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
set DemoMenu to current application's NSMenu's alloc()'s initWithTitle:"Demo"
set ReturnValue to DemoMenu's addItemWithTitle:"Demo 1" action:"doDemo1:" keyEquivalent:""
ReturnValue's setTarget:me
set ReturnValue to DemoMenu's addItemWithTitle:"Demo 2" action:"doDemo2:" keyEquivalent:""
ReturnValue's setTarget:me
set ReturnValue to DemoMenu's addItemWithTitle:"Demo 3" action:"doDemo3:" keyEquivalent:""
ReturnValue's setTarget:me
set DemoMenuItem to current application's NSMenuItem's alloc()'s initWithTitle:"Demo" action:"" keyEquivalent:""
DemoMenuItem's setSubmenu:DemoMenu
set RootMenu to current application's NSApp's mainMenu()
RootMenu's addItem:DemoMenuItem
on doDemo1:sender
display dialog "message" buttons {"Cancel", "OK"} default button "OK" with title "Demo 1"
end doDemo1:
on doDemo2:sender
display dialog "message" buttons {"Cancel", "OK"} default button "OK" with title "Demo 2"
end doDemo2:
on doDemo3:sender
display dialog "message" buttons {"Cancel", "OK"} default button "OK" with title "Demo 3"
end doDemo3:
on validateMenuItem:anItem
set theSel to anItem's action() as text
display dialog theSel
return true
end validateMenuItem:
You can see that the handler is being called, but for some reason its result is being ignored. I’m guessing this is a hole in the bridging mechanism, and that the value we return is not being converted to the expected BOOL
type, and is thus not being interpreted correctly.
That leaves the nuclear option, which is probably fine here. This means turning off autoenabling completely — items will be enabled as long as they have an action and a target can be found, and as long as you haven’t set them to be disabled.
It’s a nuclear option because, for some reason, we have to use it for the main menubar, so if you’re relying on other items being enabled according to circumstances, you need to handle them manually too. So:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions
set DemoMenu to current application's NSMenu's alloc()'s initWithTitle:"Demo"
set ReturnValue to DemoMenu's addItemWithTitle:"Demo 1" action:"doDemo1:" keyEquivalent:""
ReturnValue's setTarget:me
set ReturnValue to DemoMenu's addItemWithTitle:"Demo 2" action:"doDemo2:" keyEquivalent:""
ReturnValue's setTarget:me
set ReturnValue to DemoMenu's addItemWithTitle:"Demo 3" action:"doDemo3:" keyEquivalent:""
ReturnValue's setTarget:me
set DemoMenuItem to current application's NSMenuItem's alloc()'s initWithTitle:"Demo" action:"" keyEquivalent:""
DemoMenuItem's setSubmenu:DemoMenu
set RootMenu to current application's NSApp's mainMenu()
RootMenu's addItem:DemoMenuItem
RootMenu's setAutoenablesItems:false -- enabling/disabling must be done manually
on doDemo1:sender
display dialog "message" buttons {"Cancel", "OK"} default button "OK" with title "Demo 1"
end doDemo1:
on doDemo2:sender
display dialog "message" buttons {"Cancel", "OK"} default button "OK" with title "Demo 2"
end doDemo2:
on doDemo3:sender
display dialog "message" buttons {"Cancel", "OK"} default button "OK" with title "Demo 3"
end doDemo3:
Use at your own risk.