org-mac-link.el 34 KB

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