Create a gist now

Instantly share code, notes, and snippets.

Sending Events to Fantastical 2 from Emacs <http://jblevins.org/log/emacs-fantastical>
;; To send events from Emacs to Fantastical using Control-C Control-F,
;; drop this script in your Emacs load path and add lines like these to
;; your .emacs:
;;
;; (autoload 'send-region-to-fantastical "fantastical-capture" "Send region to Fantastical" t)
;; (global-set-key (kbd "C-c C-f") 'send-region-to-fantastical)
;;
;; See http://jblevins.org/log/emacs-fantastical for details.
(defun applescript-quote-string (argument)
"Quote a string for passing as a string to AppleScript."
(if (or (not argument) (string-equal argument ""))
"\"\""
;; Quote using double quotes, but escape any existing quotes or
;; backslashes in the argument with backslashes.
(let ((result "")
(start 0)
end)
(save-match-data
(if (or (null (string-match "[^\"\\]" argument))
(< (match-end 0) (length argument)))
(while (string-match "[\"\\]" argument start)
(setq end (match-beginning 0)
result (concat result (substring argument start end)
"\\" (substring argument end (1+ end)))
start (1+ end))))
(concat "\"" result (substring argument start) "\"")))))
(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)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment