org-mac-link.el 32 KB

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