diff options
Diffstat (limited to 'contrib/lisp/org-mac-link.el')
-rw-r--r-- | contrib/lisp/org-mac-link.el | 255 |
1 files changed, 166 insertions, 89 deletions
diff --git a/contrib/lisp/org-mac-link.el b/contrib/lisp/org-mac-link.el index 56d67e1..ae2def3 100644 --- a/contrib/lisp/org-mac-link.el +++ b/contrib/lisp/org-mac-link.el @@ -18,6 +18,18 @@ ;; Author: Mike McLean <mike.mclean@pobox.com> ;; Add support for Microsoft Outlook for Mac as Org mode links ;; +;; Version: 1.3 +;; Author: Alan Schmitt <alan.schmitt@polytechnique.org> +;; Consistently use `org-mac-paste-applescript-links' +;; +;; Version 1.4 +;; Author: Mike McLean <mike.mclean@pobox.com> +;; Make the path to Microsoft Outlook a `defcustom' +;; +;; Version 1.5 +;; Author: Mike McLean <mike.mclean@pobox.com> +;; Add Support for Evernote +;; ;; This file is not part of GNU Emacs. ;; ;; This program is free software; you can redistribute it and/or modify @@ -58,6 +70,7 @@ ;; Skim.app - Grab a link to the selected page in the topmost pdf document ;; Microsoft Outlook.app - Grab a link to the selected message in the message list ;; DEVONthink Pro Office.app - Grab a link to the selected DEVONthink item(s); open DEVONthink item by reference +;; Evernote.app - Grab a link to the selected Evernote item(s); open Evernote item by ID ;; ;; ;; Installation: @@ -109,6 +122,12 @@ :group 'org-mac-link :type 'boolean) +(defcustom org-mac-outlook-path "/Applications/Microsoft Outlook.app" + "The path to the installed copy of Microsoft Outlook.app. Do not escape spaces as the AppleScript call will quote this string." + :tag "Path to Microsoft Outlook" + :group 'org-mac-link + :type 'string) + (defcustom org-mac-grab-devonthink-app-p t "Add menu option [d]EVONthink to grab links from DEVONthink Pro Office.app." :tag "Grab DEVONthink Pro Office.app links" @@ -165,6 +184,12 @@ :group 'org-mac-link :type 'boolean) +(defcustom org-mac-grab-Acrobat-app-p t + "Add menu option [A]crobat to grab page links from Acrobat.app." + :tag "Grab Acrobat.app page links" + :group 'org-mac-link + :type 'boolean) + (defgroup org-mac-flagged-mail nil "Options foring linking to flagged Mail.app messages." :tag "Org Mail.app" @@ -175,6 +200,23 @@ :group 'org-mac-flagged-mail :type 'string) +(defcustom org-mac-grab-Evernote-app-p + (< 0 (length (shell-command-to-string + "mdfind kMDItemCFBundleIdentifier == 'com.evernote.Evernote'"))) + "Add menu option [e]vernote to grab note links from Evernote.app." + :tag "Grab Evernote.app note links" + :group 'org-mac-link + :type 'boolean) + +(defcustom org-mac-evernote-path (replace-regexp-in-string (rx (* (any " \t\n")) eos) + "" + (shell-command-to-string + "mdfind kMDItemCFBundleIdentifier == 'com.evernote.Evernote'")) + "The path to the installed copy of Evernote.app. Do not escape spaces as the AppleScript call will quote this string." + :tag "Path to Evernote" + :group 'org-mac-link + :type 'string) + ;; In mac.c, removed in Emacs 23. (declare-function do-applescript "org-mac-message" (script)) @@ -206,13 +248,15 @@ When done, go grab the link, and insert it at point." ("f" "irefox" org-mac-firefox-insert-frontmost-url ,org-mac-grab-Firefox-app-p) ("v" "imperator" org-mac-vimperator-insert-frontmost-url ,org-mac-grab-Firefox+Vimperator-p) ("c" "hrome" org-mac-chrome-insert-frontmost-url ,org-mac-grab-Chrome-app-p) + ("e" "evernote" org-mac-evernote-note-insert-selected ,org-mac-grab-Evernote-app-p) ("t" "ogether" org-mac-together-insert-selected ,org-mac-grab-Together-app-p) - ("S" "kim" org-mac-skim-insert-page ,org-mac-grab-Skim-app-p))) + ("S" "kim" org-mac-skim-insert-page ,org-mac-grab-Skim-app-p) + ("A" "crobat" org-mac-acrobat-insert-page ,org-mac-grab-Acrobat-app-p))) (menu-string (make-string 0 ?x)) input) ;; Create the menu string for the keymap - (mapc '(lambda (descriptor) + (mapc (lambda (descriptor) (when (elt descriptor 3) (setf menu-string (concat menu-string "[" (elt descriptor 0) "]" @@ -223,7 +267,7 @@ When done, go grab the link, and insert it at point." ;; Prompt the user, and grab the link (message menu-string) (setq input (read-char-exclusive)) - (mapc '(lambda (descriptor) + (mapc (lambda (descriptor) (let ((key (elt (elt descriptor 0) 0)) (active (elt descriptor 3)) (grab-function (elt descriptor 2))) @@ -299,15 +343,7 @@ The links are of the form <link>::split::<name>." (defun org-mac-firefox-get-frontmost-url () (interactive) (message "Applescript: Getting Firefox url...") - (let* ((url-and-title (org-as-mac-firefox-get-frontmost-url)) - (split-link (split-string url-and-title "::split::")) - (URL (car split-link)) - (description (cadr split-link)) - (org-link)) - (when (not (string= URL "")) - (setq org-link (org-make-link-string URL description))) - (kill-new org-link) - org-link)) + (org-mac-paste-applescript-links (org-as-mac-firefox-get-frontmost-url))) (defun org-mac-firefox-insert-frontmost-url () (interactive) @@ -345,15 +381,7 @@ The links are of the form <link>::split::<name>." (defun org-mac-vimperator-get-frontmost-url () (interactive) (message "Applescript: Getting Vimperator url...") - (let* ((url-and-title (org-as-mac-vimperator-get-frontmost-url)) - (split-link (split-string url-and-title "::split::")) - (URL (car split-link)) - (description (cadr split-link)) - (org-link)) - (when (not (string= URL "")) - (setq org-link (org-make-link-string URL description))) - (kill-new org-link) - org-link)) + (org-mac-paste-applescript-links (org-as-mac-vimperator-get-frontmost-url))) (defun org-mac-vimperator-insert-frontmost-url () (interactive) @@ -383,15 +411,7 @@ The links are of the form <link>::split::<name>." (defun org-mac-chrome-get-frontmost-url () (interactive) (message "Applescript: Getting Chrome url...") - (let* ((url-and-title (org-as-mac-chrome-get-frontmost-url)) - (split-link (split-string url-and-title "::split::")) - (URL (car split-link)) - (description (cadr split-link)) - (org-link)) - (when (not (string= URL "")) - (setq org-link (org-make-link-string URL description))) - (kill-new org-link) - org-link)) + (org-mac-paste-applescript-links (org-as-mac-chrome-get-frontmost-url))) (defun org-mac-chrome-insert-frontmost-url () (interactive) @@ -422,8 +442,7 @@ The links are of the form <link>::split::<name>." ;; Handle links from together.app - -(org-add-link-type "x-together-item" 'org-mac-together-item-open) +(org-link-set-parameters "x-together-item" :follow #'org-mac-together-item-open) (defun org-mac-together-item-open (uid) "Open UID, which is a reference to an item in Together." @@ -478,8 +497,7 @@ The links are of the form <link>::split::<name>." ;; Handle links from AddressBook.app - -(org-add-link-type "addressbook" 'org-mac-addressbook-item-open) +(org-link-set-parameters "addressbook" :follow #'org-mac-addressbook-item-open) (defun org-mac-addressbook-item-open (uid) "Open UID, which is a reference to an item in the addressbook." @@ -512,7 +530,7 @@ The links are of the form <link>::split::<name>." ;; ;; Original code & idea by Christopher Suckling (org-mac-protocol) -(org-add-link-type "skim" 'org-mac-skim-open) +(org-link-set-parameters "skim" :follow #'org-mac-skim-open) (defun org-mac-skim-open (uri) "Visit page of pdf in Skim" @@ -557,30 +575,73 @@ The links are of the form <link>::split::<name>." (defun org-mac-skim-get-page () (interactive) (message "Applescript: Getting Skim page link...") - (let* ((link-and-descr (as-get-skim-page-link)) - (split-link (split-string link-and-descr "::split::")) - (link (car split-link)) - (description (cadr split-link)) - (org-link)) - (when (not (string= link "")) - (setq org-link (org-make-link-string link description))) - (kill-new org-link) - org-link)) + (org-mac-paste-applescript-links (as-get-skim-page-link))) (defun org-mac-skim-insert-page () (interactive) (insert (org-mac-skim-get-page))) +;; Handle links from Adobe Acrobat Pro.app +;; +;; Original code & idea by Christopher Suckling (org-mac-protocol) +;; +;; The URI format is path_to_pdf_file::page_number + +(org-link-set-parameters "acrobat" :follow #'org-mac-acrobat-open) + +(defun org-mac-acrobat-open (uri) + "Visit page of pdf in Acrobat" + (let* ((page (when (string-match "::\\(.+\\)\\'" uri) + (match-string 1 uri))) + (document (substring uri 0 (match-beginning 0)))) + (do-applescript + (concat + "tell application \"Adobe Acrobat Pro\"\n" + " activate\n" + " set theDoc to \"" document "\"\n" + " set thePage to " page "\n" + " open theDoc\n" + " tell PDF Window 1\n" + " goto page thePage\n" + " end tell\n" + "end tell")))) + +;; The applescript returns link in the format +;; "adobe:path_to_pdf_file::page_number::split::document_title, p.page_label" + +(defun org-mac-as-get-acrobat-page-link () + (do-applescript + (concat + "tell application \"Adobe Acrobat Pro\"\n" + " set theDoc to active doc\n" + " set theWindow to (PDF Window 1 of theDoc)\n" + " set thePath to (file alias of theDoc)\n" + " set theTitle to (name of theWindow)\n" + " set thePage to (page number of theWindow)\n" + " set theLabel to (label text of (page thePage of theWindow))\n" + "end tell\n" + "set theResult to \"acrobat:\" & thePath & \"::\" & thePage & \"::split::\" & theTitle & \", p.\" & theLabel\n" + "return theResult as string\n"))) + +(defun org-mac-acrobat-get-page () + (interactive) + (message "Applescript: Getting Acrobat page link...") + (org-mac-paste-applescript-links (org-mac-as-get-acrobat-page-link))) + +(defun org-mac-acrobat-insert-page () + (interactive) + (insert (org-mac-acrobat-get-page))) + ;; Handle links from Microsoft Outlook.app -(org-add-link-type "mac-outlook" 'org-mac-outlook-message-open) +(org-link-set-parameters "mac-outlook" :follow #'org-mac-outlook-message-open) (defun org-mac-outlook-message-open (msgid) "Open a message in Outlook" (do-applescript (concat - "tell application \"/Applications/Microsoft Office 2011/Microsoft Outlook.app\"\n" + "tell application \"" org-mac-outlook-path "\"\n" (format "open message id %s\n" (substring-no-properties msgid)) "activate\n" "end tell"))) @@ -589,7 +650,7 @@ The links are of the form <link>::split::<name>." "AppleScript to create links to selected messages in Microsoft Outlook.app." (do-applescript (concat - "tell application \"/Applications/Microsoft Office 2011/Microsoft Outlook.app\"\n" + "tell application \"" org-mac-outlook-path "\"\n" "set msgCount to count current messages\n" "if (msgCount < 1) then\n" "return\n" @@ -640,27 +701,12 @@ The Org-syntax text will be pushed to the kill ring, and also returned." (interactive "sLink to (s)elected or (f)lagged messages: ") (setq select-or-flag (or select-or-flag "s")) (message "Org Mac Outlook: searching mailboxes...") - (let* ((as-link-list - (if (string= select-or-flag "s") - (org-as-get-selected-outlook-mail) - (if (string= select-or-flag "f") - (org-sh-get-flagged-outlook-mail) - (error "Please select \"s\" or \"f\"")))) - (link-list - (mapcar - (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x) (setq x (match-string 1 x))) x) - (split-string as-link-list "[\r\n]+"))) - split-link URL description orglink orglink-insert rtn orglink-list) - (while link-list - (setq split-link (split-string (pop link-list) "::split::")) - (setq URL (car split-link)) - (setq description (cadr split-link)) - (when (not (string= URL "")) - (setq orglink (org-make-link-string URL description)) - (push orglink orglink-list))) - (setq rtn (mapconcat 'identity orglink-list "\n")) - (kill-new rtn) - rtn)) + (org-mac-paste-applescript-links + (if (string= select-or-flag "s") + (org-as-get-selected-outlook-mail) + (if (string= select-or-flag "f") + (org-sh-get-flagged-outlook-mail) + (error "Please select \"s\" or \"f\""))))) (defun org-mac-outlook-message-insert-selected () "Insert a link to the messages currently selected in Microsoft Outlook.app. @@ -695,11 +741,60 @@ after heading." (insert "\n") (org-insert-heading nil t) (insert org-heading "\n" (org-mac-outlook-message-get-links "f")))))) + +;; Handle links from Evernote.app + +(org-link-set-parameters "mac-evernote" :follow #'org-mac-evernote-note-open) + +(defun org-mac-evernote-note-open (noteid) + "Open a note in Evernote" + (do-applescript + (concat + "tell application \"" org-mac-evernote-path "\"\n" + " set theNotes to get every note of every notebook where its local id is \"" (substring-no-properties noteid) "\"\n" + " repeat with _note in theNotes\n" + " if length of _note is not 0 then\n" + " set _selectedNote to _note\n" + " end if\n" + " end repeat\n" + " open note window with item 1 of _selectedNote\n" + " activate\n" + "end tell"))) + +(defun org-as-get-selected-evernote-notes () + "AppleScript to create links to selected notes in Evernote.app." + (do-applescript + (concat + "tell application \"" org-mac-evernote-path "\"\n" + " set noteCount to count selection\n" + " if (noteCount < 1) then\n" + " return\n" + " end if\n" + " set theLinkList to {}\n" + " set theSelection to selection\n" + " repeat with theNote in theSelection\n" + " set theTitle to title of theNote\n" + " set theID to local id of theNote\n" + " set theURL to \"mac-evernote:\" & theID\n" + " set theLink to theURL & \"::split::\" & theTitle & \"\n\"\n" + " copy theLink to end of theLinkList\n" + " end repeat\n" + " return theLinkList as string\n" + "end tell\n"))) + +(defun org-mac-evernote-note-insert-selected () + "Insert a link to the notes currently selected in Evernote.app. +This will use AppleScript to get the note id and the title of the +note(s) in Evernote.app and make a link out of it/them." + (interactive) + (message "Org Mac Evernote: searching notes...") +(insert (org-mac-paste-applescript-links + (org-as-get-selected-evernote-notes)))) ;; Handle links from DEVONthink Pro Office.app -(org-add-link-type "x-devonthink-item" 'org-devonthink-item-open) +(org-link-set-parameters "x-devonthink-item" :follow #'org-devonthink-item-open) (defun org-devonthink-item-open (uid) "Open UID, which is a reference to an item in DEVONthink Pro Office." @@ -734,25 +829,7 @@ selected items in DEVONthink Pro Office.app and make links out of it/them. This function will push the Org-syntax text to the kill ring, and also return it." (message "Org Mac DEVONthink: looking for selected items...") - (let* ((as-link-list (org-as-get-selected-devonthink-item)) - (link-list (if as-link-list - (mapcar - (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x) - (setq x (match-string 1 x))) - x) - (split-string as-link-list "[\r\n]+")) - nil)) - orglink-list) - (while link-list - (let* ((current-item (pop link-list))) - (message "current item: %s" current-item) - (when (and current-item (not (string= current-item ""))) - (let* ((split-link (split-string current-item "::split::")) - (orglink (org-make-link-string - (url-encode-url (car split-link)) - (cadr split-link)))) - (push orglink orglink-list))))) - (kill-new (mapconcat 'identity orglink-list "\n")))) + (org-mac-paste-applescript-links (org-as-get-selected-devonthink-item))) (defun org-mac-devonthink-item-insert-selected () "Insert a link to the item(s) currently selected in DEVONthink Pro Office. @@ -764,7 +841,7 @@ selected items in DEVONthink Pro Office and make link(s) out of it/them." ;; Handle links from Mail.app -(org-add-link-type "message" 'org-mac-message-open) +(org-link-set-parameters "message" :follow #'org-mac-message-open) (defun org-mac-message-open (message-id) "Visit the message with MESSAGE-ID. |