Heikki @ home

Loose leaves from my tree

OS X system notifications from terminal tasks with fish

This blog entry shows how to get OS X system notifications from terminal tasks. Its really great idea is to trigger notifications only when terminal is no longer the frontmost application.

The fish shell

The detailed instructions give the commands for zsh which is like bash on steroids. I, however, use fish that does most things differently (and better). I record here how I modified the scripts for fish.

The crucial thing with fish shell in OS X is not to declare it as your login shell. It is not POSIX compliant and things will not work as expected.

The sane way to do it is to install fish from homebrew (brew install fish) and not make it the default shell, but set it in the terminal preferences. In iTerm2, go to Preferences/Profiles/General and set the command for the default profile to /usr/local/bin/fish.

Installation of scripts

The osascript determines if the system notification is needed and does the call. Put the code as it is on the web page into a file named notifyme, set execute permissions (e.g. chmod 755 notifyme) and place somewhere which is on your path (e.g. ~/bin or /usr/local/bin).

The fish code needs to be placed in your main fish configuration file.

The default fish configuration file is ~/.config/fish/config.fish, but I use fish with a framework called oh-my-fish that stores its configuration in ~/.config/omf/init.fish.

Here is the function f_notifyme rewritten using fish syntax that collects the information from shell and calls the osascript:

function f_notifyme -d "OS X system nofication"
  set LAST_EXIT_CODE $status
  set CMD (history|head -1)
  # No point in waiting for the command to complete
  notifyme $CMD $LAST_EXIT_CODE

The function f_notifyme needs to called every time a command finishes. The means that the shell prompt needs to be modified. In fish, this is done using functions fish_prompt and fish_right_prompt. It depends on your setup which one you want to modify. This example assumes that you are not using the right prompt for anything else:

function fish_right_prompt -d "create system nofication at prompt"

Save the file and start a new fish shell to start receiving notifications.


If you want to tweek the setup further, you'll find debugging easier if you temporarily change reverse the logic in the notifyme osascript by setting line 6 to =if frontApp is "iTerm2"=. You can then call it from the command prompt and see the notification to come up (e.g f_notifyme or notifyme test status).