org-mac-link.el 33 KB

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