I am aware of the Script Editor menu. That is not what I'm looking for.
Is there a way to run an Applescript from the menu bar? I can save it as a file and run it from the Script Editor menu, and I can save it as an application and stick it in my dock. Is there a file format that allows it to run from the menu bar, as many applications do? Or a way to edit the code that does the same thing?
Best Answer
Cocoa-AppleScript as a script or AppleScript application created in Script Editor supports Menu Bar app. This script runs on macOS 10.12 (maybe runs on 10.10.x, 10.11.x). This script can be saved as application.
I previously edited the original code, posted by Piyomaru, just to the point it would compile and wanted to add some additional information to make the example code more useful for anyone relatively new to AppleScript.
Presently in the example code, if saved as an application it has no code to quit the application, it only removes the menu bar object. So, in the
actionHandler
handler of the script, add the following to theelse
branch of theif
statement, after the existing code:It's coded so if/when run from within Script Editor it will not attempt to close Script Editor. You can use it without the
if
statement block and use justtell current application to quit
if you're not going to run it from within Script Editor.Because this script is using Cocoa-AppleScript, once run from within Script Editor, it must be compiled again just before saving it in order to flush the C and Objective-C pointers created by the Cocoa-Applescript code, since pointers cannot be saved in the script. Just click the hammer icon on the Toolbar, or: Script Editor > Script > Compile ⌘K
Additionally, when saved as an application you must check the Stay open after run handler check-box when saving it, and by default the app's Dock Tile will show in the Dock when running. As this might be unwanted behavior for a menu bar app, use the following command in Terminal to add the necessary key to the app's Info.plist file so it will not show in the Dock.
name_of_app
with the actual name you saved it as, and if necessary change the path if not in/Applications
.In the
actionHandler
handler of the original example code, it just displays a dialog box with the menu item number that was clicked, which certainly fine for example code, however, I'd like to show a different example how to have separate code execute based on which menu item was clicked. The following example code could replace the entireactionHandler
handler in the original script:Now this could be written differently, i.e. "Quit" as an
else
orelse if
at the bottom, but I did it this way because I want all other menu commands to be anelse if
choice, even though logically any other given menu command is more likely to be clicked then "Quit". For me, I think it's easier to add/edit/remove the menu command's code when written this way. It's just a personal preference, so do it as you feel what's right for you.A discussion note from the AppleScript Language Guide:
For the most part of the original example code, you should only have to modify two line of code in the
init
handler:""
is for a separator.For a different icon:
Hopefully it's obvious that the
if
statement block in theactionHandler
handler will correspond to theaList
in theinit
handler, so match the names in theaTitle is equal to
in theactionHandler
handler to the names in theaList
in theinit
handler.The rest of the code should not need to be modified, although it certainly can if need be or wanted.