3

I just started using erc in emacs and I've built emacs with --with-dbus compile option. I enabled notifications (so that I can get a notifications when my erc nickname gets mentioned) module in Erc modules list. But, whenever my nick gets mentioned in an erc channel, dbus gives me this error message:Error: (dbus-error "No connection to bus" :session). I've no idea how to fix this :(

How can I get notifications to work for macOS?

Edit: I've started the dbus process with: brew services start dbus

Chakravarthy Raghunandan
  • 3,132
  • 2
  • 18
  • 42
  • Is there an application you need to run to convert dbus messages to macOS notifications? BTW, you can create notifications using the "display" shell command. I'd imagine there's some way to get erc to use that instead of dbus, but I don't know. – Alan Third Dec 01 '16 at 13:09
  • @AlanThird I found out how to get notifications from ERC nick mentions via terminal-notifier. It sends notifications to the notification centre. A reddit user pointed out the solution. I'm waiting for him to write an answer here. – Chakravarthy Raghunandan Dec 01 '16 at 13:51
  • 1
    @AlanThird [This](https://www.reddit.com/r/emacs/comments/5fvibz/how_to_get_notifications_for_erc_in_macos/) is the reddit thread about this discussion – Chakravarthy Raghunandan Dec 01 '16 at 13:56
  • One option would be to set up an applescript inside an elisp function and have it run whenever needed, similar to the answer in the related thread on reddit re erc-terminal-notifier.el. You can Google emacs applescript for some ideas of how to do this, and you'll need to decide what you want the applescript to do -- e.g., display a notification; or, say "Hello, you've got a message". – lawlist Dec 01 '16 at 17:03
  • @lawlist, Ahh I am not too sure I'd want to spend time doing all that. I know nothing about apple script. I guess I'll just stick to using `terminal-notifier`. It was awfully simple to setup and use. Thanks for the info though. – Chakravarthy Raghunandan Dec 01 '16 at 17:45

3 Answers3

6

Another way is to use the ns-do-applescript elisp function:

(ns-do-applescript "display notification \"hello world\"")

or

(ns-do-applescript "display notification \"hello world\" with title \"some title\"")

to run AppleScript from Emacs.

More info on what commands are available in AppleScript:

https://developer.apple.com/library/content/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html

emil
  • 161
  • 4
1

Thanks to jbornhold answer in this reddit discussion, I was able to enable notifications for erc nick mentions.

Here's how you can do it:-

  • install terminal notifier with brew install terminal-notifier
  • install erc-terminal-notifier.el and add (require 'erc-terminal-notifier) in your .emacs

You might want to enable -reply option in erc-terminal-notifier.el so that you get alert style notifications.

Chakravarthy Raghunandan
  • 3,132
  • 2
  • 18
  • 42
  • _Just_ installing erc-terminal-notifier and terminal-notifier doesn't do it for me (and adding require in the emacs file). Erc still seems to still attempt sending messages to dbus. Is there a step that is not documented or I missed? The documentation for erc-terminal-notifier just seems to talk about installation. – Ashesh Aug 05 '18 at 12:56
1

Actually emil's suggestion is good. A quick and dirty way is override erc-notifications-notify. Add the below code to your ~/.emacs. No need to install dbus & extra files.

(require 'erc-desktop-notifications)

(defun erc-notifications-notify (nick msg)
  "Notify that NICK send some MSG via AppleScript."
    (ns-do-applescript
     (concat "display notification \"" (oz/escape-applescript msg)
             "\" with title \"" (oz/escape-applescript nick) "\"")))

(defun oz/escape-applescript (str)
  "Quote \\ and \"."
  (let ((len (length str)) (i 0) (q "") char)
    (while (< i len)
      (setq char (substring str i (1+ i))
            i (1+ i))
      (when (or (string= char "\\") (string= char "\""))
        (setq q (concat q "\\")))
      (setq q (concat q char)))
    q))

Notifications are shown as alerts or banners, depending on your settings in System Preferences > Notifications > Emacs.

Yez Ezey
  • 11
  • 2
  • Overriding such functions wholesale is pretty ugly. We have `advice-add` to do it in a more polite way. For `oz/escape-applescript`, I recommend you use `replace-regexp-in-string`. – Stefan Jun 25 '19 at 16:09
  • Why? The advice feature lets you add to the existing definition of a function, by advising the function. But we don't need the original code of `erc-notifications-notify` at all here. And personally I don't like regexp because it makes code unreadable. `(replace-regexp-in-string "\\([\\\"]\\)" "\\\\\\1" str))` I am basically C/C++ programmer. – Yez Ezey Jun 26 '19 at 19:00
  • You can also completely override the function with `add-advice`, but with the advantage that it's properly recorded, so `C-h f` will tell you about it and you can de-override it later if you want. – Stefan Jun 26 '19 at 20:41
  • The main reason I suggested `replace-regexp-in-string` is because your loop has a classic O(N^2) behavior since it loops N times and each iteration uses `concat` which is itself O(N). – Stefan Jun 26 '19 at 20:43