メニューバー上にステータスバーアイテムを作成し、サブメニューつきのメニューを生成するAppleScriptです。
2階層のサブメニューに対応したことで、より有用性が高まりました(Thanks Shane!)。
実行には、スクリプトエディタ上でControl-Command-Rの操作を行ってください。
set aList to {“Piyomaru”, “Software”, “”, “Takaaki”, {“Yes”, “No”}, “”, “Machine”, {“MacBook Pro”, “MacBook Air”, “Mac mini”}, “”, “Quit”}
のようなリストを与えると、
のようにメニューを生成。各メニューアイテムをクリックするとIDをNotification表示します。「Quit」を選択するとメニュー表示を終了します。
次はn階層のサブメニュー対応とか、ファイルシステム上のディレクトリ構造を反映させるとか、そういう機能がほしいところです。
AppleScript名:ステータスバーのじっけん v4 |
— Created 2017-03-03 by Takaaki Naganoya — Modified 2018-02-15 by Shane Stanley–Thanks!! — Modified 2018-02-15 by Takaaki Naganoya use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "AppKit" property aStatusItem : missing value on run init() of me end run on init() set aList to {"Piyomaru", "Software", "", "Takaaki", {"Yes", "No"}, "", "Machine", {"MacBook Pro", "MacBook Air", "Mac mini"}, "", "Quit"} set aStatusItem to current application’s NSStatusBar’s systemStatusBar()’s statusItemWithLength:(current application’s NSVariableStatusItemLength) aStatusItem’s setTitle:"🍎" aStatusItem’s setHighlightMode:true aStatusItem’s setMenu:(createMenu(aList) of me) end init on createMenu(aList) set aMenu to current application’s NSMenu’s alloc()’s init() set aCount to 10 set prevMenuItem to "" repeat with i in aList set j to contents of i set aClass to (class of j) as string if j is equal to "" then set aMenuItem to (current application’s NSMenuItem’s separatorItem()) (aMenu’s addItem:aMenuItem) else if (aClass = "text") or (aClass = "string") then if j = "Quit" then set aMenuItem to (current application’s NSMenuItem’s alloc()’s initWithTitle:j action:"actionHandler:" keyEquivalent:"") else set aMenuItem to (current application’s NSMenuItem’s alloc()’s initWithTitle:j action:"actionHandler:" keyEquivalent:"") end if (aMenuItem’s setTag:aCount) (aMenuItem’s setTarget:me) (aMenu’s addItem:aMenuItem) set aCount to aCount + 10 copy aMenuItem to prevMenuItem else if aClass = "list" then –Generate Submenu set subMenu to current application’s NSMenu’s new() (aMenuItem’s setSubmenu:subMenu) set subCounter to 1 repeat with ii in j set jj to contents of ii set subMenuItem1 to (current application’s NSMenuItem’s alloc()’s initWithTitle:jj action:"actionHandler:" keyEquivalent:"") (subMenuItem1’s setTarget:me) (subMenuItem1’s setTag:(aCount + subCounter)) (subMenu’s addItem:subMenuItem1) set subCounter to subCounter + 1 end repeat end if end if end repeat return aMenu end createMenu on actionHandler:sender set aTag to tag of sender as string set aTitle to title of sender as string if aTitle is equal to "Quit" then current application’s NSStatusBar’s systemStatusBar()’s removeStatusItem:aStatusItem else display notification (aTag as string) end if end actionHandler: |
More from my site
(Visited 275 times, 3 visits today)
tqbinh says:
Thank you for the script. Could you make it to show up the menu at current mouse position (like a context menu) when use press a shortcut key, say, Command+M ?
Takaaki Naganoya says:
We can’t make global keyboard shortcut with status menu items.
Look the all status bar items and their menu. There is no keyboard shortcut-ed items.
tqbinh says:
Thank you for clarifying. I asked because some status bar app, like BetterSnapTool support global keyboard shortcut to open menu.
Takaaki Naganoya says:
Is BetterSnapTool written in AppleScript? No.
It should be written in C++ and Objective-C.
And I don’t like global keyboard shortcut. There is a lot of workaround not to use global keyboard shortcut.
If it is a business with enough badget, I’ll research it :-).
tqbinh says:
Thank you again for clarifying!
tqbinh says:
Hi
Today I got this error when running the script from Script Editor on Catalina 10.15.6:
error “NSWindow drag regions should only be invalidated on the Main Thread!” number -10000
with this line highlighted:
set aStatusItem to current application’s NSStatusBar’s systemStatusBar()’s statusItemWithLength:(current application’s NSVariableStatusItemLength)
If I export it as an app, it runs fine.
Thank!
Takaaki Naganoya says:
You have to run this script by Control-Command-R (Run in main thread) with Script Editor.
tqbinh says:
Thank! It runs with Control+Command+R now.