3

Basically I want to issue a command in a bash script or an Apple Script and see some string (or icon) in the status bar aka "where the clock is".

Something similar to osascript but for the status bar:

/usr/bin/osascript -e 'display notification "Lorem ipsum dolor sit amet" with title "Title"'

6

This is something that I looked for a long time ago and did not find a satisfactory solution to. Your question has inspired me to finish the project that I began back when I was searching for a solution to this.

Check out menubarnotifier on GitHub. You can display a notification in the menubar by passing a string to the script:

./menubarnotifier.py "Notification Text Here"

...and it will display in the OS X menubar. Clicking the notification will exit the application.

Screenshot

Add the location of the script to your path (or just create an alias to the script itself) and you will be able to use it from anywhere in the Terminal.

The problem with the existing implementation is that it logs using NSLog, which writes to stderr by default. You will need to suppress the NSLog messages to get any real use out of the script.

My approach is to add the following function to your ~/.bash_profile and then call the function when you want to add a notification to the menubar:

# menubarnotifier.py function
mn () {
    /path/to/menubarnotifier.py "$1" 2>/dev/null &
}

So you can use it with just:

mn "Notification Text Here"

And it will not log to stdout. It will also run in the background to not require Ctrl+C.


The script uses PyObjC, so you will need to install that if you don't have it already. I installed it using MacPorts easily with sudo port install py27-pyobjc* but I think you can get away with just sudo port install py27-pyobjc py27-pyobjc-cocoa.

The idea is to use NSStatusBar.systemStatusBar().statusItemWithLength_() to create a new item in the OS X menubar.

self.statusItem = NSStatusBar.systemStatusBar().statusItemWithLength_(NSVariableStatusItemLength)
self.statusItem.setTitle_(display_text)

I'll add more functionality later (such as images like you mentioned in your question). I also need to figure out a better logging mechanism so you can just run it and not see the NSLog output. For now, use the bash function that I wrote above to get this to operate the way you want it to.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .