Scriptable menu bar icons
Is there an easy way I can add custom icons to the menu bar status area? Ideally something like Growl (e.g., where you can just execute a command) but instead of notifications, just renders icons in the menu bar.
I'm mainly looking to draw numbers that I'd use to alert me of various things. For example, I can configure my email client to run a command/AppleScript to update the numbers when an certain kind of email arrives.
Something like this:
Solution 1:
I would suggest starting with this thread, which describes using Cocoa Objective-C calls from within AppleScript. There is an example Xcode project here.
And Launch is a complete application written in AppleScript that functionally does this. Its Xcode project file is here.
The basic premise is to generate NSMenuItem instances and add them to the system's NSStatusBar:
set statusMenu to (NSMenu's alloc)'s initWithTitle_("Launch")
-- (build and do stuff with the statusMenu here)
set sysStatusBar to NSStatusBar's systemStatusBar
set statusItem to sysStatusBar's statusItemWithLength_(32)
tell statusItem to setMenu_(statusMenu)
tell statusItem to setHighlightMode_(1)
set menuImage to NSImage's imageNamed_("icon.png")
tell statusItem to setImage_(menuImage)
AppleScript excerpt from Launch's source code, Copyright © Lee Hanken
Solution 2:
Okay, I have made an app for this. It is on GitHub: 0942v8653/ScriptableStatusBar. Just keep the app running and use the sbar
tool to set and remove items.
As the readme says:
sbar set face 😃 'Bye!:sbar remove face'
Creates a face in the status bar. The menu will have an option Bye!
that runs sbar remove face
getting rid of the menu it creates.
You can download a compiled version from https://github.com/0942v8653/ScriptableStatusBar/releases.
Solution 3:
You can also use RubyCocoa:
require "osx/cocoa"
include OSX
app = NSApplication.sharedApplication
statusitem = NSStatusBar.systemStatusBar().statusItemWithLength(NSVariableStatusItemLength)
statusitem.setTitle("aa")
app.run
Solution 4:
Another option is to use Rumps, a Python library that uses the Obj-C classes and functions.
http://rumps.readthedocs.org/en/latest/