org-mac-link.el 32 KB

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