Monday, May 26, 2014

Sending notifications from Emacs (Mac OS X)


Sending notifications from emacs is something I find useful. In an earlier blog post I talked about how to use Growl to do so. http://justinsboringpage.blogspot.com/2009/09/making-emacs-growl.html

Well now you don't need Growl any longer. There's a neat github project called terminal notifier https://github.com/alloy/terminal-notifier which let's you send notifications from the terminal.

You can install it simply, via Homebrew or Rubygems as follows:
$ [sudo] gem install terminal-notifier
OR
brew install terminal-notifier
Then you can send notifications using a command like this:

terminal-notifier -message "hello"

Finally in order to send the notification from emacs we need to write a little Emacs lisp.

Check out this gist for the code I use:

https://gist.github.com/justinhj/eb2d354d06631076566f#file-gistfile1-el

This lets you send a notification in the future using M-x timed-notification

You are prompted for a time, and the format of that time can be given in a human readable way such as "2 seconds" or "5 minutes" (If you're curious for the allowed options look at the info page in emacs for the function timer-duration )

Then you are prompted for the message "Go to the store", and the message will be sent.

The code is very simple, it simply uses run-at-time to run the terminal command in the future. A useful command is find-executable, which given a name will find that executable in your path and run it. This makes configuring tools like this less effort.

Troubleshooting

Hey if it doesn't work first time what can you do?


  1. Check if terminal notify is installed correctly by running at terminal
  2. If that succeeds and you don't get a message check if you have enabled do not disturb mode
  3. Otherwise if that succeeds and yet emacs isn't sending messages you likely don't have the executable on the path. M-x customize-variable exec-path








4 comments:

Steve Purcell said...

I highly encourage using John Wiegley's "alert" package, which directly supports "terminal-notifier", as well as a variety of other notification methods.

Justin said...

Thanks, that looks very comprehensive. My solution does what I need for now.

Robert said...

Two notes:
1. terminal-notifier is also available from Mac Ports
2. Wiegley's alert seems to still use only Growl, and not the built-in (Apple) notifications.

Anonymous said...

In 2017 it seems you need to use "reattach-to-user-namespace" wrapper around terminal-notifier. But otherwise, the solution here works.

I added define-jabber-alert macro call so that you can pick the new notification in the customization group (jabber alerts): https://gist.github.com/piotrklibert/37697db0fa742054835eafdfb9b7e492