org-mac-link.el 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. ;;; org-mac-link.el --- Insert org-mode links to items selected in various Mac apps
  2. ;;
  3. ;; Copyright (c) 2010-2018 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. ;;;###autoload
  214. (defun org-mac-grab-link ()
  215. "Prompt for an application to grab a link from.
  216. When done, go grab the link, and insert it at point."
  217. (interactive)
  218. (let* ((descriptors
  219. `(("F" "inder" org-mac-finder-insert-selected ,org-mac-grab-Finder-app-p)
  220. ("m" "ail" org-mac-message-insert-selected ,org-mac-grab-Mail-app-p)
  221. ("d" "EVONthink Pro Office" org-mac-devonthink-item-insert-selected
  222. ,org-mac-grab-devonthink-app-p)
  223. ("o" "utlook" org-mac-outlook-message-insert-selected ,org-mac-grab-Outlook-app-p)
  224. ("a" "ddressbook" org-mac-addressbook-insert-selected ,org-mac-grab-Addressbook-app-p)
  225. ("s" "afari" org-mac-safari-insert-frontmost-url ,org-mac-grab-Safari-app-p)
  226. ("f" "irefox" org-mac-firefox-insert-frontmost-url ,org-mac-grab-Firefox-app-p)
  227. ("v" "imperator" org-mac-vimperator-insert-frontmost-url ,org-mac-grab-Firefox+Vimperator-p)
  228. ("c" "hrome" org-mac-chrome-insert-frontmost-url ,org-mac-grab-Chrome-app-p)
  229. ("e" "evernote" org-mac-evernote-note-insert-selected ,org-mac-grab-Evernote-app-p)
  230. ("t" "ogether" org-mac-together-insert-selected ,org-mac-grab-Together-app-p)
  231. ("S" "kim" org-mac-skim-insert-page ,org-mac-grab-Skim-app-p)
  232. ("A" "crobat" org-mac-acrobat-insert-page ,org-mac-grab-Acrobat-app-p)))
  233. (menu-string (make-string 0 ?x))
  234. input)
  235. ;; Create the menu string for the keymap
  236. (mapc (lambda (descriptor)
  237. (when (elt descriptor 3)
  238. (setf menu-string (concat menu-string
  239. "[" (elt descriptor 0) "]"
  240. (elt descriptor 1) " "))))
  241. descriptors)
  242. (setf (elt menu-string (- (length menu-string) 1)) ?:)
  243. ;; Prompt the user, and grab the link
  244. (message menu-string)
  245. (setq input (read-char-exclusive))
  246. (mapc (lambda (descriptor)
  247. (let ((key (elt (elt descriptor 0) 0))
  248. (active (elt descriptor 3))
  249. (grab-function (elt descriptor 2)))
  250. (when (and active (eq input key))
  251. (call-interactively grab-function))))
  252. descriptors)))
  253. (defun org-mac-paste-applescript-links (as-link-list)
  254. "Paste in a list of links from an applescript handler.
  255. The links are of the form <link>::split::<name>."
  256. (let* ((noquote-as-link-list
  257. (if (string-prefix-p "\"" as-link-list)
  258. (substring as-link-list 1 -1)
  259. as-link-list))
  260. (link-list
  261. (mapcar (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x)
  262. (setq x (match-string 1 x)))
  263. x)
  264. (split-string noquote-as-link-list "[\r\n]+")))
  265. split-link URL description orglink orglink-insert rtn orglink-list)
  266. (while link-list
  267. (setq split-link (split-string (pop link-list) "::split::"))
  268. (setq URL (car split-link))
  269. (setq description (cadr split-link))
  270. (when (not (string= URL ""))
  271. (setq orglink (org-make-link-string URL description))
  272. (push orglink orglink-list)))
  273. (setq rtn (mapconcat 'identity orglink-list "\n"))
  274. (kill-new rtn)
  275. rtn))
  276. ;; Handle links from Firefox.app
  277. ;;
  278. ;; This code allows you to grab the current active url from the main
  279. ;; Firefox.app window, and insert it as a link into an org-mode
  280. ;; document. Unfortunately, firefox does not expose an applescript
  281. ;; dictionary, so this is necessarily introduces some limitations.
  282. ;;
  283. ;; The applescript to grab the url from Firefox.app uses the System
  284. ;; Events application to give focus to the firefox application, select
  285. ;; the contents of the url bar, and copy it. It then uses the title of
  286. ;; the window as the text of the link. There is no way to grab links
  287. ;; from other open tabs, and further, if there is more than one window
  288. ;; open, it is not clear which one will be used (though emperically it
  289. ;; seems that it is always the last active window).
  290. (defun org-as-mac-firefox-get-frontmost-url ()
  291. (let ((result
  292. (do-applescript
  293. (concat
  294. "set oldClipboard to the clipboard\n"
  295. "set frontmostApplication to path to frontmost application\n"
  296. "tell application \"Firefox\"\n"
  297. " activate\n"
  298. " delay 0.15\n"
  299. " tell application \"System Events\"\n"
  300. " keystroke \"l\" using {command down}\n"
  301. " keystroke \"a\" using {command down}\n"
  302. " keystroke \"c\" using {command down}\n"
  303. " end tell\n"
  304. " delay 0.15\n"
  305. " set theUrl to the clipboard\n"
  306. " set the clipboard to oldClipboard\n"
  307. " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
  308. "end tell\n"
  309. "activate application (frontmostApplication as text)\n"
  310. "set links to {}\n"
  311. "copy theResult to the end of links\n"
  312. "return links as string\n"))))
  313. (car (split-string result "[\r\n]+" t))))
  314. ;;;###autoload
  315. (defun org-mac-firefox-get-frontmost-url ()
  316. (interactive)
  317. (message "Applescript: Getting Firefox url...")
  318. (org-mac-paste-applescript-links (org-as-mac-firefox-get-frontmost-url)))
  319. ;;;###autoload
  320. (defun org-mac-firefox-insert-frontmost-url ()
  321. (interactive)
  322. (insert (org-mac-firefox-get-frontmost-url)))
  323. ;; Handle links from Google Firefox.app running the Vimperator extension
  324. ;; Grab the frontmost url from Firefox+Vimperator. Same limitations are
  325. ;; Firefox
  326. (defun org-as-mac-vimperator-get-frontmost-url ()
  327. (let ((result
  328. (do-applescript
  329. (concat
  330. "set oldClipboard to the clipboard\n"
  331. "set frontmostApplication to path to frontmost application\n"
  332. "tell application \"Firefox\"\n"
  333. " activate\n"
  334. " delay 0.15\n"
  335. " tell application \"System Events\"\n"
  336. " keystroke \"y\"\n"
  337. " end tell\n"
  338. " delay 0.15\n"
  339. " set theUrl to the clipboard\n"
  340. " set the clipboard to oldClipboard\n"
  341. " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
  342. "end tell\n"
  343. "activate application (frontmostApplication as text)\n"
  344. "set links to {}\n"
  345. "copy theResult to the end of links\n"
  346. "return links as string\n"))))
  347. (replace-regexp-in-string
  348. "\s+-\s+Vimperator" "" (car (split-string result "[\r\n]+" t)))))
  349. ;;;###autoload
  350. (defun org-mac-vimperator-get-frontmost-url ()
  351. (interactive)
  352. (message "Applescript: Getting Vimperator url...")
  353. (org-mac-paste-applescript-links (org-as-mac-vimperator-get-frontmost-url)))
  354. ;;;###autoload
  355. (defun org-mac-vimperator-insert-frontmost-url ()
  356. (interactive)
  357. (insert (org-mac-vimperator-get-frontmost-url)))
  358. ;; Handle links from Google Chrome.app
  359. ;; Grab the frontmost url from Google Chrome. Same limitations as
  360. ;; Firefox because Chrome doesn't publish an Applescript dictionary
  361. (defun org-as-mac-chrome-get-frontmost-url ()
  362. (let ((result
  363. (do-applescript
  364. (concat
  365. "set frontmostApplication to path to frontmost application\n"
  366. "tell application \"Google Chrome\"\n"
  367. " set theUrl to get URL of active tab of first window\n"
  368. " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
  369. "end tell\n"
  370. "activate application (frontmostApplication as text)\n"
  371. "set links to {}\n"
  372. "copy theResult to the end of links\n"
  373. "return links as string\n"))))
  374. (replace-regexp-in-string
  375. "^\"\\|\"$" "" (car (split-string result "[\r\n]+" t)))))
  376. ;;;###autoload
  377. (defun org-mac-chrome-get-frontmost-url ()
  378. (interactive)
  379. (message "Applescript: Getting Chrome url...")
  380. (org-mac-paste-applescript-links (org-as-mac-chrome-get-frontmost-url)))
  381. ;;;###autoload
  382. (defun org-mac-chrome-insert-frontmost-url ()
  383. (interactive)
  384. (insert (org-mac-chrome-get-frontmost-url)))
  385. ;; Handle links from Safari.app
  386. ;; Grab the frontmost url from Safari.
  387. (defun org-as-mac-safari-get-frontmost-url ()
  388. (do-applescript
  389. (concat
  390. "tell application \"Safari\"\n"
  391. " set theUrl to URL of document 1\n"
  392. " set theName to the name of the document 1\n"
  393. " return theUrl & \"::split::\" & theName & \"\n\"\n"
  394. "end tell\n")))
  395. ;;;###autoload
  396. (defun org-mac-safari-get-frontmost-url ()
  397. (interactive)
  398. (message "Applescript: Getting Safari url...")
  399. (org-mac-paste-applescript-links
  400. (org-as-mac-safari-get-frontmost-url)))
  401. ;;;###autoload
  402. (defun org-mac-safari-insert-frontmost-url ()
  403. (interactive)
  404. (insert (org-mac-safari-get-frontmost-url)))
  405. ;; Handle links from together.app
  406. (org-link-set-parameters "x-together-item" :follow #'org-mac-together-item-open)
  407. (defun org-mac-together-item-open (uid)
  408. "Open UID, which is a reference to an item in Together."
  409. (shell-command (concat "open -a Together \"x-together-item:" uid "\"")))
  410. (defun as-get-selected-together-items ()
  411. (do-applescript
  412. (concat
  413. "tell application \"Together\"\n"
  414. " set theLinkList to {}\n"
  415. " set theSelection to selected items\n"
  416. " repeat with theItem in theSelection\n"
  417. " set theLink to (get item link of theItem) & \"::split::\" & (get name of theItem) & \"\n\"\n"
  418. " copy theLink to end of theLinkList\n"
  419. " end repeat\n"
  420. " return theLinkList as string\n"
  421. "end tell")))
  422. ;;;###autoload
  423. (defun org-mac-together-get-selected ()
  424. (interactive)
  425. (message "Applescript: Getting Togther items...")
  426. (org-mac-paste-applescript-links (as-get-selected-together-items)))
  427. ;;;###autoload
  428. (defun org-mac-together-insert-selected ()
  429. (interactive)
  430. (insert (org-mac-together-get-selected)))
  431. ;; Handle links from Finder.app
  432. (defun as-get-selected-finder-items ()
  433. (do-applescript
  434. (concat
  435. "tell application \"Finder\"\n"
  436. " set theSelection to the selection\n"
  437. " set links to {}\n"
  438. " repeat with theItem in theSelection\n"
  439. " set theLink to \"file://\" & (POSIX path of (theItem as string)) & \"::split::\" & (get the name of theItem) & \"\n\"\n"
  440. " copy theLink to the end of links\n"
  441. " end repeat\n"
  442. " return links as string\n"
  443. "end tell\n")))
  444. ;;;###autoload
  445. (defun org-mac-finder-item-get-selected ()
  446. (interactive)
  447. (message "Applescript: Getting Finder items...")
  448. (org-mac-paste-applescript-links (as-get-selected-finder-items)))
  449. ;;;###autoload
  450. (defun org-mac-finder-insert-selected ()
  451. (interactive)
  452. (insert (org-mac-finder-item-get-selected)))
  453. ;; Handle links from AddressBook.app
  454. (org-link-set-parameters "addressbook" :follow #'org-mac-addressbook-item-open)
  455. (defun org-mac-addressbook-item-open (uid)
  456. "Open UID, which is a reference to an item in the addressbook."
  457. (shell-command (concat "open \"addressbook:" uid "\"")))
  458. (defun as-get-selected-addressbook-items ()
  459. (do-applescript
  460. (concat
  461. "tell application \"Address Book\"\n"
  462. " set theSelection to the selection\n"
  463. " set links to {}\n"
  464. " repeat with theItem in theSelection\n"
  465. " set theLink to \"addressbook://\" & (the id of theItem) & \"::split::\" & (the name of theItem) & \"\n\"\n"
  466. " copy theLink to the end of links\n"
  467. " end repeat\n"
  468. " return links as string\n"
  469. "end tell\n")))
  470. ;;;###autoload
  471. (defun org-mac-addressbook-item-get-selected ()
  472. (interactive)
  473. (message "Applescript: Getting Address Book items...")
  474. (org-mac-paste-applescript-links (as-get-selected-addressbook-items)))
  475. ;;;###autoload
  476. (defun org-mac-addressbook-insert-selected ()
  477. (interactive)
  478. (insert (org-mac-addressbook-item-get-selected)))
  479. ;; Handle links from Skim.app
  480. ;;
  481. ;; Original code & idea by Christopher Suckling (org-mac-protocol)
  482. (org-link-set-parameters "skim" :follow #'org-mac-skim-open)
  483. (defun org-mac-skim-open (uri)
  484. "Visit page of pdf in Skim"
  485. (let* ((page (when (string-match "::\\(.+\\)\\'" uri)
  486. (match-string 1 uri)))
  487. (document (substring uri 0 (match-beginning 0))))
  488. (do-applescript
  489. (concat
  490. "tell application \"Skim\"\n"
  491. "activate\n"
  492. "set theDoc to \"" document "\"\n"
  493. "set thePage to " page "\n"
  494. "open theDoc\n"
  495. "go document 1 to page thePage of document 1\n"
  496. "end tell"))))
  497. (defun as-get-skim-page-link ()
  498. (do-applescript
  499. (concat
  500. "tell application \"Skim\"\n"
  501. "set theDoc to front document\n"
  502. "set theTitle to (name of theDoc)\n"
  503. "set thePath to (path of theDoc)\n"
  504. "set thePage to (get index for current page of theDoc)\n"
  505. "set theSelection to selection of theDoc\n"
  506. "set theContent to contents of (get text for theSelection)\n"
  507. "if theContent is missing value then\n"
  508. " set theContent to theTitle & \", p. \" & thePage\n"
  509. (when org-mac-Skim-highlight-selection-p
  510. (concat
  511. "else\n"
  512. " tell theDoc\n"
  513. " set theNote to make note with properties {type:highlight note, selection:theSelection}\n"
  514. " set text of theNote to (get text for theSelection)\n"
  515. " end tell\n"))
  516. "end if\n"
  517. "set theLink to \"skim://\" & thePath & \"::\" & thePage & "
  518. "\"::split::\" & theContent\n"
  519. "end tell\n"
  520. "return theLink as string\n")))
  521. ;;;###autoload
  522. (defun org-mac-skim-get-page ()
  523. (interactive)
  524. (message "Applescript: Getting Skim page link...")
  525. (org-mac-paste-applescript-links (as-get-skim-page-link)))
  526. ;;;###autoload
  527. (defun org-mac-skim-insert-page ()
  528. (interactive)
  529. (insert (org-mac-skim-get-page)))
  530. ;; Handle links from Adobe Acrobat Pro.app
  531. ;;
  532. ;; Original code & idea by Christopher Suckling (org-mac-protocol)
  533. ;;
  534. ;; The URI format is path_to_pdf_file::page_number
  535. (org-link-set-parameters "acrobat" :follow #'org-mac-acrobat-open)
  536. (defun org-mac-acrobat-open (uri)
  537. "Visit page of pdf in Acrobat"
  538. (let* ((page (when (string-match "::\\(.+\\)\\'" uri)
  539. (match-string 1 uri)))
  540. (document (substring uri 0 (match-beginning 0))))
  541. (do-applescript
  542. (concat
  543. "tell application \"Adobe Acrobat Pro\"\n"
  544. " activate\n"
  545. " set theDoc to \"" document "\"\n"
  546. " set thePage to " page "\n"
  547. " open theDoc\n"
  548. " tell PDF Window 1\n"
  549. " goto page thePage\n"
  550. " end tell\n"
  551. "end tell"))))
  552. ;; The applescript returns link in the format
  553. ;; "adobe:path_to_pdf_file::page_number::split::document_title, p.page_label"
  554. (defun org-mac-as-get-acrobat-page-link ()
  555. (do-applescript
  556. (concat
  557. "tell application \"Adobe Acrobat Pro\"\n"
  558. " set theDoc to active doc\n"
  559. " set theWindow to (PDF Window 1 of theDoc)\n"
  560. " set thePath to (file alias of theDoc)\n"
  561. " set theTitle to (name of theWindow)\n"
  562. " set thePage to (page number of theWindow)\n"
  563. " set theLabel to (label text of (page thePage of theWindow))\n"
  564. "end tell\n"
  565. "set theResult to \"acrobat:\" & thePath & \"::\" & thePage & \"::split::\" & theTitle & \", p.\" & theLabel\n"
  566. "return theResult as string\n")))
  567. ;;;###autoload
  568. (defun org-mac-acrobat-get-page ()
  569. (interactive)
  570. (message "Applescript: Getting Acrobat page link...")
  571. (org-mac-paste-applescript-links (org-mac-as-get-acrobat-page-link)))
  572. ;;;###autoload
  573. (defun org-mac-acrobat-insert-page ()
  574. (interactive)
  575. (insert (org-mac-acrobat-get-page)))
  576. ;; Handle links from Microsoft Outlook.app
  577. (org-link-set-parameters "mac-outlook" :follow #'org-mac-outlook-message-open)
  578. (defun org-mac-outlook-message-open (msgid)
  579. "Open a message in Outlook"
  580. (do-applescript
  581. (concat
  582. "tell application \"" org-mac-outlook-path "\"\n"
  583. (format "open message id %s\n" (substring-no-properties msgid))
  584. "activate\n"
  585. "end tell")))
  586. (defun org-as-get-selected-outlook-mail ()
  587. "AppleScript to create links to selected messages in Microsoft Outlook.app."
  588. (do-applescript
  589. (concat
  590. "tell application \"" org-mac-outlook-path "\"\n"
  591. "set msgCount to count current messages\n"
  592. "if (msgCount < 1) then\n"
  593. "return\n"
  594. "end if\n"
  595. "set theLinkList to {}\n"
  596. "set theSelection to (get current messages)\n"
  597. "repeat with theMessage in theSelection\n"
  598. "set theID to id of theMessage as string\n"
  599. "set theURL to \"mac-outlook:\" & theID\n"
  600. "set theSubject to subject of theMessage\n"
  601. "set theLink to theURL & \"::split::\" & theSubject & \"\n\"\n"
  602. "copy theLink to end of theLinkList\n"
  603. "end repeat\n"
  604. "return theLinkList as string\n"
  605. "end tell")))
  606. (defun org-sh-get-flagged-outlook-mail ()
  607. "Shell commands to create links to flagged messages in Microsoft Outlook.app."
  608. (mapconcat
  609. (lambda (x) ""
  610. (concat
  611. "mac-outlook:"
  612. (mapconcat
  613. (lambda (y) "" y)
  614. (split-string
  615. (shell-command-to-string
  616. (format "mdls -raw -name com_microsoft_outlook_recordID -name kMDItemDisplayName \"%s\"" x))
  617. "\000")
  618. "::split::")
  619. "\n"))
  620. (with-temp-buffer
  621. (let ((coding-system-for-read (or file-name-coding-system 'utf-8))
  622. (coding-system-for-write 'utf-8))
  623. (shell-command
  624. "mdfind com_microsoft_outlook_flagged==1"
  625. (current-buffer)))
  626. (split-string
  627. (buffer-string) "\n" t))
  628. ""))
  629. ;;;###autoload
  630. (defun org-mac-outlook-message-get-links (&optional select-or-flag)
  631. "Create links to the messages currently selected or flagged in Microsoft Outlook.app.
  632. This will use AppleScript to get the message-id and the subject of the
  633. messages in Microsoft Outlook.app and make a link out of it.
  634. When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
  635. the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
  636. The Org-syntax text will be pushed to the kill ring, and also returned."
  637. (interactive "sLink to (s)elected or (f)lagged messages: ")
  638. (setq select-or-flag (or select-or-flag "s"))
  639. (message "Org Mac Outlook: searching mailboxes...")
  640. (org-mac-paste-applescript-links
  641. (if (string= select-or-flag "s")
  642. (org-as-get-selected-outlook-mail)
  643. (if (string= select-or-flag "f")
  644. (org-sh-get-flagged-outlook-mail)
  645. (error "Please select \"s\" or \"f\"")))))
  646. ;;;###autoload
  647. (defun org-mac-outlook-message-insert-selected ()
  648. "Insert a link to the messages currently selected in Microsoft Outlook.app.
  649. This will use AppleScript to get the message-id and the subject
  650. of the active mail in Microsoft Outlook.app and make a link out
  651. of it."
  652. (interactive)
  653. (insert (org-mac-outlook-message-get-links "s")))
  654. ;;;###autoload
  655. (defun org-mac-outlook-message-insert-flagged (org-buffer org-heading)
  656. "Asks for an org buffer and a heading within it, and replace message links.
  657. If heading exists, delete all mac-outlook:// links within
  658. heading's first level. If heading doesn't exist, create it at
  659. point-max. Insert list of mac-outlook:// links to flagged mail
  660. after heading."
  661. (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
  662. (with-current-buffer org-buffer
  663. (goto-char (point-min))
  664. (let ((isearch-forward t)
  665. (message-re "\\[\\[\\(mac-outlook:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
  666. (if (org-goto-local-search-headings org-heading nil t)
  667. (if (not (eobp))
  668. (progn
  669. (save-excursion
  670. (while (re-search-forward
  671. message-re (save-excursion (outline-next-heading)) t)
  672. (delete-region (match-beginning 0) (match-end 0)))
  673. (insert "\n" (org-mac-outlook-message-get-links "f")))
  674. (flush-lines "^$" (point) (outline-next-heading)))
  675. (insert "\n" (org-mac-outlook-message-get-links "f")))
  676. (goto-char (point-max))
  677. (insert "\n")
  678. (org-insert-heading nil t)
  679. (insert org-heading "\n" (org-mac-outlook-message-get-links "f"))))))
  680. ;; Handle links from Evernote.app
  681. (org-link-set-parameters "mac-evernote" :follow #'org-mac-evernote-note-open)
  682. (defun org-mac-evernote-note-open (noteid)
  683. "Open a note in Evernote"
  684. (do-applescript
  685. (concat
  686. "tell application \"" org-mac-evernote-path "\"\n"
  687. " set theNotes to get every note of every notebook where its local id is \"" (substring-no-properties noteid) "\"\n"
  688. " repeat with _note in theNotes\n"
  689. " if length of _note is not 0 then\n"
  690. " set _selectedNote to _note\n"
  691. " end if\n"
  692. " end repeat\n"
  693. " open note window with item 1 of _selectedNote\n"
  694. " activate\n"
  695. "end tell")))
  696. (defun org-as-get-selected-evernote-notes ()
  697. "AppleScript to create links to selected notes in Evernote.app."
  698. (do-applescript
  699. (concat
  700. "tell application \"" org-mac-evernote-path "\"\n"
  701. " set noteCount to count selection\n"
  702. " if (noteCount < 1) then\n"
  703. " return\n"
  704. " end if\n"
  705. " set theLinkList to {}\n"
  706. " set theSelection to selection\n"
  707. " repeat with theNote in theSelection\n"
  708. " set theTitle to title of theNote\n"
  709. " set theID to local id of theNote\n"
  710. " set theURL to \"mac-evernote:\" & theID\n"
  711. " set theLink to theURL & \"::split::\" & theTitle & \"\n\"\n"
  712. " copy theLink to end of theLinkList\n"
  713. " end repeat\n"
  714. " return theLinkList as string\n"
  715. "end tell\n")))
  716. ;;;###autoload
  717. (defun org-mac-evernote-note-insert-selected ()
  718. "Insert a link to the notes currently selected in Evernote.app.
  719. This will use AppleScript to get the note id and the title of the
  720. note(s) in Evernote.app and make a link out of it/them."
  721. (interactive)
  722. (message "Org Mac Evernote: searching notes...")
  723. (insert (org-mac-paste-applescript-links
  724. (org-as-get-selected-evernote-notes))))
  725. ;; Handle links from DEVONthink Pro Office.app
  726. (org-link-set-parameters "x-devonthink-item" :follow #'org-devonthink-item-open)
  727. (defun org-devonthink-item-open (uid)
  728. "Open UID, which is a reference to an item in DEVONthink Pro Office."
  729. (shell-command (concat "open \"x-devonthink-item:" uid "\"")))
  730. (defun org-as-get-selected-devonthink-item ()
  731. "AppleScript to create links to selected items in DEVONthink Pro Office.app."
  732. (do-applescript
  733. (concat
  734. "set theLinkList to {}\n"
  735. "tell application \"DEVONthink Pro\"\n"
  736. "set selectedRecords to selection\n"
  737. "set selectionCount to count of selectedRecords\n"
  738. "if (selectionCount < 1) then\n"
  739. "return\n"
  740. "end if\n"
  741. "repeat with theRecord in selectedRecords\n"
  742. "set theID to uuid of theRecord\n"
  743. "set theURL to \"x-devonthink-item:\" & theID\n"
  744. "set theSubject to name of theRecord\n"
  745. "set theLink to theURL & \"::split::\" & theSubject & \"\n\"\n"
  746. "copy theLink to end of theLinkList\n"
  747. "end repeat\n"
  748. "end tell\n"
  749. "return theLinkList as string"
  750. )))
  751. (defun org-mac-devonthink-get-links ()
  752. "Create links to the item(s) currently selected in DEVONthink Pro Office.
  753. This will use AppleScript to get the `uuid' and the `name' of the
  754. selected items in DEVONthink Pro Office.app and make links out of
  755. it/them. This function will push the Org-syntax text to the kill
  756. ring, and also return it."
  757. (message "Org Mac DEVONthink: looking for selected items...")
  758. (org-mac-paste-applescript-links (org-as-get-selected-devonthink-item)))
  759. ;;;###autoload
  760. (defun org-mac-devonthink-item-insert-selected ()
  761. "Insert a link to the item(s) currently selected in DEVONthink Pro Office.
  762. This will use AppleScript to get the `uuid'(s) and the name(s) of the
  763. selected items in DEVONthink Pro Office and make link(s) out of it/them."
  764. (interactive)
  765. (insert (org-mac-devonthink-get-links)))
  766. ;; Handle links from Mail.app
  767. (org-link-set-parameters "message" :follow #'org-mac-message-open)
  768. (defun org-mac-message-open (message-id)
  769. "Visit the message with MESSAGE-ID.
  770. This will use the command `open' with the message URL."
  771. (start-process (concat "open message:" message-id) nil
  772. "open" (concat "message://<" (substring message-id 2) ">")))
  773. (defun org-as-get-selected-mail ()
  774. "AppleScript to create links to selected messages in Mail.app."
  775. (do-applescript
  776. (concat
  777. "tell application \"Mail\"\n"
  778. "set theLinkList to {}\n"
  779. "set theSelection to selection\n"
  780. "repeat with theMessage in theSelection\n"
  781. "set theID to message id of theMessage\n"
  782. "set theSubject to subject of theMessage\n"
  783. "set theLink to \"message://\" & theID & \"::split::\" & theSubject\n"
  784. "if (theLinkList is not equal to {}) then\n"
  785. "set theLink to \"\n\" & theLink\n"
  786. "end if\n"
  787. "copy theLink to end of theLinkList\n"
  788. "end repeat\n"
  789. "return theLinkList as string\n"
  790. "end tell")))
  791. (defun org-as-get-flagged-mail ()
  792. "AppleScript to create links to flagged messages in Mail.app."
  793. (unless org-mac-mail-account
  794. (error "You must set org-mac-mail-account"))
  795. (do-applescript
  796. (concat
  797. ;; Get links
  798. "tell application \"Mail\"\n"
  799. "set theMailboxes to every mailbox of account \"" org-mac-mail-account "\"\n"
  800. "set theLinkList to {}\n"
  801. "repeat with aMailbox in theMailboxes\n"
  802. "set theSelection to (every message in aMailbox whose flagged status = true)\n"
  803. "repeat with theMessage in theSelection\n"
  804. "set theID to message id of theMessage\n"
  805. "set theSubject to subject of theMessage\n"
  806. "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
  807. "copy theLink to end of theLinkList\n"
  808. "end repeat\n"
  809. "end repeat\n"
  810. "return theLinkList as string\n"
  811. "end tell")))
  812. ;;;###autoload
  813. (defun org-mac-message-get-links (&optional select-or-flag)
  814. "Create links to the messages currently selected or flagged in Mail.app.
  815. This will use AppleScript to get the message-id and the subject of the
  816. messages in Mail.app and make a link out of it.
  817. When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
  818. the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
  819. The Org-syntax text will be pushed to the kill ring, and also returned."
  820. (interactive "sLink to (s)elected or (f)lagged messages: ")
  821. (setq select-or-flag (or select-or-flag "s"))
  822. (message "AppleScript: searching mailboxes...")
  823. (org-mac-paste-applescript-links
  824. (cond
  825. ((string= select-or-flag "s") (org-as-get-selected-mail))
  826. ((string= select-or-flag "f") (org-as-get-flagged-mail))
  827. (t (error "Please select \"s\" or \"f\"")))))
  828. ;;;###autoload
  829. (defun org-mac-message-insert-selected ()
  830. "Insert a link to the messages currently selected in Mail.app.
  831. This will use AppleScript to get the message-id and the subject of the
  832. active mail in Mail.app and make a link out of it."
  833. (interactive)
  834. (insert (org-mac-message-get-links "s")))
  835. ;; The following line is for backward compatibility
  836. (defalias 'org-mac-message-insert-link 'org-mac-message-insert-selected)
  837. ;;;###autoload
  838. (defun org-mac-message-insert-flagged (org-buffer org-heading)
  839. "Asks for an org buffer and a heading within it, and replace message links.
  840. If heading exists, delete all message:// links within heading's first
  841. level. If heading doesn't exist, create it at point-max. Insert
  842. list of message:// links to flagged mail after heading."
  843. (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
  844. (with-current-buffer org-buffer
  845. (goto-char (point-min))
  846. (let ((isearch-forward t)
  847. (message-re "\\[\\[\\(message:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
  848. (if (org-goto-local-search-headings org-heading nil t)
  849. (if (not (eobp))
  850. (progn
  851. (save-excursion
  852. (while (re-search-forward
  853. message-re (save-excursion (outline-next-heading)) t)
  854. (delete-region (match-beginning 0) (match-end 0)))
  855. (insert "\n" (org-mac-message-get-links "f")))
  856. (flush-lines "^$" (point) (outline-next-heading)))
  857. (insert "\n" (org-mac-message-get-links "f")))
  858. (goto-char (point-max))
  859. (insert "\n")
  860. (org-insert-heading nil t)
  861. (insert org-heading "\n" (org-mac-message-get-links "f"))))))
  862. (provide 'org-mac-link)
  863. ;;; org-mac-link.el ends here