From mboxrd@z Thu Jan 1 00:00:00 1970 From: Christopher Suckling Subject: Re: More Mac OS stuff: iCal.app - org integration Date: Fri, 5 Jun 2009 22:20:15 +0100 Message-ID: References: <75D88706-DB6C-4752-B9F6-3D6819101226@gmail.com> Mime-Version: 1.0 (Apple Message framework v935.3) Content-Type: multipart/mixed; boundary=Apple-Mail-13--358356771 Return-path: Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MCgpv-0002Eh-7A for emacs-orgmode@gnu.org; Fri, 05 Jun 2009 17:20:27 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MCgpq-000213-Cv for emacs-orgmode@gnu.org; Fri, 05 Jun 2009 17:20:26 -0400 Received: from [199.232.76.173] (port=59644 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MCgpq-00020o-8n for emacs-orgmode@gnu.org; Fri, 05 Jun 2009 17:20:22 -0400 Received: from mail-ew0-f210.google.com ([209.85.219.210]:38726) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MCgpp-0002nD-RR for emacs-orgmode@gnu.org; Fri, 05 Jun 2009 17:20:22 -0400 Received: by ewy6 with SMTP id 6so3704542ewy.42 for ; Fri, 05 Jun 2009 14:20:20 -0700 (PDT) In-Reply-To: <75D88706-DB6C-4752-B9F6-3D6819101226@gmail.com> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Carsten Dominik Cc: emacs org-mode mailing list , Christopher Suckling --Apple-Mail-13--358356771 Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit On 5 Jun 2009, at 17:30, Carsten Dominik wrote: > Hi Christopher, this looks very nice, a candidate for the contrib > directory? > > - Carsten > Would be delighted. Will write up the documentation on worg when it's in the contrib directory. Anything you need me to change in the commentary etc? Forgot to say in original version that this is Leopard only. Apple changed file format between Tiger and Leopard from one ics file per calendar to one ics file per event (for Spotlight searching). I've added a check for Leopard before calling the function that concatenates all the event ics files into a calendar ics file, but I've no longer a Tiger machine and can't recall whether the Tiger ics layout is as simple as I think it was. So if anyone is still running Tiger and may find this useful, would you be kind enough to test the new version and see if it works? Best wishes, Christopher > On Jun 5, 2009, at 1:50 PM, Christopher Suckling wrote: > >> The nature of my work means that I find it more convenient to work >> in the month view of iCal.app as my default diary with lots of >> allday and multiday events. Of course, I'd still like to see them >> in my daily org agenda. > --Apple-Mail-13--358356771 Content-Disposition: attachment; filename=org-mac-iCal.el Content-Type: application/octet-stream; x-unix-mode=0644; name="org-mac-iCal.el" Content-Transfer-Encoding: 7bit ;;; org-mac-iCal.el --- Imports events from iCal.app to the Emacs diary ;; Copyright (C) 2009 Christopher Suckling ;; Author: Christopher Suckling ;; This file is Free Software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 3, or (at your option) ;; any later version. ;; It is distributed in the hope that it will be useful, but WITHOUT ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public ;; License for more details. ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs; see the file COPYING. If not, write to the ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, ;; Boston, MA 02110-1301, USA. ;; Version: 0.1057.104 ;; Keywords: outlines, calendar ;;; Commentary: ;; ;; This file provides the import of events from Mac OS X 10.5 iCal.app ;; into the Emacs diary (it is not compatible with OS X < 10.5). The ;; function org-mac-iCal will import events in all checked iCal.app ;; calendars for the date range org-mac-iCal-range months, centered ;; around the current date. ;; ;; CAVEAT: This function is destructive; it will overwrite the current ;; contents of the Emacs diary. ;; ;; Installation: add (require 'org-mac-iCal) to your .emacs. ;; ;; If you view Emacs diary entries in org-agenda, the following hook ;; will ensure that all-day events are not orphaned below TODO items ;; and that any supplementary fields to events (e.g. Location) are ;; grouped with their parent event ;; ;; (add-hook 'org-agenda-cleanup-fancy-diary-hook ;; (lambda () ;; (goto-char (point-min)) ;; (save-excursion ;; (while (re-search-forward "^[a-z]" nil t) ;; (goto-char (match-beginning 0)) ;; (insert "0:00-24:00 "))) ;; (while (re-search-forward "^ [a-z]" nil t) ;; (goto-char (match-beginning 0)) ;; (save-excursion ;; (re-search-backward "^[0-9]+:[0-9]+-[0-9]+:[0-9]+ " nil t)) ;; (insert (match-string 0))))) ;;; Code: (defcustom org-mac-iCal-range 2 "The range in months to import iCal.app entries into the Emacs diary. The import is centered around today's date; thus a value of 2 imports entries for one month before and one month after today's date" :group 'org-time :type 'integer) (defun org-mac-iCal () "Selects checked calendars in iCal.app and imports them into the the Emacs diary" (interactive) ;; kill diary buffers then empty diary files to avoid duplicates (setq currentBuffer (buffer-name)) (setq openBuffers (mapcar (function buffer-name) (buffer-list))) (omi-kill-diary-buffer openBuffers) (with-temp-buffer (insert-file-contents diary-file) (delete-region (point-min) (point-max)) (write-region (point-min) (point-max) diary-file)) ;; determine available calendars (setq caldav-folders (directory-files "~/Library/Calendars" 1 ".*caldav$")) (setq caldav-calendars nil) (mapc (lambda (x) (setq caldav-calendars (nconc caldav-calendars (directory-files x 1 ".*calendar$")))) caldav-folders) (setq local-calendars nil) (setq local-calendars (directory-files "~/Library/Calendars" 1 ".*calendar$")) (setq all-calendars (append caldav-calendars local-calendars)) ;; parse each calendar's Info.plist to see if calendar is checked in iCal (setq all-calendars (delq 'nil (mapcar (lambda (x) (omi-checked x)) all-calendars))) ;; for each caledar, concatenate individual events into a single ics file (with-temp-buffer (shell-command "sw_vers" " *temp*") (when (re-search-backward "10.5" nil t) (omi-concat-leopard-ics all-calendars))) ;; move any caldav ics files to the same place as local ics files (mapc (lambda (x) (when (directory-files x 1 ".*ics$") (rename-file (car (directory-files x 1 ".*ics$")) (concat "~/Library/Calendars/" (car (directory-files x nil ".*ics$")))))) caldav-folders) ;; check calendar has contents and import (setq import-calendars (directory-files "~/Library/Calendars" 1 ".*ics$")) (mapc (lambda (x) (when (/= (nth 7 (file-attributes x 'string)) 0) (omi-import-ics x))) import-calendars) ;; tidy up intermediate files and buffers (setq usedCalendarsBuffers (mapcar (function buffer-name) (buffer-list))) (omi-kill-ics-buffer usedCalendarsBuffers) (setq usedCalendarsFiles (directory-files "~/Library/Calendars" 1 ".*ics$")) (omi-delete-ics-file usedCalendarsFiles) (switch-to-buffer currentBuffer)) (defun omi-concat-leopard-ics (list) "Leopard stores each iCal.app event in a separate ics file. Whilst useful for Spotlight indexing, this is less helpful for icalendar-import-file. omi-concat-leopard-ics concatenates these individual event files into a single ics file" (mapc (lambda (x) (setq omi-leopard-events (directory-files (concat x "/Events") 1 ".*ics$")) (with-temp-buffer (mapc (lambda (y) (insert-file-contents (expand-file-name y))) omi-leopard-events) (write-region (point-min) (point-max) (concat (expand-file-name x) ".ics")))) list)) (defun omi-import-ics (string) "Imports an ics file into the Emacs diary. First tidies up the ics file so that it is suitable for import and selects a sensible date range so that Emacs calendar view doesn't grind to a halt" (with-temp-buffer (insert-file-contents string) (goto-char (point-min)) (while (re-search-forward "^BEGIN:VCALENDAR$" nil t) (setq startEntry (match-beginning 0)) (re-search-forward "^END:VCALENDAR$" nil t) (setq endEntry (match-end 0)) (save-restriction (narrow-to-region startEntry endEntry) (goto-char (point-min)) (re-search-forward "\\(^DTSTART;.*:\\)\\([0-9][0-9][0-9][0-9]\\)\\([0-9][0-9]\\)" nil t) (if (or (eq (match-string 2) nil) (eq (match-string 3) nil)) (progn (setq yearEntry 0) (setq monthEntry 0)) (setq yearEntry (string-to-number (match-string 2))) (setq monthEntry (string-to-number (match-string 3)))) (setq year (string-to-number (format-time-string "%Y"))) (setq month (string-to-number (format-time-string "%m"))) (when (or (and (= yearEntry year) (or (< monthEntry (- month (/ org-mac-iCal-range 2))) (> monthEntry (+ month (/ org-mac-iCal-range 2))))) (< yearEntry (- year 1)) (> yearEntry (+ year 1)) (and (= yearEntry (- year 1)) (/= monthEntry 12)) (and (= yearEntry (+ year 1)) (/= monthEntry 1))) (delete-region startEntry endEntry)))) (while (re-search-forward "^END:VEVENT$" nil t) (delete-blank-lines)) (goto-line 1) (insert "BEGIN:VCALENDAR\n\n") (goto-line 2) (while (re-search-forward "^BEGIN:VCALENDAR$" nil t) (replace-match "\n")) (goto-line 2) (while (re-search-forward "^END:VCALENDAR$" nil t) (replace-match "\n")) (insert "END:VCALENDAR") (goto-line 1) (delete-blank-lines) (while (re-search-forward "^END:VEVENT$" nil t) (delete-blank-lines)) (goto-line 1) (while (re-search-forward "^ORG.*" nil t) (replace-match "\n")) (goto-line 1) (write-region (point-min) (point-max) string)) (icalendar-import-file string (expand-file-name "~/.emacs.d/diary"))) (defun omi-kill-diary-buffer (list) (mapc (lambda (x) (if (string-match "^diary" x) (kill-buffer x))) list)) (defun omi-kill-ics-buffer (list) (mapc (lambda (x) (if (string-match "ics$" x) (kill-buffer x))) list)) (defun omi-delete-ics-file (list) (mapc (lambda (x) (delete-file x)) list)) (defun omi-checked (directory) "Parse Info.plist in iCal.app calendar folder and determine whether Checked key is 1. If Checked key is not 1, remove calendar from list of calendars for import" (let* ((root (xml-parse-file (car (directory-files directory 1 "Info.plist")))) (plist (car root)) (dict (car (xml-get-children plist 'dict))) (keys (cdr (xml-node-children dict))) (keys (mapcar (lambda (x) (cond ((listp x) x))) keys)) (keys (delq 'nil keys))) (when (equal "1" (car (cddr (lax-plist-get keys '(key nil "Checked"))))) directory))) (provide 'org-mac-iCal) ;;; org-mac-iCal.el ends here --Apple-Mail-13--358356771 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Emacs-orgmode mailing list Remember: use `Reply All' to send replies to the list. Emacs-orgmode@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-orgmode --Apple-Mail-13--358356771--