org-mac-link.el 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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. (defgroup org-mac-flagged-mail nil
  158. "Options foring linking to flagged Mail.app messages."
  159. :tag "Org Mail.app"
  160. :group 'org-link)
  161. (defcustom org-mac-mail-account nil
  162. "The Mail.app account in which to search for flagged messages."
  163. :group 'org-mac-flagged-mail
  164. :type 'string)
  165. ;; In mac.c, removed in Emacs 23.
  166. (declare-function do-applescript "org-mac-message" (script))
  167. (unless (fboundp 'do-applescript)
  168. ;; Need to fake this using shell-command-to-string
  169. (defun do-applescript (script)
  170. (let (start cmd return)
  171. (while (string-match "\n" script)
  172. (setq script (replace-match "\r" t t script)))
  173. (while (string-match "'" script start)
  174. (setq start (+ 2 (match-beginning 0))
  175. script (replace-match "\\'" t t script)))
  176. (setq cmd (concat "osascript -e '" script "'"))
  177. (setq return (shell-command-to-string cmd))
  178. (concat "\"" (org-trim return) "\""))))
  179. (defun org-mac-grab-link ()
  180. "Prompt for an application to grab a link from.
  181. When done, go grab the link, and insert it at point."
  182. (interactive)
  183. (let* ((descriptors
  184. `(("F" "inder" org-mac-finder-insert-selected ,org-mac-grab-Finder-app-p)
  185. ("m" "ail" org-mac-message-insert-selected ,org-mac-grab-Mail-app-p)
  186. ("d" "EVONthink Pro Office" org-mac-devonthink-item-insert-selected
  187. ,org-mac-grab-devonthink-app-p)
  188. ("o" "utlook" org-mac-outlook-message-insert-selected ,org-mac-grab-Outlook-app-p)
  189. ("a" "ddressbook" org-mac-addressbook-insert-selected ,org-mac-grab-Addressbook-app-p)
  190. ("s" "afari" org-mac-safari-insert-frontmost-url ,org-mac-grab-Safari-app-p)
  191. ("f" "irefox" org-mac-firefox-insert-frontmost-url ,org-mac-grab-Firefox-app-p)
  192. ("v" "imperator" org-mac-vimperator-insert-frontmost-url ,org-mac-grab-Firefox+Vimperator-p)
  193. ("c" "hrome" org-mac-chrome-insert-frontmost-url ,org-mac-grab-Chrome-app-p)
  194. ("t" "ogether" org-mac-together-insert-selected ,org-mac-grab-Together-app-p)
  195. ("S" "kim" org-mac-skim-insert-page ,org-mac-grab-Skim-app-p)))
  196. (menu-string (make-string 0 ?x))
  197. input)
  198. ;; Create the menu string for the keymap
  199. (mapc '(lambda (descriptor)
  200. (when (elt descriptor 3)
  201. (setf menu-string (concat menu-string
  202. "[" (elt descriptor 0) "]"
  203. (elt descriptor 1) " "))))
  204. descriptors)
  205. (setf (elt menu-string (- (length menu-string) 1)) ?:)
  206. ;; Prompt the user, and grab the link
  207. (message menu-string)
  208. (setq input (read-char-exclusive))
  209. (mapc '(lambda (descriptor)
  210. (let ((key (elt (elt descriptor 0) 0))
  211. (active (elt descriptor 3))
  212. (grab-function (elt descriptor 2)))
  213. (when (and active (eq input key))
  214. (call-interactively grab-function))))
  215. descriptors)))
  216. (defun org-mac-paste-applescript-links (as-link-list)
  217. "Paste in a list of links from an applescript handler.
  218. The links are of the form <link>::split::<name>."
  219. (let* ((noquote-as-link-list
  220. (if (string-prefix-p "\"" as-link-list)
  221. (substring as-link-list 1 -1)
  222. as-link-list))
  223. (link-list
  224. (mapcar (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x)
  225. (setq x (match-string 1 x)))
  226. x)
  227. (split-string noquote-as-link-list "[\r\n]+")))
  228. split-link URL description orglink orglink-insert rtn orglink-list)
  229. (while link-list
  230. (setq split-link (split-string (pop link-list) "::split::"))
  231. (setq URL (car split-link))
  232. (setq description (cadr split-link))
  233. (when (not (string= URL ""))
  234. (setq orglink (org-make-link-string URL description))
  235. (push orglink orglink-list)))
  236. (setq rtn (mapconcat 'identity orglink-list "\n"))
  237. (kill-new rtn)
  238. rtn))
  239. ;; Handle links from Firefox.app
  240. ;;
  241. ;; This code allows you to grab the current active url from the main
  242. ;; Firefox.app window, and insert it as a link into an org-mode
  243. ;; document. Unfortunately, firefox does not expose an applescript
  244. ;; dictionary, so this is necessarily introduces some limitations.
  245. ;;
  246. ;; The applescript to grab the url from Firefox.app uses the System
  247. ;; Events application to give focus to the firefox application, select
  248. ;; the contents of the url bar, and copy it. It then uses the title of
  249. ;; the window as the text of the link. There is no way to grab links
  250. ;; from other open tabs, and further, if there is more than one window
  251. ;; open, it is not clear which one will be used (though emperically it
  252. ;; seems that it is always the last active window).
  253. (defun org-as-mac-firefox-get-frontmost-url ()
  254. (let ((result
  255. (do-applescript
  256. (concat
  257. "set oldClipboard to the clipboard\n"
  258. "set frontmostApplication to path to frontmost application\n"
  259. "tell application \"Firefox\"\n"
  260. " activate\n"
  261. " delay 0.15\n"
  262. " tell application \"System Events\"\n"
  263. " keystroke \"l\" using {command down}\n"
  264. " keystroke \"a\" using {command down}\n"
  265. " keystroke \"c\" using {command down}\n"
  266. " end tell\n"
  267. " delay 0.15\n"
  268. " set theUrl to the clipboard\n"
  269. " set the clipboard to oldClipboard\n"
  270. " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
  271. "end tell\n"
  272. "activate application (frontmostApplication as text)\n"
  273. "set links to {}\n"
  274. "copy theResult to the end of links\n"
  275. "return links as string\n"))))
  276. (car (split-string result "[\r\n]+" t))))
  277. (defun org-mac-firefox-get-frontmost-url ()
  278. (interactive)
  279. (message "Applescript: Getting Firefox url...")
  280. (org-mac-paste-applescript-links (org-as-mac-firefox-get-frontmost-url)))
  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. (org-mac-paste-applescript-links (org-as-mac-vimperator-get-frontmost-url)))
  314. (defun org-mac-vimperator-insert-frontmost-url ()
  315. (interactive)
  316. (insert (org-mac-vimperator-get-frontmost-url)))
  317. ;; Handle links from Google Chrome.app
  318. ;; Grab the frontmost url from Google Chrome. Same limitations as
  319. ;; Firefox because Chrome doesn't publish an Applescript dictionary
  320. (defun org-as-mac-chrome-get-frontmost-url ()
  321. (let ((result
  322. (do-applescript
  323. (concat
  324. "set frontmostApplication to path to frontmost application\n"
  325. "tell application \"Google Chrome\"\n"
  326. " set theUrl to get URL of active tab of first window\n"
  327. " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
  328. "end tell\n"
  329. "activate application (frontmostApplication as text)\n"
  330. "set links to {}\n"
  331. "copy theResult to the end of links\n"
  332. "return links as string\n"))))
  333. (replace-regexp-in-string
  334. "^\"\\|\"$" "" (car (split-string result "[\r\n]+" t)))))
  335. (defun org-mac-chrome-get-frontmost-url ()
  336. (interactive)
  337. (message "Applescript: Getting Chrome url...")
  338. (org-mac-paste-applescript-links (org-as-mac-chrome-get-frontmost-url)))
  339. (defun org-mac-chrome-insert-frontmost-url ()
  340. (interactive)
  341. (insert (org-mac-chrome-get-frontmost-url)))
  342. ;; Handle links from Safari.app
  343. ;; Grab the frontmost url from Safari.
  344. (defun org-as-mac-safari-get-frontmost-url ()
  345. (do-applescript
  346. (concat
  347. "tell application \"Safari\"\n"
  348. " set theUrl to URL of document 1\n"
  349. " set theName to the name of the document 1\n"
  350. " return theUrl & \"::split::\" & theName & \"\n\"\n"
  351. "end tell\n")))
  352. (defun org-mac-safari-get-frontmost-url ()
  353. (interactive)
  354. (message "Applescript: Getting Safari url...")
  355. (org-mac-paste-applescript-links
  356. (org-as-mac-safari-get-frontmost-url)))
  357. (defun org-mac-safari-insert-frontmost-url ()
  358. (interactive)
  359. (insert (org-mac-safari-get-frontmost-url)))
  360. ;; Handle links from together.app
  361. (org-add-link-type "x-together-item" 'org-mac-together-item-open)
  362. (defun org-mac-together-item-open (uid)
  363. "Open UID, which is a reference to an item in Together."
  364. (shell-command (concat "open -a Together \"x-together-item:" uid "\"")))
  365. (defun as-get-selected-together-items ()
  366. (do-applescript
  367. (concat
  368. "tell application \"Together\"\n"
  369. " set theLinkList to {}\n"
  370. " set theSelection to selected items\n"
  371. " repeat with theItem in theSelection\n"
  372. " set theLink to (get item link of theItem) & \"::split::\" & (get name of theItem) & \"\n\"\n"
  373. " copy theLink to end of theLinkList\n"
  374. " end repeat\n"
  375. " return theLinkList as string\n"
  376. "end tell")))
  377. (defun org-mac-together-get-selected ()
  378. (interactive)
  379. (message "Applescript: Getting Togther items...")
  380. (org-mac-paste-applescript-links (as-get-selected-together-items)))
  381. (defun org-mac-together-insert-selected ()
  382. (interactive)
  383. (insert (org-mac-together-get-selected)))
  384. ;; Handle links from Finder.app
  385. (defun as-get-selected-finder-items ()
  386. (do-applescript
  387. (concat
  388. "tell application \"Finder\"\n"
  389. " set theSelection to the selection\n"
  390. " set links to {}\n"
  391. " repeat with theItem in theSelection\n"
  392. " set theLink to \"file://\" & (POSIX path of (theItem as string)) & \"::split::\" & (get the name of theItem) & \"\n\"\n"
  393. " copy theLink to the end of links\n"
  394. " end repeat\n"
  395. " return links as string\n"
  396. "end tell\n")))
  397. (defun org-mac-finder-item-get-selected ()
  398. (interactive)
  399. (message "Applescript: Getting Finder items...")
  400. (org-mac-paste-applescript-links (as-get-selected-finder-items)))
  401. (defun org-mac-finder-insert-selected ()
  402. (interactive)
  403. (insert (org-mac-finder-item-get-selected)))
  404. ;; Handle links from AddressBook.app
  405. (org-add-link-type "addressbook" 'org-mac-addressbook-item-open)
  406. (defun org-mac-addressbook-item-open (uid)
  407. "Open UID, which is a reference to an item in the addressbook."
  408. (shell-command (concat "open \"addressbook:" uid "\"")))
  409. (defun as-get-selected-addressbook-items ()
  410. (do-applescript
  411. (concat
  412. "tell application \"Address Book\"\n"
  413. " set theSelection to the selection\n"
  414. " set links to {}\n"
  415. " repeat with theItem in theSelection\n"
  416. " set theLink to \"addressbook://\" & (the id of theItem) & \"::split::\" & (the name of theItem) & \"\n\"\n"
  417. " copy theLink to the end of links\n"
  418. " end repeat\n"
  419. " return links as string\n"
  420. "end tell\n")))
  421. (defun org-mac-addressbook-item-get-selected ()
  422. (interactive)
  423. (message "Applescript: Getting Address Book items...")
  424. (org-mac-paste-applescript-links (as-get-selected-addressbook-items)))
  425. (defun org-mac-addressbook-insert-selected ()
  426. (interactive)
  427. (insert (org-mac-addressbook-item-get-selected)))
  428. ;; Handle links from Skim.app
  429. ;;
  430. ;; Original code & idea by Christopher Suckling (org-mac-protocol)
  431. (org-add-link-type "skim" 'org-mac-skim-open)
  432. (defun org-mac-skim-open (uri)
  433. "Visit page of pdf in Skim"
  434. (let* ((page (when (string-match "::\\(.+\\)\\'" uri)
  435. (match-string 1 uri)))
  436. (document (substring uri 0 (match-beginning 0))))
  437. (do-applescript
  438. (concat
  439. "tell application \"Skim\"\n"
  440. "activate\n"
  441. "set theDoc to \"" document "\"\n"
  442. "set thePage to " page "\n"
  443. "open theDoc\n"
  444. "go document 1 to page thePage of document 1\n"
  445. "end tell"))))
  446. (defun as-get-skim-page-link ()
  447. (do-applescript
  448. (concat
  449. "tell application \"Skim\"\n"
  450. "set theDoc to front document\n"
  451. "set theTitle to (name of theDoc)\n"
  452. "set thePath to (path of theDoc)\n"
  453. "set thePage to (get index for current page of theDoc)\n"
  454. "set theSelection to selection of theDoc\n"
  455. "set theContent to contents of (get text for theSelection)\n"
  456. "if theContent is missing value then\n"
  457. " set theContent to theTitle & \", p. \" & thePage\n"
  458. (when org-mac-Skim-highlight-selection-p
  459. (concat
  460. "else\n"
  461. " tell theDoc\n"
  462. " set theNote to make note with properties {type:highlight note, selection:theSelection}\n"
  463. " set text of theNote to (get text for theSelection)\n"
  464. " end tell\n"))
  465. "end if\n"
  466. "set theLink to \"skim://\" & thePath & \"::\" & thePage & "
  467. "\"::split::\" & theContent\n"
  468. "end tell\n"
  469. "return theLink as string\n")))
  470. (defun org-mac-skim-get-page ()
  471. (interactive)
  472. (message "Applescript: Getting Skim page link...")
  473. (org-mac-paste-applescript-links (as-get-skim-page-link)))
  474. (defun org-mac-skim-insert-page ()
  475. (interactive)
  476. (insert (org-mac-skim-get-page)))
  477. ;; Handle links from Microsoft Outlook.app
  478. (org-add-link-type "mac-outlook" 'org-mac-outlook-message-open)
  479. (defun org-mac-outlook-message-open (msgid)
  480. "Open a message in Outlook"
  481. (do-applescript
  482. (concat
  483. "tell application \"/Applications/Microsoft Office 2011/Microsoft Outlook.app\"\n"
  484. (format "open message id %s\n" (substring-no-properties msgid))
  485. "activate\n"
  486. "end tell")))
  487. (defun org-as-get-selected-outlook-mail ()
  488. "AppleScript to create links to selected messages in Microsoft Outlook.app."
  489. (do-applescript
  490. (concat
  491. "tell application \"/Applications/Microsoft Office 2011/Microsoft Outlook.app\"\n"
  492. "set msgCount to count current messages\n"
  493. "if (msgCount < 1) then\n"
  494. "return\n"
  495. "end if\n"
  496. "set theLinkList to {}\n"
  497. "set theSelection to (get current messages)\n"
  498. "repeat with theMessage in theSelection\n"
  499. "set theID to id of theMessage as string\n"
  500. "set theURL to \"mac-outlook:\" & theID\n"
  501. "set theSubject to subject of theMessage\n"
  502. "set theLink to theURL & \"::split::\" & theSubject & \"\n\"\n"
  503. "copy theLink to end of theLinkList\n"
  504. "end repeat\n"
  505. "return theLinkList as string\n"
  506. "end tell")))
  507. (defun org-sh-get-flagged-outlook-mail ()
  508. "Shell commands to create links to flagged messages in Microsoft Outlook.app."
  509. (mapconcat
  510. (lambda (x) ""
  511. (concat
  512. "mac-outlook:"
  513. (mapconcat
  514. (lambda (y) "" y)
  515. (split-string
  516. (shell-command-to-string
  517. (format "mdls -raw -name com_microsoft_outlook_recordID -name kMDItemDisplayName \"%s\"" x))
  518. "\000")
  519. "::split::")
  520. "\n"))
  521. (with-temp-buffer
  522. (let ((coding-system-for-read (or file-name-coding-system 'utf-8))
  523. (coding-system-for-write 'utf-8))
  524. (shell-command
  525. "mdfind com_microsoft_outlook_flagged==1"
  526. (current-buffer)))
  527. (split-string
  528. (buffer-string) "\n" t))
  529. ""))
  530. (defun org-mac-outlook-message-get-links (&optional select-or-flag)
  531. "Create links to the messages currently selected or flagged in Microsoft Outlook.app.
  532. This will use AppleScript to get the message-id and the subject of the
  533. messages in Microsoft Outlook.app and make a link out of it.
  534. When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
  535. the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
  536. The Org-syntax text will be pushed to the kill ring, and also returned."
  537. (interactive "sLink to (s)elected or (f)lagged messages: ")
  538. (setq select-or-flag (or select-or-flag "s"))
  539. (message "Org Mac Outlook: searching mailboxes...")
  540. (org-mac-paste-applescript-links
  541. (if (string= select-or-flag "s")
  542. (org-as-get-selected-outlook-mail)
  543. (if (string= select-or-flag "f")
  544. (org-sh-get-flagged-outlook-mail)
  545. (error "Please select \"s\" or \"f\"")))))
  546. (defun org-mac-outlook-message-insert-selected ()
  547. "Insert a link to the messages currently selected in Microsoft Outlook.app.
  548. This will use AppleScript to get the message-id and the subject
  549. of the active mail in Microsoft Outlook.app and make a link out
  550. of it."
  551. (interactive)
  552. (insert (org-mac-outlook-message-get-links "s")))
  553. (defun org-mac-outlook-message-insert-flagged (org-buffer org-heading)
  554. "Asks for an org buffer and a heading within it, and replace message links.
  555. If heading exists, delete all mac-outlook:// links within
  556. heading's first level. If heading doesn't exist, create it at
  557. point-max. Insert list of mac-outlook:// links to flagged mail
  558. after heading."
  559. (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
  560. (with-current-buffer org-buffer
  561. (goto-char (point-min))
  562. (let ((isearch-forward t)
  563. (message-re "\\[\\[\\(mac-outlook:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
  564. (if (org-goto-local-search-headings org-heading nil t)
  565. (if (not (eobp))
  566. (progn
  567. (save-excursion
  568. (while (re-search-forward
  569. message-re (save-excursion (outline-next-heading)) t)
  570. (delete-region (match-beginning 0) (match-end 0)))
  571. (insert "\n" (org-mac-outlook-message-get-links "f")))
  572. (flush-lines "^$" (point) (outline-next-heading)))
  573. (insert "\n" (org-mac-outlook-message-get-links "f")))
  574. (goto-char (point-max))
  575. (insert "\n")
  576. (org-insert-heading nil t)
  577. (insert org-heading "\n" (org-mac-outlook-message-get-links "f"))))))
  578. ;; Handle links from DEVONthink Pro Office.app
  579. (org-add-link-type "x-devonthink-item" 'org-devonthink-item-open)
  580. (defun org-devonthink-item-open (uid)
  581. "Open UID, which is a reference to an item in DEVONthink Pro Office."
  582. (shell-command (concat "open \"x-devonthink-item:" uid "\"")))
  583. (defun org-as-get-selected-devonthink-item ()
  584. "AppleScript to create links to selected items in DEVONthink Pro Office.app."
  585. (do-applescript
  586. (concat
  587. "set theLinkList to {}\n"
  588. "tell application \"DEVONthink Pro\"\n"
  589. "set selectedRecords to selection\n"
  590. "set selectionCount to count of selectedRecords\n"
  591. "if (selectionCount < 1) then\n"
  592. "return\n"
  593. "end if\n"
  594. "repeat with theRecord in selectedRecords\n"
  595. "set theID to uuid of theRecord\n"
  596. "set theURL to \"x-devonthink-item:\" & theID\n"
  597. "set theSubject to name of theRecord\n"
  598. "set theLink to theURL & \"::split::\" & theSubject & \"\n\"\n"
  599. "copy theLink to end of theLinkList\n"
  600. "end repeat\n"
  601. "end tell\n"
  602. "return theLinkList as string"
  603. )))
  604. (defun org-mac-devonthink-get-links ()
  605. "Create links to the item(s) currently selected in DEVONthink Pro Office.
  606. This will use AppleScript to get the `uuid' and the `name' of the
  607. selected items in DEVONthink Pro Office.app and make links out of
  608. it/them. This function will push the Org-syntax text to the kill
  609. ring, and also return it."
  610. (message "Org Mac DEVONthink: looking for selected items...")
  611. (org-mac-paste-applescript-links (org-as-get-selected-devonthink-item)))
  612. (defun org-mac-devonthink-item-insert-selected ()
  613. "Insert a link to the item(s) currently selected in DEVONthink Pro Office.
  614. This will use AppleScript to get the `uuid'(s) and the name(s) of the
  615. selected items in DEVONthink Pro Office and make link(s) out of it/them."
  616. (interactive)
  617. (insert (org-mac-devonthink-get-links)))
  618. ;; Handle links from Mail.app
  619. (org-add-link-type "message" 'org-mac-message-open)
  620. (defun org-mac-message-open (message-id)
  621. "Visit the message with MESSAGE-ID.
  622. This will use the command `open' with the message URL."
  623. (start-process (concat "open message:" message-id) nil
  624. "open" (concat "message://<" (substring message-id 2) ">")))
  625. (defun org-as-get-selected-mail ()
  626. "AppleScript to create links to selected messages in Mail.app."
  627. (do-applescript
  628. (concat
  629. "tell application \"Mail\"\n"
  630. "set theLinkList to {}\n"
  631. "set theSelection to selection\n"
  632. "repeat with theMessage in theSelection\n"
  633. "set theID to message id of theMessage\n"
  634. "set theSubject to subject of theMessage\n"
  635. "set theLink to \"message://\" & theID & \"::split::\" & theSubject\n"
  636. "if (theLinkList is not equal to {}) then\n"
  637. "set theLink to \"\n\" & theLink\n"
  638. "end if\n"
  639. "copy theLink to end of theLinkList\n"
  640. "end repeat\n"
  641. "return theLinkList as string\n"
  642. "end tell")))
  643. (defun org-as-get-flagged-mail ()
  644. "AppleScript to create links to flagged messages in Mail.app."
  645. (unless org-mac-mail-account
  646. (error "You must set org-mac-mail-account"))
  647. (do-applescript
  648. (concat
  649. ;; Get links
  650. "tell application \"Mail\"\n"
  651. "set theMailboxes to every mailbox of account \"" org-mac-mail-account "\"\n"
  652. "set theLinkList to {}\n"
  653. "repeat with aMailbox in theMailboxes\n"
  654. "set theSelection to (every message in aMailbox whose flagged status = true)\n"
  655. "repeat with theMessage in theSelection\n"
  656. "set theID to message id of theMessage\n"
  657. "set theSubject to subject of theMessage\n"
  658. "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
  659. "copy theLink to end of theLinkList\n"
  660. "end repeat\n"
  661. "end repeat\n"
  662. "return theLinkList as string\n"
  663. "end tell")))
  664. (defun org-mac-message-get-links (&optional select-or-flag)
  665. "Create links to the messages currently selected or flagged in Mail.app.
  666. This will use AppleScript to get the message-id and the subject of the
  667. messages in Mail.app and make a link out of it.
  668. When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
  669. the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
  670. The Org-syntax text will be pushed to the kill ring, and also returned."
  671. (interactive "sLink to (s)elected or (f)lagged messages: ")
  672. (setq select-or-flag (or select-or-flag "s"))
  673. (message "AppleScript: searching mailboxes...")
  674. (org-mac-paste-applescript-links
  675. (cond
  676. ((string= select-or-flag "s") (org-as-get-selected-mail))
  677. ((string= select-or-flag "f") (org-as-get-flagged-mail))
  678. (t (error "Please select \"s\" or \"f\"")))))
  679. (defun org-mac-message-insert-selected ()
  680. "Insert a link to the messages currently selected in Mail.app.
  681. This will use AppleScript to get the message-id and the subject of the
  682. active mail in Mail.app and make a link out of it."
  683. (interactive)
  684. (insert (org-mac-message-get-links "s")))
  685. ;; The following line is for backward compatibility
  686. (defalias 'org-mac-message-insert-link 'org-mac-message-insert-selected)
  687. (defun org-mac-message-insert-flagged (org-buffer org-heading)
  688. "Asks for an org buffer and a heading within it, and replace message links.
  689. If heading exists, delete all message:// links within heading's first
  690. level. If heading doesn't exist, create it at point-max. Insert
  691. list of message:// links to flagged mail after heading."
  692. (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
  693. (with-current-buffer org-buffer
  694. (goto-char (point-min))
  695. (let ((isearch-forward t)
  696. (message-re "\\[\\[\\(message:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
  697. (if (org-goto-local-search-headings org-heading nil t)
  698. (if (not (eobp))
  699. (progn
  700. (save-excursion
  701. (while (re-search-forward
  702. message-re (save-excursion (outline-next-heading)) t)
  703. (delete-region (match-beginning 0) (match-end 0)))
  704. (insert "\n" (org-mac-message-get-links "f")))
  705. (flush-lines "^$" (point) (outline-next-heading)))
  706. (insert "\n" (org-mac-message-get-links "f")))
  707. (goto-char (point-max))
  708. (insert "\n")
  709. (org-insert-heading nil t)
  710. (insert org-heading "\n" (org-mac-message-get-links "f"))))))
  711. (provide 'org-mac-link)
  712. ;;; org-mac-link.el ends here