-- script: Creating Menus example for AsObjC applications
-- written: by KniazidisR, 19 Sep 2022 17:10:25
-- IMPORTANT: save and run this script as Stay-Open application
-- If you will execute the script as is from Script Editor of Script Debugger,
-- it will erase its menu items. Only after restarting the script editor
-- the menu items will restore. smile
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
-- init new menu bar to build menus from scratch
set menubar to current application's NSMenu's alloc()'s init()
-- clear predefined menus
set menuBarItem to current application's NSMenuItem's new()
menubar's addItem:menuBarItem
current application's NSApp's setMainMenu:menubar
current application's NSApp's mainMenu()'s removeAllItems()
-- make and edit App name's menu
set asstMenuItem to menubar's addItemWithTitle:"" action:(missing value) keyEquivalent:""
set asstMenu to current application's NSMenu's alloc()'s init()
menubar's setSubmenu:asstMenu forItem:asstMenuItem
set preferencesMenuItem to asstMenu's addItemWithTitle:"Preferences" action:"actionHandler:" keyEquivalent:""
(preferencesMenuItem's setTarget:me)
set quitMenuItem to asstMenu's addItemWithTitle:"Quit" action:"actionHandler:" keyEquivalent:"q"
(quitMenuItem's setTarget:me)
-- make and edit "File" menu
set asstMenuItem to menubar's addItemWithTitle:"" action:(missing value) keyEquivalent:""
set asstMenu to current application's NSMenu's alloc()'s initWithTitle:"File"
menubar's setSubmenu:asstMenu forItem:asstMenuItem
set openMenuItem to asstMenu's addItemWithTitle:"Open" action:"actionHandler:" keyEquivalent:"o"
(openMenuItem's setTarget:me)
set saveMenuItem to asstMenu's addItemWithTitle:"Save" action:"actionHandler:" keyEquivalent:"s"
(saveMenuItem's setTarget:me)
set saveAsMenuItem to asstMenu's addItemWithTitle:"Save As..." action:"actionHandler:" keyEquivalent:"a"
(saveAsMenuItem's setTarget:me)
set printMenuItem to asstMenu's addItemWithTitle:"Print..." action:"actionHandler:" keyEquivalent:"p"
(printMenuItem's setTarget:me)
-- make and edit "Edit" menu
set asstMenuItem to menubar's addItemWithTitle:"" action:(missing value) keyEquivalent:""
set asstMenu to current application's NSMenu's alloc()'s initWithTitle:"Edit"
menubar's setSubmenu:asstMenu forItem:asstMenuItem
set openMenuItem to asstMenu's addItemWithTitle:"Copy" action:"actionHandler:" keyEquivalent:"c"
(openMenuItem's setTarget:me)
set saveMenuItem to asstMenu's addItemWithTitle:"Paste" action:"actionHandler:" keyEquivalent:"v"
(saveMenuItem's setTarget:me)
set saveAsMenuItem to asstMenu's addItemWithTitle:"Undo" action:"actionHandler:" keyEquivalent:"u"
(saveAsMenuItem's setTarget:me)
set printMenuItem to asstMenu's addItemWithTitle:"Redo" action:"actionHandler:" keyEquivalent:"r"
(printMenuItem's setTarget:me)
on actionHandler:sender
set theTitle to title of sender as string
if theTitle is "Quit" then quit
display dialog "The \"" & theTitle & "\" menu item pressed"
end actionHandler: