Clicky

Sending Events to Fantastical 2 from Emacs

August 16, 2015

Last week, I wrote a function for sending tasks to OmniFocus from Emacs. This involved creating and running short AppleScript snippet on the fly to communicate with OmniFocus. Once this basic structure is available, it’s fairly easy to connect with other applications. The next application on my list was Fantastical 2.

A Newly Parsed Calendar Event from Emacs
A Newly Parsed Calendar Event from Emacs

The send-region-to-fantastical function below takes first line of the region and sends it to Fantastical which opens the mini window (from the menu bar) and, using its natural language parsing, creates a new event which you can then review and add to your calendar. Any remaining lines in the region are added to the event as a note. Finally, it appends a note indicating that the task came from Emacs along with a timestamp.

Sending the Emacs Region to OmniFocus
Sending the Emacs Region to OmniFocus
(defun send-region-to-fantastical (beg end)
  "Send the selected region to Fantastical.
Parse the first line to create the event and use the second
and subsequent lines as the event note."
  (interactive "r")
  (let* ((region (buffer-substring-no-properties beg end))
         (match (string-match "^\\(.*\\)$" region))
         (event (substring region (match-beginning 1) (match-end 1)))
         (notes (if (< (match-end 0) (length region))
                   (concat (substring region (+ (match-end 0) 1) nil) "\n\n")
                 "")))
    (do-applescript
     (format "set theDate to current date
              set eventText to %s
              set eventNotes to %s
              set eventNotes to (eventNotes) & \"Added from Emacs on \" & (theDate as string)
              tell application \"Fantastical\"
                parse sentence (eventText) notes (eventNotes)
              end tell"
             (applescript-quote-string event)
             (applescript-quote-string notes)))))

I call send-region-to-fantastical by pressing C-c f via the following global keybinding:

(global-set-key (kbd "C-c f") 'send-region-to-fantastical)

Note that this function calls applescript-quote-string, which is a function originally from omnifocus-capture.el from a previous post. You can download both functions together here: fantastical-capture.el.

If you don’t have Fantastical 2 already, you can purchase the Mac, iPhone, and iPad versions here: