If you want to integrate your Org mode agenda into other calendars, this is an easy way to do it. There are multiple reasons why you might want to do such a thing - for example to get notifications in your favorite calendar application or to share your daily agenda with coworkers.

The following Elisp code exports your Org mode agenda files to an iCalendar file. iCalendar (or ICS) is a standard for exchanging calendaring information. If you host this file on a web server, it can be consumed by any calendar application that supports iCalendar. Here are some guides for different calendar app providers:

(setq org-directory "~/Dropbox/org/")
(defun set-org-agenda-files ()
  "Set different org-files to be used in `org-agenda`."
  (setq org-agenda-files (list (concat org-directory "things.org")
                               (concat org-directory "reference.org")
                               (concat org-directory "media.org")
                               (concat org-directory "shared_with/bob.org")
                               "~/src/your_company/admin/things.org"
                               "~/src/your_customer/admin/pm.org")))

;; Setting variables for the ics file path
(setq org-agenda-private-local-path "/tmp/dummy.ics")
(setq org-agenda-private-remote-path "/sshx:user@host:path/dummy.ics")

;; Define a custom command to save the org agenda to a file
(setq org-agenda-custom-commands
      `(("X" agenda "" nil ,(list org-agenda-private-local-path))))

(defun org-agenda-export-to-ics ()
  (set-org-agenda-files)
  ;; Run all custom agenda commands that have a file argument.
  (org-batch-store-agenda-views)

  ;; Org mode correctly exports TODO keywords as VTODO events in ICS.
  ;; However, some proprietary calendars do not really work with
  ;; standards (looking at you Google), so VTODO is ignored and only
  ;; VEVENT is read.
  (with-current-buffer (find-file-noselect org-agenda-private-local-path)
    (goto-char (point-min))
    (while (re-search-forward "VTODO" nil t)
      (replace-match "VEVENT"))
    (save-buffer))

  ;; Copy the ICS file to a remote server (Tramp paths work).
  (copy-file org-agenda-private-local-path org-agenda-private-remote-path t))

You could run the function org-agenda-export-to-ics as a hook whenever you change an agenda file. Since I'm editing my Org files not just with Emacs, but also with organice, I'm doing this on a regular basis in a cron job which runs this code:

#!/bin/bash
emacs -batch -l ~/.emacs.d/init.el -eval "(org-agenda-export-to-ics)" -kill

if [[ "$?" != 0 ]]; then
  notify-send -u critical "exporting org agenda failed"
fi

The hourly cron job looks like this:

0 * * * * /home/munen/bin/export-org-agenda.sh

If you liked this post, please consider supporting our Free and Open Source software work – you can sponsor us on Github and Patreon or star our FLOSS repositories.