org-mac-link.el 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. ;;; org-mac-link.el --- Insert org-mode links to items selected in various Mac apps
  2. ;;
  3. ;; Copyright (c) 2010-2015 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Anthony Lander <anthony.lander@gmail.com>
  6. ;; John Wiegley <johnw@gnu.org>
  7. ;; Christopher Suckling <suckling at gmail dot com>
  8. ;; Daniil Frumin <difrumin@gmail.com>
  9. ;; Alan Schmitt <alan.schmitt@polytechnique.org>
  10. ;; Mike McLean <mike.mclean@pobox.com>
  11. ;;
  12. ;;
  13. ;; Version: 1.1
  14. ;; Keywords: org, mac, hyperlink
  15. ;;
  16. ;; Version: 1.2
  17. ;; Keywords: outlook
  18. ;; Author: Mike McLean <mike.mclean@pobox.com>
  19. ;; Add support for Microsoft Outlook for Mac as Org mode links
  20. ;;
  21. ;; Version: 1.3
  22. ;; Author: Alan Schmitt <alan.schmitt@polytechnique.org>
  23. ;; Consistently use `org-mac-paste-applescript-links'
  24. ;;
  25. ;; Version 1.4
  26. ;; Author: Mike McLean <mike.mclean@pobox.com>
  27. ;; Make the path to Microsoft Outlook a `defcustom'
  28. ;;
  29. ;; Version 1.5
  30. ;; Author: Mike McLean <mike.mclean@pobox.com>
  31. ;; Add Support for Evernote
  32. ;;
  33. ;; This file is not part of GNU Emacs.
  34. ;;
  35. ;; This program is free software; you can redistribute it and/or modify
  36. ;; it under the terms of the GNU General Public License as published by
  37. ;; the Free Software Foundation; either version 3, or (at your option)
  38. ;; any later version.
  39. ;;
  40. ;; This program is distributed in the hope that it will be useful,
  41. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  42. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  43. ;; GNU General Public License for more details.
  44. ;;
  45. ;; You should have received a copy of the GNU General Public License
  46. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  47. ;;
  48. ;;; Commentary:
  49. ;;
  50. ;; This code allows you to grab either the current selected items, or
  51. ;; the frontmost url in various mac appliations, and insert them as
  52. ;; hyperlinks into the current org-mode document at point.
  53. ;;
  54. ;; This code is heavily based on, and indeed incorporates,
  55. ;; org-mac-message.el written by John Wiegley and Christopher
  56. ;; Suckling.
  57. ;;
  58. ;; Detailed comments for each application interface are inlined with
  59. ;; the code. Here is a brief overview of how the code interacts with
  60. ;; each application:
  61. ;;
  62. ;; Finder.app - grab links to the selected files in the frontmost window
  63. ;; Mail.app - grab links to the selected messages in the message list
  64. ;; AddressBook.app - Grab links to the selected addressbook Cards
  65. ;; Firefox.app - Grab the url of the frontmost tab in the frontmost window
  66. ;; Vimperator/Firefox.app - Grab the url of the frontmost tab in the frontmost window
  67. ;; Safari.app - Grab the url of the frontmost tab in the frontmost window
  68. ;; Google Chrome.app - Grab the url of the frontmost tab in the frontmost window
  69. ;; Together.app - Grab links to the selected items in the library list
  70. ;; Skim.app - Grab a link to the selected page in the topmost pdf document
  71. ;; Microsoft Outlook.app - Grab a link to the selected message in the message list
  72. ;; DEVONthink Pro Office.app - Grab a link to the selected DEVONthink item(s); open DEVONthink item by reference
  73. ;; Evernote.app - Grab a link to the selected Evernote item(s); open Evernote item by ID
  74. ;;
  75. ;;
  76. ;; Installation:
  77. ;;
  78. ;; add (require 'org-mac-link) to your .emacs, and optionally bind a
  79. ;; key to activate the link grabber menu, like this:
  80. ;;
  81. ;; (add-hook 'org-mode-hook (lambda ()
  82. ;; (define-key org-mode-map (kbd "C-c g") 'org-mac-grab-link)))
  83. ;;
  84. ;; Usage:
  85. ;;
  86. ;; Type C-c g (or whatever key you defined, as above), or type M-x
  87. ;; org-mac-grab-link RET to activate the link grabber. This will present
  88. ;; you with a menu to choose an application from which to grab a link
  89. ;; to insert at point. You may also type C-g to abort.
  90. ;;
  91. ;; Customizing:
  92. ;;
  93. ;; You may customize which applications appear in the grab menu by
  94. ;; customizing the group `org-mac-link'. Changes take effect
  95. ;; immediately.
  96. ;;
  97. ;;
  98. ;;; Code:
  99. (require 'org)
  100. (defgroup org-mac-link nil
  101. "Options for grabbing links from Mac applications."
  102. :tag "Org Mac link"
  103. :group 'org-link)
  104. (defcustom org-mac-grab-Finder-app-p t
  105. "Add menu option [F]inder to grab links from the Finder."
  106. :tag "Grab Finder.app links"
  107. :group 'org-mac-link
  108. :type 'boolean)
  109. (defcustom org-mac-grab-Mail-app-p t
  110. "Add menu option [m]ail to grab links from Mail.app."
  111. :tag "Grab Mail.app links"
  112. :group 'org-mac-link
  113. :type 'boolean)
  114. (defcustom org-mac-grab-Outlook-app-p t
  115. "Add menu option [o]utlook to grab links from Microsoft Outlook.app."
  116. :tag "Grab Microsoft Outlook.app links"
  117. :group 'org-mac-link
  118. :type 'boolean)
  119. (defcustom org-mac-outlook-path "/Applications/Microsoft Outlook.app"
  120. "The path to the installed copy of Microsoft Outlook.app. Do not escape spaces as the AppleScript call will quote this string."
  121. :tag "Path to Microsoft Outlook"
  122. :group 'org-mac-link
  123. :type 'string)
  124. (defcustom org-mac-grab-devonthink-app-p t
  125. "Add menu option [d]EVONthink to grab links from DEVONthink Pro Office.app."
  126. :tag "Grab DEVONthink Pro Office.app links"
  127. :group 'org-mac-link
  128. :type 'boolean)
  129. (defcustom org-mac-grab-Addressbook-app-p t
  130. "Add menu option [a]ddressbook to grab links from AddressBook.app."
  131. :tag "Grab AddressBook.app links"
  132. :group 'org-mac-link
  133. :type 'boolean)
  134. (defcustom org-mac-grab-Safari-app-p t
  135. "Add menu option [s]afari to grab links from Safari.app."
  136. :tag "Grab Safari.app links"
  137. :group 'org-mac-link
  138. :type 'boolean)
  139. (defcustom org-mac-grab-Firefox-app-p t
  140. "Add menu option [f]irefox to grab links from Firefox.app."
  141. :tag "Grab Firefox.app links"
  142. :group 'org-mac-link
  143. :type 'boolean)
  144. (defcustom org-mac-grab-Firefox+Vimperator-p nil
  145. "Add menu option [v]imperator to grab links from Firefox.app running the Vimperator plugin."
  146. :tag "Grab Vimperator/Firefox.app links"
  147. :group 'org-mac-link
  148. :type 'boolean)
  149. (defcustom org-mac-grab-Chrome-app-p t
  150. "Add menu option [c]hrome to grab links from Google Chrome.app."
  151. :tag "Grab Google Chrome.app links"
  152. :group 'org-mac-link
  153. :type 'boolean)
  154. (defcustom org-mac-grab-Together-app-p nil
  155. "Add menu option [t]ogether to grab links from Together.app."
  156. :tag "Grab Together.app links"
  157. :group 'org-mac-link
  158. :type 'boolean)
  159. (defcustom org-mac-grab-Skim-app-p
  160. (< 0 (length (shell-command-to-string
  161. "mdfind kMDItemCFBundleIdentifier == 'net.sourceforge.skim-app.skim'")))
  162. "Add menu option [S]kim to grab page links from Skim.app."
  163. :tag "Grab Skim.app page links"
  164. :group 'org-mac-link
  165. :type 'boolean)
  166. (defcustom org-mac-Skim-highlight-selection-p nil
  167. "Highlight the active selection when grabbing a link from Skim.app."
  168. :tag "Highlight selection in Skim.app"
  169. :group 'org-mac-link
  170. :type 'boolean)
  171. (defcustom org-mac-grab-Acrobat-app-p t
  172. "Add menu option [A]crobat to grab page links from Acrobat.app."
  173. :tag "Grab Acrobat.app page links"
  174. :group 'org-mac-link
  175. :type 'boolean)
  176. (defgroup org-mac-flagged-mail nil
  177. "Options foring linking to flagged Mail.app messages."
  178. :tag "Org Mail.app"
  179. :group 'org-link)
  180. (defcustom org-mac-mail-account nil
  181. "The Mail.app account in which to search for flagged messages."
  182. :group 'org-mac-flagged-mail
  183. :type 'string)
  184. (defcustom org-mac-grab-Evernote-app-p
  185. (< 0 (length (shell-command-to-string
  186. "mdfind kMDItemCFBundleIdentifier == 'com.evernote.Evernote'")))
  187. "Add menu option [e]vernote to grab note links from Evernote.app."
  188. :tag "Grab Evernote.app note links"
  189. :group 'org-mac-link
  190. :type 'boolean)
  191. (defcustom org-mac-evernote-path (replace-regexp-in-string (rx (* (any " \t\n")) eos)
  192. ""
  193. (shell-command-to-string
  194. "mdfind kMDItemCFBundleIdentifier == 'com.evernote.Evernote'"))
  195. "The path to the installed copy of Evernote.app. Do not escape spaces as the AppleScript call will quote this string."
  196. :tag "Path to Evernote"
  197. :group 'org-mac-link
  198. :type 'string)
  199. ;; In mac.c, removed in Emacs 23.
  200. (declare-function do-applescript "org-mac-message" (script))
  201. (unless (fboundp 'do-applescript)
  202. ;; Need to fake this using shell-command-to-string
  203. (defun do-applescript (script)
  204. (let (start cmd return)
  205. (while (string-match "\n" script)
  206. (setq script (replace-match "\r" t t script)))
  207. (while (string-match "'" script start)
  208. (setq start (+ 2 (match-beginning 0))
  209. script (replace-match "\\'" t t script)))
  210. (setq cmd (concat "osascript -e '" script "'"))
  211. (setq return (shell-command-to-string cmd))
  212. (concat "\"" (org-trim return) "\""))))
  213. (defun org-mac-grab-link ()
  214. "Prompt for an application to grab a link from.
  215. When done, go grab the link, and insert it at point."
  216. (interactive)
  217. (let* ((descriptors
  218. `(("F" "inder" org-mac-finder-insert-selected ,org-mac-grab-Finder-app-p)
  219. ("m" "ail" org-mac-message-insert-selected ,org-mac-grab-Mail-app-p)
  220. ("d" "EVONthink Pro Office" org-mac-devonthink-item-insert-selected
  221. ,org-mac-grab-devonthink-app-p)
  222. ("o" "utlook" org-mac-outlook-message-insert-selected ,org-mac-grab-Outlook-app-p)
  223. ("a" "ddressbook" org-mac-addressbook-insert-selected ,org-mac-grab-Addressbook-app-p)
  224. ("s" "afari" org-mac-safari-insert-frontmost-url ,org-mac-grab-Safari-app-p)
  225. ("f" "irefox" org-mac-firefox-insert-frontmost-url ,org-mac-grab-Firefox-app-p)
  226. ("v" "imperator" org-mac-vimperator-insert-frontmost-url ,org-mac-grab-Firefox+Vimperator-p)
  227. ("c" "hrome" org-mac-chrome-insert-frontmost-url ,org-mac-grab-Chrome-app-p)
  228. ("e" "evernote" org-mac-evernote-note-insert-selected ,org-mac-grab-Evernote-app-p)
  229. ("t" "ogether" org-mac-together-insert-selected ,org-mac-grab-Together-app-p)
  230. ("S" "kim" org-mac-skim-insert-page ,org-mac-grab-Skim-app-p)
  231. ("A" "crobat" org-mac-acrobat-insert-page ,org-mac-grab-Acrobat-app-p)))
  232. (menu-string (make-string 0 ?x))
  233. input)
  234. ;; Create the menu string for the keymap
  235. (mapc (lambda (descriptor)
  236. (when (elt descriptor 3)
  237. (setf menu-string (concat menu-string
  238. "[" (elt descriptor 0) "]"
  239. (elt descriptor 1) " "))))
  240. descriptors)
  241. (setf (elt menu-string (- (length menu-string) 1)) ?:)
  242. ;; Prompt the user, and grab the link
  243. (message menu-string)
  244. (setq input (read-char-exclusive))
  245. (mapc (lambda (descriptor)
  246. (let ((key (elt (elt descriptor 0) 0))
  247. (active (elt descriptor 3))
  248. (grab-function (elt descriptor 2)))
  249. (when (and active (eq input key))
  250. (call-interactively grab-function))))
  251. descriptors)))
  252. (defun org-mac-paste-applescript-links (as-link-list)
  253. "Paste in a list of links from an applescript handler.
  254. The links are of the form <link>::split::<name>."
  255. (let* ((noquote-as-link-list
  256. (if (string-prefix-p "\"" as-link-list)
  257. (substring as-link-list 1 -1)
  258. as-link-list))
  259. (link-list
  260. (mapcar (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x)
  261. (setq x (match-string 1 x)))
  262. x)
  263. (split-string noquote-as-link-list "[\r\n]+")))
  264. split-link URL description orglink orglink-insert rtn orglink-list)
  265. (while link-list
  266. (setq split-link (split-string (pop link-list) "::split::"))
  267. (setq URL (car split-link))
  268. (setq description (cadr split-link))
  269. (when (not (string= URL ""))
  270. (setq orglink (org-make-link-string URL description))
  271. (push orglink orglink-list)))
  272. (setq rtn (mapconcat 'identity orglink-list "\n"))
  273. (kill-new rtn)
  274. rtn))
  275. ;; Handle links from Firefox.app
  276. ;;
  277. ;; This code allows you to grab the current active url from the main
  278. ;; Firefox.app window, and insert it as a link into an org-mode
  279. ;; document. Unfortunately, firefox does not expose an applescript
  280. ;; dictionary, so this is necessarily introduces some limitations.
  281. ;;
  282. ;; The applescript to grab the url from Firefox.app uses the System
  283. ;; Events application to give focus to the firefox application, select
  284. ;; the contents of the url bar, and copy it. It then uses the title of
  285. ;; the window as the text of the link. There is no way to grab links
  286. ;; from other open tabs, and further, if there is more than one window
  287. ;; open, it is not clear which one will be used (though emperically it
  288. ;; seems that it is always the last active window).
  289. (defun org-as-mac-firefox-get-frontmost-url ()
  290. (let ((result
  291. (do-applescript
  292. (concat
  293. "set oldClipboard to the clipboard\n"
  294. "set frontmostApplication to path to frontmost application\n"
  295. "tell application \"Firefox\"\n"
  296. " activate\n"
  297. " delay 0.15\n"
  298. " tell application \"System Events\"\n"
  299. " keystroke \"l\" using {command down}\n"
  300. " keystroke \"a\" using {command down}\n"
  301. " keystroke \"c\" using {command down}\n"
  302. " end tell\n"
  303. " delay 0.15\n"
  304. " set theUrl to the clipboard\n"
  305. " set the clipboard to oldClipboard\n"
  306. " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
  307. "end tell\n"
  308. "activate application (frontmostApplication as text)\n"
  309. "set links to {}\n"
  310. "copy theResult to the end of links\n"
  311. "return links as string\n"))))
  312. (car (split-string result "[\r\n]+" t))))
  313. (defun org-mac-firefox-get-frontmost-url ()
  314. (interactive)
  315. (message "Applescript: Getting Firefox url...")
  316. (org-mac-paste-applescript-links (org-as-mac-firefox-get-frontmost-url)))
  317. (defun org-mac-firefox-insert-frontmost-url ()
  318. (interactive)
  319. (insert (org-mac-firefox-get-frontmost-url)))
  320. ;; Handle links from Google Firefox.app running the Vimperator extension
  321. ;; Grab the frontmost url from Firefox+Vimperator. Same limitations are
  322. ;; Firefox
  323. (defun org-as-mac-vimperator-get-frontmost-url ()
  324. (let ((result
  325. (do-applescript
  326. (concat
  327. "set oldClipboard to the clipboard\n"
  328. "set frontmostApplication to path to frontmost application\n"
  329. "tell application \"Firefox\"\n"
  330. " activate\n"
  331. " delay 0.15\n"
  332. " tell application \"System Events\"\n"
  333. " keystroke \"y\"\n"
  334. " end tell\n"
  335. " delay 0.15\n"
  336. " set theUrl to the clipboard\n"
  337. " set the clipboard to oldClipboard\n"
  338. " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
  339. "end tell\n"
  340. "activate application (frontmostApplication as text)\n"
  341. "set links to {}\n"
  342. "copy theResult to the end of links\n"
  343. "return links as string\n"))))
  344. (replace-regexp-in-string
  345. "\s+-\s+Vimperator" "" (car (split-string result "[\r\n]+" t)))))
  346. (defun org-mac-vimperator-get-frontmost-url ()
  347. (interactive)
  348. (message "Applescript: Getting Vimperator url...")
  349. (org-mac-paste-applescript-links (org-as-mac-vimperator-get-frontmost-url)))
  350. (defun org-mac-vimperator-insert-frontmost-url ()
  351. (interactive)
  352. (insert (org-mac-vimperator-get-frontmost-url)))
  353. ;; Handle links from Google Chrome.app
  354. ;; Grab the frontmost url from Google Chrome. Same limitations as
  355. ;; Firefox because Chrome doesn't publish an Applescript dictionary
  356. (defun org-as-mac-chrome-get-frontmost-url ()
  357. (let ((result
  358. (do-applescript
  359. (concat
  360. "set frontmostApplication to path to frontmost application\n"
  361. "tell application \"Google Chrome\"\n"
  362. " set theUrl to get URL of active tab of first window\n"
  363. " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
  364. "end tell\n"
  365. "activate application (frontmostApplication as text)\n"
  366. "set links to {}\n"
  367. "copy theResult to the end of links\n"
  368. "return links as string\n"))))
  369. (replace-regexp-in-string
  370. "^\"\\|\"$" "" (car (split-string result "[\r\n]+" t)))))
  371. (defun org-mac-chrome-get-frontmost-url ()
  372. (interactive)
  373. (message "Applescript: Getting Chrome url...")
  374. (org-mac-paste-applescript-links (org-as-mac-chrome-get-frontmost-url)))
  375. (defun org-mac-chrome-insert-frontmost-url ()
  376. (interactive)
  377. (insert (org-mac-chrome-get-frontmost-url)))
  378. ;; Handle links from Safari.app
  379. ;; Grab the frontmost url from Safari.
  380. (defun org-as-mac-safari-get-frontmost-url ()
  381. (do-applescript
  382. (concat
  383. "tell application \"Safari\"\n"
  384. " set theUrl to URL of document 1\n"
  385. " set theName to the name of the document 1\n"
  386. " return theUrl & \"::split::\" & theName & \"\n\"\n"
  387. "end tell\n")))
  388. (defun org-mac-safari-get-frontmost-url ()
  389. (interactive)
  390. (message "Applescript: Getting Safari url...")
  391. (org-mac-paste-applescript-links
  392. (org-as-mac-safari-get-frontmost-url)))
  393. (defun org-mac-safari-insert-frontmost-url ()
  394. (interactive)
  395. (insert (org-mac-safari-get-frontmost-url)))
  396. ;; Handle links from together.app
  397. (org-add-link-type "x-together-item" 'org-mac-together-item-open)
  398. (defun org-mac-together-item-open (uid)
  399. "Open UID, which is a reference to an item in Together."
  400. (shell-command (concat "open -a Together \"x-together-item:" uid "\"")))
  401. (defun as-get-selected-together-items ()
  402. (do-applescript
  403. (concat
  404. "tell application \"Together\"\n"
  405. " set theLinkList to {}\n"
  406. " set theSelection to selected items\n"
  407. " repeat with theItem in theSelection\n"
  408. " set theLink to (get item link of theItem) & \"::split::\" & (get name of theItem) & \"\n\"\n"
  409. " copy theLink to end of theLinkList\n"
  410. " end repeat\n"
  411. " return theLinkList as string\n"
  412. "end tell")))
  413. (defun org-mac-together-get-selected ()
  414. (interactive)
  415. (message "Applescript: Getting Togther items...")
  416. (org-mac-paste-applescript-links (as-get-selected-together-items)))
  417. (defun org-mac-together-insert-selected ()
  418. (interactive)
  419. (insert (org-mac-together-get-selected)))
  420. ;; Handle links from Finder.app
  421. (defun as-get-selected-finder-items ()
  422. (do-applescript
  423. (concat
  424. "tell application \"Finder\"\n"
  425. " set theSelection to the selection\n"
  426. " set links to {}\n"
  427. " repeat with theItem in theSelection\n"
  428. " set theLink to \"file://\" & (POSIX path of (theItem as string)) & \"::split::\" & (get the name of theItem) & \"\n\"\n"
  429. " copy theLink to the end of links\n"
  430. " end repeat\n"
  431. " return links as string\n"
  432. "end tell\n")))
  433. (defun org-mac-finder-item-get-selected ()
  434. (interactive)
  435. (message "Applescript: Getting Finder items...")
  436. (org-mac-paste-applescript-links (as-get-selected-finder-items)))
  437. (defun org-mac-finder-insert-selected ()
  438. (interactive)
  439. (insert (org-mac-finder-item-get-selected)))
  440. ;; Handle links from AddressBook.app
  441. (org-add-link-type "addressbook" 'org-mac-addressbook-item-open)
  442. (defun org-mac-addressbook-item-open (uid)
  443. "Open UID, which is a reference to an item in the addressbook."
  444. (shell-command (concat "open \"addressbook:" uid "\"")))
  445. (defun as-get-selected-addressbook-items ()
  446. (do-applescript
  447. (concat
  448. "tell application \"Address Book\"\n"
  449. " set theSelection to the selection\n"
  450. " set links to {}\n"
  451. " repeat with theItem in theSelection\n"
  452. " set theLink to \"addressbook://\" & (the id of theItem) & \"::split::\" & (the name of theItem) & \"\n\"\n"
  453. " copy theLink to the end of links\n"
  454. " end repeat\n"
  455. " return links as string\n"
  456. "end tell\n")))
  457. (defun org-mac-addressbook-item-get-selected ()
  458. (interactive)
  459. (message "Applescript: Getting Address Book items...")
  460. (org-mac-paste-applescript-links (as-get-selected-addressbook-items)))
  461. (defun org-mac-addressbook-insert-selected ()
  462. (interactive)
  463. (insert (org-mac-addressbook-item-get-selected)))
  464. ;; Handle links from Skim.app
  465. ;;
  466. ;; Original code & idea by Christopher Suckling (org-mac-protocol)
  467. (org-add-link-type "skim" 'org-mac-skim-open)
  468. (defun org-mac-skim-open (uri)
  469. "Visit page of pdf in Skim"
  470. (let* ((page (when (string-match "::\\(.+\\)\\'" uri)
  471. (match-string 1 uri)))
  472. (document (substring uri 0 (match-beginning 0))))
  473. (do-applescript
  474. (concat
  475. "tell application \"Skim\"\n"
  476. "activate\n"
  477. "set theDoc to \"" document "\"\n"
  478. "set thePage to " page "\n"
  479. "open theDoc\n"
  480. "go document 1 to page thePage of document 1\n"
  481. "end tell"))))
  482. (defun as-get-skim-page-link ()
  483. (do-applescript
  484. (concat
  485. "tell application \"Skim\"\n"
  486. "set theDoc to front document\n"
  487. "set theTitle to (name of theDoc)\n"
  488. "set thePath to (path of theDoc)\n"
  489. "set thePage to (get index for current page of theDoc)\n"
  490. "set theSelection to selection of theDoc\n"
  491. "set theContent to contents of (get text for theSelection)\n"
  492. "if theContent is missing value then\n"
  493. " set theContent to theTitle & \", p. \" & thePage\n"
  494. (when org-mac-Skim-highlight-selection-p
  495. (concat
  496. "else\n"
  497. " tell theDoc\n"
  498. " set theNote to make note with properties {type:highlight note, selection:theSelection}\n"
  499. " set text of theNote to (get text for theSelection)\n"
  500. " end tell\n"))
  501. "end if\n"
  502. "set theLink to \"skim://\" & thePath & \"::\" & thePage & "
  503. "\"::split::\" & theContent\n"
  504. "end tell\n"
  505. "return theLink as string\n")))
  506. (defun org-mac-skim-get-page ()
  507. (interactive)
  508. (message "Applescript: Getting Skim page link...")
  509. (org-mac-paste-applescript-links (as-get-skim-page-link)))
  510. (defun org-mac-skim-insert-page ()
  511. (interactive)
  512. (insert (org-mac-skim-get-page)))
  513. ;; Handle links from Adobe Acrobat Pro.app
  514. ;;
  515. ;; Original code & idea by Christopher Suckling (org-mac-protocol)
  516. ;;
  517. ;; The URI format is path_to_pdf_file::page_number
  518. (org-add-link-type "acrobat" 'org-mac-acrobat-open)
  519. (defun org-mac-acrobat-open (uri)
  520. "Visit page of pdf in Acrobat"
  521. (let* ((page (when (string-match "::\\(.+\\)\\'" uri)
  522. (match-string 1 uri)))
  523. (document (substring uri 0 (match-beginning 0))))
  524. (do-applescript
  525. (concat
  526. "tell application \"Adobe Acrobat Pro\"\n"
  527. " activate\n"
  528. " set theDoc to \"" document "\"\n"
  529. " set thePage to " page "\n"
  530. " open theDoc\n"
  531. " tell PDF Window 1\n"
  532. " goto page thePage\n"
  533. " end tell\n"
  534. "end tell"))))
  535. ;; The applescript returns link in the format
  536. ;; "adobe:path_to_pdf_file::page_number::split::document_title, p.page_label"
  537. (defun org-mac-as-get-acrobat-page-link ()
  538. (do-applescript
  539. (concat
  540. "tell application \"Adobe Acrobat Pro\"\n"
  541. " set theDoc to active doc\n"
  542. " set theWindow to (PDF Window 1 of theDoc)\n"
  543. " set thePath to (file alias of theDoc)\n"
  544. " set theTitle to (name of theWindow)\n"
  545. " set thePage to (page number of theWindow)\n"
  546. " set theLabel to (label text of (page thePage of theWindow))\n"
  547. "end tell\n"
  548. "set theResult to \"acrobat:\" & thePath & \"::\" & thePage & \"::split::\" & theTitle & \", p.\" & theLabel\n"
  549. "return theResult as string\n")))
  550. (defun org-mac-acrobat-get-page ()
  551. (interactive)
  552. (message "Applescript: Getting Acrobat page link...")
  553. (org-mac-paste-applescript-links (org-mac-as-get-acrobat-page-link)))
  554. (defun org-mac-acrobat-insert-page ()
  555. (interactive)
  556. (insert (org-mac-acrobat-get-page)))
  557. ;; Handle links from Microsoft Outlook.app
  558. (org-add-link-type "mac-outlook" 'org-mac-outlook-message-open)
  559. (defun org-mac-outlook-message-open (msgid)
  560. "Open a message in Outlook"
  561. (do-applescript
  562. (concat
  563. "tell application \"" org-mac-outlook-path "\"\n"
  564. (format "open message id %s\n" (substring-no-properties msgid))
  565. "activate\n"
  566. "end tell")))
  567. (defun org-as-get-selected-outlook-mail ()
  568. "AppleScript to create links to selected messages in Microsoft Outlook.app."
  569. (do-applescript
  570. (concat
  571. "tell application \"" org-mac-outlook-path "\"\n"
  572. "set msgCount to count current messages\n"
  573. "if (msgCount < 1) then\n"
  574. "return\n"
  575. "end if\n"
  576. "set theLinkList to {}\n"
  577. "set theSelection to (get current messages)\n"
  578. "repeat with theMessage in theSelection\n"
  579. "set theID to id of theMessage as string\n"
  580. "set theURL to \"mac-outlook:\" & theID\n"
  581. "set theSubject to subject of theMessage\n"
  582. "set theLink to theURL & \"::split::\" & theSubject & \"\n\"\n"
  583. "copy theLink to end of theLinkList\n"
  584. "end repeat\n"
  585. "return theLinkList as string\n"
  586. "end tell")))
  587. (defun org-sh-get-flagged-outlook-mail ()
  588. "Shell commands to create links to flagged messages in Microsoft Outlook.app."
  589. (mapconcat
  590. (lambda (x) ""
  591. (concat
  592. "mac-outlook:"
  593. (mapconcat
  594. (lambda (y) "" y)
  595. (split-string
  596. (shell-command-to-string
  597. (format "mdls -raw -name com_microsoft_outlook_recordID -name kMDItemDisplayName \"%s\"" x))
  598. "\000")
  599. "::split::")
  600. "\n"))
  601. (with-temp-buffer
  602. (let ((coding-system-for-read (or file-name-coding-system 'utf-8))
  603. (coding-system-for-write 'utf-8))
  604. (shell-command
  605. "mdfind com_microsoft_outlook_flagged==1"
  606. (current-buffer)))
  607. (split-string
  608. (buffer-string) "\n" t))
  609. ""))
  610. (defun org-mac-outlook-message-get-links (&optional select-or-flag)
  611. "Create links to the messages currently selected or flagged in Microsoft Outlook.app.
  612. This will use AppleScript to get the message-id and the subject of the
  613. messages in Microsoft Outlook.app and make a link out of it.
  614. When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
  615. the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
  616. The Org-syntax text will be pushed to the kill ring, and also returned."
  617. (interactive "sLink to (s)elected or (f)lagged messages: ")
  618. (setq select-or-flag (or select-or-flag "s"))
  619. (message "Org Mac Outlook: searching mailboxes...")
  620. (org-mac-paste-applescript-links
  621. (if (string= select-or-flag "s")
  622. (org-as-get-selected-outlook-mail)
  623. (if (string= select-or-flag "f")
  624. (org-sh-get-flagged-outlook-mail)
  625. (error "Please select \"s\" or \"f\"")))))
  626. (defun org-mac-outlook-message-insert-selected ()
  627. "Insert a link to the messages currently selected in Microsoft Outlook.app.
  628. This will use AppleScript to get the message-id and the subject
  629. of the active mail in Microsoft Outlook.app and make a link out
  630. of it."
  631. (interactive)
  632. (insert (org-mac-outlook-message-get-links "s")))
  633. (defun org-mac-outlook-message-insert-flagged (org-buffer org-heading)
  634. "Asks for an org buffer and a heading within it, and replace message links.
  635. If heading exists, delete all mac-outlook:// links within
  636. heading's first level. If heading doesn't exist, create it at
  637. point-max. Insert list of mac-outlook:// links to flagged mail
  638. after heading."
  639. (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
  640. (with-current-buffer org-buffer
  641. (goto-char (point-min))
  642. (let ((isearch-forward t)
  643. (message-re "\\[\\[\\(mac-outlook:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
  644. (if (org-goto-local-search-headings org-heading nil t)
  645. (if (not (eobp))
  646. (progn
  647. (save-excursion
  648. (while (re-search-forward
  649. message-re (save-excursion (outline-next-heading)) t)
  650. (delete-region (match-beginning 0) (match-end 0)))
  651. (insert "\n" (org-mac-outlook-message-get-links "f")))
  652. (flush-lines "^$" (point) (outline-next-heading)))
  653. (insert "\n" (org-mac-outlook-message-get-links "f")))
  654. (goto-char (point-max))
  655. (insert "\n")
  656. (org-insert-heading nil t)
  657. (insert org-heading "\n" (org-mac-outlook-message-get-links "f"))))))
  658. ;; Handle links from Evernote.app
  659. (org-add-link-type "mac-evernote" 'org-mac-evernote-note-open)
  660. (defun org-mac-evernote-note-open (noteid)
  661. "Open a note in Evernote"
  662. (do-applescript
  663. (concat
  664. "tell application \"" org-mac-evernote-path "\"\n"
  665. " set theNotes to get every note of every notebook where its local id is \"" (substring-no-properties noteid) "\"\n"
  666. " repeat with _note in theNotes\n"
  667. " if length of _note is not 0 then\n"
  668. " set _selectedNote to _note\n"
  669. " end if\n"
  670. " end repeat\n"
  671. " open note window with item 1 of _selectedNote\n"
  672. " activate\n"
  673. "end tell")))
  674. (defun org-as-get-selected-evernote-notes ()
  675. "AppleScript to create links to selected notes in Evernote.app."
  676. (do-applescript
  677. (concat
  678. "tell application \"" org-mac-evernote-path "\"\n"
  679. " set noteCount to count selection\n"
  680. " if (noteCount < 1) then\n"
  681. " return\n"
  682. " end if\n"
  683. " set theLinkList to {}\n"
  684. " set theSelection to selection\n"
  685. " repeat with theNote in theSelection\n"
  686. " set theTitle to title of theNote\n"
  687. " set theID to local id of theNote\n"
  688. " set theURL to \"mac-evernote:\" & theID\n"
  689. " set theLink to theURL & \"::split::\" & theTitle & \"\n\"\n"
  690. " copy theLink to end of theLinkList\n"
  691. " end repeat\n"
  692. " return theLinkList as string\n"
  693. "end tell\n")))
  694. (defun org-mac-evernote-note-insert-selected ()
  695. "Insert a link to the notes currently selected in Evernote.app.
  696. This will use AppleScript to get the note id and the title of the
  697. note(s) in Evernote.app and make a link out of it/them."
  698. (interactive)
  699. (message "Org Mac Evernote: searching notes...")
  700. (insert (org-mac-paste-applescript-links
  701. (org-as-get-selected-evernote-notes))))
  702. ;; Handle links from DEVONthink Pro Office.app
  703. (org-add-link-type "x-devonthink-item" 'org-devonthink-item-open)
  704. (defun org-devonthink-item-open (uid)
  705. "Open UID, which is a reference to an item in DEVONthink Pro Office."
  706. (shell-command (concat "open \"x-devonthink-item:" uid "\"")))
  707. (defun org-as-get-selected-devonthink-item ()
  708. "AppleScript to create links to selected items in DEVONthink Pro Office.app."
  709. (do-applescript
  710. (concat
  711. "set theLinkList to {}\n"
  712. "tell application \"DEVONthink Pro\"\n"
  713. "set selectedRecords to selection\n"
  714. "set selectionCount to count of selectedRecords\n"
  715. "if (selectionCount < 1) then\n"
  716. "return\n"
  717. "end if\n"
  718. "repeat with theRecord in selectedRecords\n"
  719. "set theID to uuid of theRecord\n"
  720. "set theURL to \"x-devonthink-item:\" & theID\n"
  721. "set theSubject to name of theRecord\n"
  722. "set theLink to theURL & \"::split::\" & theSubject & \"\n\"\n"
  723. "copy theLink to end of theLinkList\n"
  724. "end repeat\n"
  725. "end tell\n"
  726. "return theLinkList as string"
  727. )))
  728. (defun org-mac-devonthink-get-links ()
  729. "Create links to the item(s) currently selected in DEVONthink Pro Office.
  730. This will use AppleScript to get the `uuid' and the `name' of the
  731. selected items in DEVONthink Pro Office.app and make links out of
  732. it/them. This function will push the Org-syntax text to the kill
  733. ring, and also return it."
  734. (message "Org Mac DEVONthink: looking for selected items...")
  735. (org-mac-paste-applescript-links (org-as-get-selected-devonthink-item)))
  736. (defun org-mac-devonthink-item-insert-selected ()
  737. "Insert a link to the item(s) currently selected in DEVONthink Pro Office.
  738. This will use AppleScript to get the `uuid'(s) and the name(s) of the
  739. selected items in DEVONthink Pro Office and make link(s) out of it/them."
  740. (interactive)
  741. (insert (org-mac-devonthink-get-links)))
  742. ;; Handle links from Mail.app
  743. (org-add-link-type "message" 'org-mac-message-open)
  744. (defun org-mac-message-open (message-id)
  745. "Visit the message with MESSAGE-ID.
  746. This will use the command `open' with the message URL."
  747. (start-process (concat "open message:" message-id) nil
  748. "open" (concat "message://<" (substring message-id 2) ">")))
  749. (defun org-as-get-selected-mail ()
  750. "AppleScript to create links to selected messages in Mail.app."
  751. (do-applescript
  752. (concat
  753. "tell application \"Mail\"\n"
  754. "set theLinkList to {}\n"
  755. "set theSelection to selection\n"
  756. "repeat with theMessage in theSelection\n"
  757. "set theID to message id of theMessage\n"
  758. "set theSubject to subject of theMessage\n"
  759. "set theLink to \"message://\" & theID & \"::split::\" & theSubject\n"
  760. "if (theLinkList is not equal to {}) then\n"
  761. "set theLink to \"\n\" & theLink\n"
  762. "end if\n"
  763. "copy theLink to end of theLinkList\n"
  764. "end repeat\n"
  765. "return theLinkList as string\n"
  766. "end tell")))
  767. (defun org-as-get-flagged-mail ()
  768. "AppleScript to create links to flagged messages in Mail.app."
  769. (unless org-mac-mail-account
  770. (error "You must set org-mac-mail-account"))
  771. (do-applescript
  772. (concat
  773. ;; Get links
  774. "tell application \"Mail\"\n"
  775. "set theMailboxes to every mailbox of account \"" org-mac-mail-account "\"\n"
  776. "set theLinkList to {}\n"
  777. "repeat with aMailbox in theMailboxes\n"
  778. "set theSelection to (every message in aMailbox whose flagged status = true)\n"
  779. "repeat with theMessage in theSelection\n"
  780. "set theID to message id of theMessage\n"
  781. "set theSubject to subject of theMessage\n"
  782. "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
  783. "copy theLink to end of theLinkList\n"
  784. "end repeat\n"
  785. "end repeat\n"
  786. "return theLinkList as string\n"
  787. "end tell")))
  788. (defun org-mac-message-get-links (&optional select-or-flag)
  789. "Create links to the messages currently selected or flagged in Mail.app.
  790. This will use AppleScript to get the message-id and the subject of the
  791. messages in Mail.app and make a link out of it.
  792. When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
  793. the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
  794. The Org-syntax text will be pushed to the kill ring, and also returned."
  795. (interactive "sLink to (s)elected or (f)lagged messages: ")
  796. (setq select-or-flag (or select-or-flag "s"))
  797. (message "AppleScript: searching mailboxes...")
  798. (org-mac-paste-applescript-links
  799. (cond
  800. ((string= select-or-flag "s") (org-as-get-selected-mail))
  801. ((string= select-or-flag "f") (org-as-get-flagged-mail))
  802. (t (error "Please select \"s\" or \"f\"")))))
  803. (defun org-mac-message-insert-selected ()
  804. "Insert a link to the messages currently selected in Mail.app.
  805. This will use AppleScript to get the message-id and the subject of the
  806. active mail in Mail.app and make a link out of it."
  807. (interactive)
  808. (insert (org-mac-message-get-links "s")))
  809. ;; The following line is for backward compatibility
  810. (defalias 'org-mac-message-insert-link 'org-mac-message-insert-selected)
  811. (defun org-mac-message-insert-flagged (org-buffer org-heading)
  812. "Asks for an org buffer and a heading within it, and replace message links.
  813. If heading exists, delete all message:// links within heading's first
  814. level. If heading doesn't exist, create it at point-max. Insert
  815. list of message:// links to flagged mail after heading."
  816. (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
  817. (with-current-buffer org-buffer
  818. (goto-char (point-min))
  819. (let ((isearch-forward t)
  820. (message-re "\\[\\[\\(message:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
  821. (if (org-goto-local-search-headings org-heading nil t)
  822. (if (not (eobp))
  823. (progn
  824. (save-excursion
  825. (while (re-search-forward
  826. message-re (save-excursion (outline-next-heading)) t)
  827. (delete-region (match-beginning 0) (match-end 0)))
  828. (insert "\n" (org-mac-message-get-links "f")))
  829. (flush-lines "^$" (point) (outline-next-heading)))
  830. (insert "\n" (org-mac-message-get-links "f")))
  831. (goto-char (point-max))
  832. (insert "\n")
  833. (org-insert-heading nil t)
  834. (insert org-heading "\n" (org-mac-message-get-links "f"))))))
  835. (provide 'org-mac-link)
  836. ;;; org-mac-link.el ends here