org-mac-link.el 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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. ;; Authors:
  7. ;; Anthony Lander <anthony.lander@gmail.com>
  8. ;; John Wiegley <johnw@gnu.org>
  9. ;; Christopher Suckling <suckling at gmail dot com>
  10. ;; Daniil Frumin <difrumin@gmail.com>
  11. ;; Alan Schmitt <alan.schmitt@polytechnique.org>
  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. ;;
  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 concerning grabbing links from external Mac
  89. applications and inserting them in org documents"
  90. :tag "Org Mac link"
  91. :group 'org-link)
  92. (defcustom org-mac-grab-Finder-app-p t
  93. "Enable 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. "Enable 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. "Enable 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-Addressbook-app-p t
  108. "Enable menu option [a]ddressbook to grab links from AddressBook.app"
  109. :tag "Grab AddressBook.app links"
  110. :group 'org-mac-link
  111. :type 'boolean)
  112. (defcustom org-mac-grab-Safari-app-p t
  113. "Enable menu option [s]afari to grab links from Safari.app"
  114. :tag "Grab Safari.app links"
  115. :group 'org-mac-link
  116. :type 'boolean)
  117. (defcustom org-mac-grab-Firefox-app-p t
  118. "Enable menu option [f]irefox to grab links from Firefox.app"
  119. :tag "Grab Firefox.app links"
  120. :group 'org-mac-link
  121. :type 'boolean)
  122. (defcustom org-mac-grab-Firefox+Vimperator-p nil
  123. "Enable menu option [v]imperator to grab links from Firefox.app running the Vimperator plugin"
  124. :tag "Grab Vimperator/Firefox.app links"
  125. :group 'org-mac-link
  126. :type 'boolean)
  127. (defcustom org-mac-grab-Chrome-app-p t
  128. "Enable menu option [c]hrome to grab links from Google Chrome.app"
  129. :tag "Grab Google Chrome.app links"
  130. :group 'org-mac-link
  131. :type 'boolean)
  132. (defcustom org-mac-grab-Together-app-p nil
  133. "Enable menu option [t]ogether to grab links from Together.app"
  134. :tag "Grab Together.app links"
  135. :group 'org-mac-link
  136. :type 'boolean)
  137. (defcustom org-mac-grab-Skim-app-p
  138. (< 0 (length (shell-command-to-string
  139. "mdfind kMDItemCFBundleIdentifier == 'net.sourceforge.skim-app.skim'")))
  140. "Enable menu option [S]kim to grab page links from Skim.app"
  141. :tag "Grab Skim.app page links"
  142. :group 'org-mac-link
  143. :type 'boolean)
  144. (defcustom org-mac-Skim-highlight-selection-p nil
  145. "Highlight (using notes) the selection (if present) when grabbing the a link from Skim.app"
  146. :tag "Highlight selection in Skim.app"
  147. :group 'org-mac-link
  148. :type 'boolean)
  149. (defgroup org-mac-flagged-mail nil
  150. "Options concerning linking to flagged Mail.app messages."
  151. :tag "Org Mail.app"
  152. :group 'org-link)
  153. (defcustom org-mac-mail-account "customize"
  154. "The Mail.app account in which to search for flagged messages."
  155. :group 'org-mac-flagged-mail
  156. :type 'string)
  157. ;; In mac.c, removed in Emacs 23.
  158. (declare-function do-applescript "org-mac-message" (script))
  159. (unless (fboundp 'do-applescript)
  160. ;; Need to fake this using shell-command-to-string
  161. (defun do-applescript (script)
  162. (let (start cmd return)
  163. (while (string-match "\n" script)
  164. (setq script (replace-match "\r" t t script)))
  165. (while (string-match "'" script start)
  166. (setq start (+ 2 (match-beginning 0))
  167. script (replace-match "\\'" t t script)))
  168. (setq cmd (concat "osascript -e '" script "'"))
  169. (setq return (shell-command-to-string cmd))
  170. (concat "\"" (org-trim return) "\""))))
  171. (defun org-mac-grab-link ()
  172. "Prompt the user for an application to grab a link from, then go grab the link, and insert it at point"
  173. (interactive)
  174. (let* ((descriptors `(("F" "inder" org-mac-finder-insert-selected ,org-mac-grab-Finder-app-p)
  175. ("m" "ail" org-mac-message-insert-selected ,org-mac-grab-Mail-app-p)
  176. ("o" "utlook" org-mac-outlook-message-insert-selected ,org-mac-grab-Outlook-app-p)
  177. ("a" "ddressbook" org-mac-addressbook-insert-selected ,org-mac-grab-Addressbook-app-p)
  178. ("s" "afari" org-mac-safari-insert-frontmost-url ,org-mac-grab-Safari-app-p)
  179. ("f" "irefox" org-mac-firefox-insert-frontmost-url ,org-mac-grab-Firefox-app-p)
  180. ("v" "imperator" org-mac-vimperator-insert-frontmost-url ,org-mac-grab-Firefox+Vimperator-p)
  181. ("c" "hrome" org-mac-chrome-insert-frontmost-url ,org-mac-grab-Chrome-app-p)
  182. ("t" "ogether" org-mac-together-insert-selected ,org-mac-grab-Together-app-p)
  183. ("S" "kim" org-mac-skim-insert-page ,org-mac-grab-Skim-app-p)))
  184. (menu-string (make-string 0 ?x))
  185. input)
  186. ;; Create the menu string for the keymap
  187. (mapc '(lambda (descriptor)
  188. (when (elt descriptor 3)
  189. (setf menu-string (concat menu-string "[" (elt descriptor 0) "]" (elt descriptor 1) " "))))
  190. descriptors)
  191. (setf (elt menu-string (- (length menu-string) 1)) ?:)
  192. ;; Prompt the user, and grab the link
  193. (message menu-string)
  194. (setq input (read-char-exclusive))
  195. (mapc '(lambda (descriptor)
  196. (let ((key (elt (elt descriptor 0) 0))
  197. (active (elt descriptor 3))
  198. (grab-function (elt descriptor 2)))
  199. (when (and active (eq input key))
  200. (call-interactively grab-function))))
  201. descriptors)))
  202. (defun org-mac-paste-applescript-links (as-link-list)
  203. "Paste in a list of links from an applescript handler. The
  204. links are of the form <link>::split::<name>"
  205. (let* ((link-list
  206. (mapcar
  207. (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x) (setq x (match-string 1 x))) x)
  208. (split-string as-link-list "[\r\n]+")))
  209. split-link URL description orglink orglink-insert rtn orglink-list)
  210. (while link-list
  211. (setq split-link (split-string (pop link-list) "::split::"))
  212. (setq URL (car split-link))
  213. (setq description (cadr split-link))
  214. (when (not (string= URL ""))
  215. (setq orglink (org-make-link-string URL description))
  216. (push orglink orglink-list)))
  217. (setq rtn (mapconcat 'identity orglink-list "\n"))
  218. (kill-new rtn)
  219. rtn))
  220. ;; Handle links from Firefox.app
  221. ;;
  222. ;; This code allows you to grab the current active url from the main
  223. ;; Firefox.app window, and insert it as a link into an org-mode
  224. ;; document. Unfortunately, firefox does not expose an applescript
  225. ;; dictionary, so this is necessarily introduces some limitations.
  226. ;;
  227. ;; The applescript to grab the url from Firefox.app uses the System
  228. ;; Events application to give focus to the firefox application, select
  229. ;; the contents of the url bar, and copy it. It then uses the title of
  230. ;; the window as the text of the link. There is no way to grab links
  231. ;; from other open tabs, and further, if there is more than one window
  232. ;; open, it is not clear which one will be used (though emperically it
  233. ;; seems that it is always the last active window).
  234. (defun org-as-mac-firefox-get-frontmost-url ()
  235. (let ((result (do-applescript
  236. (concat
  237. "set oldClipboard to the clipboard\n"
  238. "set frontmostApplication to path to frontmost application\n"
  239. "tell application \"Firefox\"\n"
  240. " activate\n"
  241. " delay 0.15\n"
  242. " tell application \"System Events\"\n"
  243. " keystroke \"l\" using {command down}\n"
  244. " keystroke \"a\" using {command down}\n"
  245. " keystroke \"c\" using {command down}\n"
  246. " end tell\n"
  247. " delay 0.15\n"
  248. " set theUrl to the clipboard\n"
  249. " set the clipboard to oldClipboard\n"
  250. " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
  251. "end tell\n"
  252. "activate application (frontmostApplication as text)\n"
  253. "set links to {}\n"
  254. "copy theResult to the end of links\n"
  255. "return links as string\n"))))
  256. (car (split-string result "[\r\n]+" t))))
  257. (defun org-mac-firefox-get-frontmost-url ()
  258. (interactive)
  259. (message "Applescript: Getting Firefox url...")
  260. (let* ((url-and-title (org-as-mac-firefox-get-frontmost-url))
  261. (split-link (split-string url-and-title "::split::"))
  262. (URL (car split-link))
  263. (description (cadr split-link))
  264. (org-link))
  265. (when (not (string= URL ""))
  266. (setq org-link (org-make-link-string URL description)))
  267. (kill-new org-link)
  268. org-link))
  269. (defun org-mac-firefox-insert-frontmost-url ()
  270. (interactive)
  271. (insert (org-mac-firefox-get-frontmost-url)))
  272. ;; Handle links from Google Firefox.app running the Vimperator extension
  273. ;; Grab the frontmost url from Firefox+Vimperator. Same limitations are
  274. ;; Firefox
  275. (defun org-as-mac-vimperator-get-frontmost-url ()
  276. (let ((result (do-applescript
  277. (concat
  278. "set oldClipboard to the clipboard\n"
  279. "set frontmostApplication to path to frontmost application\n"
  280. "tell application \"Firefox\"\n"
  281. " activate\n"
  282. " delay 0.15\n"
  283. " tell application \"System Events\"\n"
  284. " keystroke \"y\"\n"
  285. " end tell\n"
  286. " delay 0.15\n"
  287. " set theUrl to the clipboard\n"
  288. " set the clipboard to oldClipboard\n"
  289. " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
  290. "end tell\n"
  291. "activate application (frontmostApplication as text)\n"
  292. "set links to {}\n"
  293. "copy theResult to the end of links\n"
  294. "return links as string\n"))))
  295. (replace-regexp-in-string "\s+-\s+Vimperator" "" (car (split-string result "[\r\n]+" t)))))
  296. (defun org-mac-vimperator-get-frontmost-url ()
  297. (interactive)
  298. (message "Applescript: Getting Vimperator url...")
  299. (let* ((url-and-title (org-as-mac-vimperator-get-frontmost-url))
  300. (split-link (split-string url-and-title "::split::"))
  301. (URL (car split-link))
  302. (description (cadr split-link))
  303. (org-link))
  304. (when (not (string= URL ""))
  305. (setq org-link (org-make-link-string URL description)))
  306. (kill-new org-link)
  307. org-link))
  308. (defun org-mac-vimperator-insert-frontmost-url ()
  309. (interactive)
  310. (insert (org-mac-vimperator-get-frontmost-url)))
  311. ;; Handle links from Google Chrome.app
  312. ;; Grab the frontmost url from Google Chrome. Same limitations as
  313. ;; Firefox because Chrome doesn't publish an Applescript dictionary
  314. (defun org-as-mac-chrome-get-frontmost-url ()
  315. (let ((result (do-applescript
  316. (concat
  317. "set frontmostApplication to path to frontmost application\n"
  318. "tell application \"Google Chrome\"\n"
  319. " set theUrl to get URL of active tab of first window\n"
  320. " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
  321. "end tell\n"
  322. "activate application (frontmostApplication as text)\n"
  323. "set links to {}\n"
  324. "copy theResult to the end of links\n"
  325. "return links as string\n"))))
  326. (replace-regexp-in-string "^\"\\|\"$" ""
  327. (car (split-string result "[\r\n]+" t)))))
  328. (defun org-mac-chrome-get-frontmost-url ()
  329. (interactive)
  330. (message "Applescript: Getting Chrome url...")
  331. (let* ((url-and-title (org-as-mac-chrome-get-frontmost-url))
  332. (split-link (split-string url-and-title "::split::"))
  333. (URL (car split-link))
  334. (description (cadr split-link))
  335. (org-link))
  336. (when (not (string= URL ""))
  337. (setq org-link (org-make-link-string URL description)))
  338. (kill-new org-link)
  339. org-link))
  340. (defun org-mac-chrome-insert-frontmost-url ()
  341. (interactive)
  342. (insert (org-mac-chrome-get-frontmost-url)))
  343. ;; Handle links from Safari.app
  344. ;; Grab the frontmost url from Safari.
  345. (defun org-as-mac-safari-get-frontmost-url ()
  346. (let ((result (do-applescript
  347. (concat
  348. "tell application \"Safari\"\n"
  349. " set theUrl to URL of document 1\n"
  350. " set theName to the name of the document 1\n"
  351. " return theUrl & \"::split::\" & theName & \"\n\"\n"
  352. "end tell\n"))))
  353. (car (split-string result "[\r\n]+" t))))
  354. (defun org-mac-safari-get-frontmost-url ()
  355. (interactive)
  356. (message "Applescript: Getting Safari url...")
  357. (let* ((url-and-title (org-as-mac-safari-get-frontmost-url))
  358. (split-link (split-string url-and-title "::split::"))
  359. (URL (car split-link))
  360. (description (cadr split-link))
  361. (org-link))
  362. (when (not (string= URL ""))
  363. (setq org-link (org-make-link-string URL description)))
  364. (kill-new org-link)
  365. org-link))
  366. (defun org-mac-safari-insert-frontmost-url ()
  367. (interactive)
  368. (insert (org-mac-safari-get-frontmost-url)))
  369. ;;
  370. ;;
  371. ;; Handle links from together.app
  372. ;;
  373. ;;
  374. (org-add-link-type "x-together-item" 'org-mac-together-item-open)
  375. (defun org-mac-together-item-open (uid)
  376. "Open the given uid, which is a reference to an item in Together"
  377. (shell-command (concat "open -a Together \"x-together-item:" uid "\"")))
  378. (defun as-get-selected-together-items ()
  379. (do-applescript
  380. (concat
  381. "tell application \"Together\"\n"
  382. " set theLinkList to {}\n"
  383. " set theSelection to selected items\n"
  384. " repeat with theItem in theSelection\n"
  385. " set theLink to (get item link of theItem) & \"::split::\" & (get name of theItem) & \"\n\"\n"
  386. " copy theLink to end of theLinkList\n"
  387. " end repeat\n"
  388. " return theLinkList as string\n"
  389. "end tell")))
  390. (defun org-mac-together-get-selected ()
  391. (interactive)
  392. (message "Applescript: Getting Togther items...")
  393. (org-mac-paste-applescript-links (as-get-selected-together-items)))
  394. (defun org-mac-together-insert-selected ()
  395. (interactive)
  396. (insert (org-mac-together-get-selected)))
  397. ;;
  398. ;;
  399. ;; Handle links from Finder.app
  400. ;;
  401. ;;
  402. (defun as-get-selected-finder-items ()
  403. (do-applescript
  404. (concat
  405. "tell application \"Finder\"\n"
  406. " set theSelection to the selection\n"
  407. " set links to {}\n"
  408. " repeat with theItem in theSelection\n"
  409. " set theLink to \"file://\" & (POSIX path of (theItem as string)) & \"::split::\" & (get the name of theItem) & \"\n\"\n"
  410. " copy theLink to the end of links\n"
  411. " end repeat\n"
  412. " return links as string\n"
  413. "end tell\n")))
  414. (defun org-mac-finder-item-get-selected ()
  415. (interactive)
  416. (message "Applescript: Getting Finder items...")
  417. (org-mac-paste-applescript-links (as-get-selected-finder-items)))
  418. (defun org-mac-finder-insert-selected ()
  419. (interactive)
  420. (insert (org-mac-finder-item-get-selected)))
  421. ;;
  422. ;;
  423. ;; Handle links from AddressBook.app
  424. ;;
  425. ;;
  426. (org-add-link-type "addressbook" 'org-mac-addressbook-item-open)
  427. (defun org-mac-addressbook-item-open (uid)
  428. "Open the given uid, which is a reference to an item in Together"
  429. (shell-command (concat "open \"addressbook:" uid "\"")))
  430. (defun as-get-selected-addressbook-items ()
  431. (do-applescript
  432. (concat
  433. "tell application \"Address Book\"\n"
  434. " set theSelection to the selection\n"
  435. " set links to {}\n"
  436. " repeat with theItem in theSelection\n"
  437. " set theLink to \"addressbook://\" & (the id of theItem) & \"::split::\" & (the name of theItem) & \"\n\"\n"
  438. " copy theLink to the end of links\n"
  439. " end repeat\n"
  440. " return links as string\n"
  441. "end tell\n")))
  442. (defun org-mac-addressbook-item-get-selected ()
  443. (interactive)
  444. (message "Applescript: Getting Address Book items...")
  445. (org-mac-paste-applescript-links (as-get-selected-addressbook-items)))
  446. (defun org-mac-addressbook-insert-selected ()
  447. (interactive)
  448. (insert (org-mac-addressbook-item-get-selected)))
  449. ;;
  450. ;;
  451. ;; Handle links from Skim.app
  452. ;;
  453. ;; Original code & idea by Christopher Suckling (org-mac-protocol)
  454. (org-add-link-type "skim" 'org-mac-skim-open)
  455. (defun org-mac-skim-open (uri)
  456. "Visit page of pdf in Skim"
  457. (let* ((page (when (string-match "::\\(.+\\)\\'" uri)
  458. (match-string 1 uri)))
  459. (document (substring uri 0 (match-beginning 0))))
  460. (do-applescript
  461. (concat
  462. "tell application \"Skim\"\n"
  463. "activate\n"
  464. "set theDoc to \"" document "\"\n"
  465. "set thePage to " page "\n"
  466. "open theDoc\n"
  467. "go document 1 to page thePage of document 1\n"
  468. "end tell"))))
  469. (defun as-get-skim-page-link ()
  470. (do-applescript
  471. (concat
  472. "tell application \"Skim\"\n"
  473. "set theDoc to front document\n"
  474. "set theTitle to (name of theDoc)\n"
  475. "set thePath to (path of theDoc)\n"
  476. "set thePage to (get index for current page of theDoc)\n"
  477. "set theSelection to selection of theDoc\n"
  478. "set theContent to contents of (get text for theSelection)\n"
  479. "if theContent is missing value then\n"
  480. " set theContent to theTitle & \", p. \" & thePage\n"
  481. (when org-mac-Skim-highlight-selection-p
  482. (concat
  483. "else\n"
  484. " tell theDoc\n"
  485. " set theNote to make note with properties {type:highlight note, selection:theSelection}\n"
  486. " set text of theNote to (get text for theSelection)\n"
  487. " end tell\n"))
  488. "end if\n"
  489. "set theLink to \"skim://\" & thePath & \"::\" & thePage & "
  490. "\"::split::\" & theContent\n"
  491. "end tell\n"
  492. "return theLink as string\n")))
  493. (defun org-mac-skim-get-page ()
  494. (interactive)
  495. (message "Applescript: Getting Skim page link...")
  496. (let* ((link-and-descr (as-get-skim-page-link))
  497. (split-link (split-string link-and-descr "::split::"))
  498. (link (car split-link))
  499. (description (cadr split-link))
  500. (org-link))
  501. (when (not (string= link ""))
  502. (setq org-link (org-make-link-string link description)))
  503. (kill-new org-link)
  504. org-link))
  505. (defun org-mac-skim-insert-page ()
  506. (interactive)
  507. (insert (org-mac-skim-get-page)))
  508. ;;
  509. ;;
  510. ;; Handle links from Microsoft Outlook.app
  511. ;;
  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 of the
  598. active mail in Microsoft Outlook.app and make a link out 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 heading's first
  604. level. If heading doesn't exist, create it at point-max. Insert
  605. list of mac-outlook:// links to flagged mail after heading."
  606. (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
  607. (with-current-buffer org-buffer
  608. (goto-char (point-min))
  609. (let ((isearch-forward t)
  610. (message-re "\\[\\[\\(mac-outlook:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
  611. (if (org-goto-local-search-headings org-heading nil t)
  612. (if (not (eobp))
  613. (progn
  614. (save-excursion
  615. (while (re-search-forward
  616. message-re (save-excursion (outline-next-heading)) t)
  617. (delete-region (match-beginning 0) (match-end 0)))
  618. (insert "\n" (org-mac-outlook-message-get-links "f")))
  619. (flush-lines "^$" (point) (outline-next-heading)))
  620. (insert "\n" (org-mac-outlook-message-get-links "f")))
  621. (goto-char (point-max))
  622. (insert "\n")
  623. (org-insert-heading nil t)
  624. (insert org-heading "\n" (org-mac-outlook-message-get-links "f"))))))
  625. ;;
  626. ;;
  627. ;; Handle links from Mail.app
  628. ;;
  629. (org-add-link-type "message" 'org-mac-message-open)
  630. (defun org-mac-message-open (message-id)
  631. "Visit the message with the given MESSAGE-ID.
  632. This will use the command `open' with the message URL."
  633. (start-process (concat "open message:" message-id) nil
  634. "open" (concat "message://<" (substring message-id 2) ">")))
  635. (defun org-as-get-selected-mail ()
  636. "AppleScript to create links to selected messages in Mail.app."
  637. (do-applescript
  638. (concat
  639. "tell application \"Mail\"\n"
  640. "set theLinkList to {}\n"
  641. "set theSelection to selection\n"
  642. "repeat with theMessage in theSelection\n"
  643. "set theID to message id of theMessage\n"
  644. "set theSubject to subject of theMessage\n"
  645. "set theLink to \"message://\" & theID & \"::split::\" & theSubject\n"
  646. "if (theLinkList is not equal to {}) then\n"
  647. "set theLink to \"\n\" & theLink\n"
  648. "end if\n"
  649. "copy theLink to end of theLinkList\n"
  650. "end repeat\n"
  651. "return theLinkList as string\n"
  652. "end tell")))
  653. (defun org-as-get-flagged-mail ()
  654. "AppleScript to create links to flagged messages in Mail.app."
  655. (do-applescript
  656. (concat
  657. ;; Is Growl installed?
  658. "tell application \"System Events\"\n"
  659. "set growlHelpers to the name of every process whose creator type contains \"GRRR\"\n"
  660. "if (count of growlHelpers) > 0 then\n"
  661. "set growlHelperApp to item 1 of growlHelpers\n"
  662. "else\n"
  663. "set growlHelperApp to \"\"\n"
  664. "end if\n"
  665. "end tell\n"
  666. ;; Get links
  667. "tell application \"Mail\"\n"
  668. "set theMailboxes to every mailbox of account \"" org-mac-mail-account "\"\n"
  669. "set theLinkList to {}\n"
  670. "repeat with aMailbox in theMailboxes\n"
  671. "set theSelection to (every message in aMailbox whose flagged status = true)\n"
  672. "repeat with theMessage in theSelection\n"
  673. "set theID to message id of theMessage\n"
  674. "set theSubject to subject of theMessage\n"
  675. "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
  676. "copy theLink to end of theLinkList\n"
  677. ;; Report progress through Growl
  678. ;; This "double tell" idiom is described in detail at
  679. ;; http://macscripter.net/viewtopic.php?id=24570 The
  680. ;; script compiler needs static knowledge of the
  681. ;; growlHelperApp. Hmm, since we're compiling
  682. ;; on-the-fly here, this is likely to be way less
  683. ;; portable than I'd hoped. It'll work when the name
  684. ;; is still "GrowlHelperApp", though.
  685. "if growlHelperApp is not \"\" then\n"
  686. "tell application \"GrowlHelperApp\"\n"
  687. "tell application growlHelperApp\n"
  688. "set the allNotificationsList to {\"FlaggedMail\"}\n"
  689. "set the enabledNotificationsList to allNotificationsList\n"
  690. "register as application \"FlaggedMail\" all notifications allNotificationsList default notifications enabledNotificationsList icon of application \"Mail\"\n"
  691. "notify with name \"FlaggedMail\" title \"Importing flagged message\" description theSubject application name \"FlaggedMail\"\n"
  692. "end tell\n"
  693. "end tell\n"
  694. "end if\n"
  695. "end repeat\n"
  696. "end repeat\n"
  697. "return theLinkList as string\n"
  698. "end tell")))
  699. (defun org-mac-message-get-links (&optional select-or-flag)
  700. "Create links to the messages currently selected or flagged in Mail.app.
  701. This will use AppleScript to get the message-id and the subject of the
  702. messages in Mail.app and make a link out of it.
  703. When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
  704. the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
  705. The Org-syntax text will be pushed to the kill ring, and also returned."
  706. (interactive "sLink to (s)elected or (f)lagged messages: ")
  707. (setq select-or-flag (or select-or-flag "s"))
  708. (message "AppleScript: searching mailboxes...")
  709. (let* ((as-link-list
  710. (if (string= select-or-flag "s")
  711. (org-as-get-selected-mail)
  712. (if (string= select-or-flag "f")
  713. (org-as-get-flagged-mail)
  714. (error "Please select \"s\" or \"f\""))))
  715. (link-list
  716. (mapcar
  717. (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x) (setq x (match-string 1 x))) x)
  718. (split-string (substring as-link-list 1 -1) "[\r\n]+")))
  719. split-link URL description orglink orglink-insert rtn orglink-list)
  720. (while link-list
  721. (setq split-link (split-string (pop link-list) "::split::"))
  722. (setq URL (car split-link))
  723. (setq description (cadr split-link))
  724. (when (not (string= URL ""))
  725. (setq orglink (org-make-link-string URL description))
  726. (push orglink orglink-list)))
  727. (setq rtn (mapconcat 'identity orglink-list "\n"))
  728. (kill-new rtn)
  729. rtn))
  730. (defun org-mac-message-insert-selected ()
  731. "Insert a link to the messages currently selected in Mail.app.
  732. This will use AppleScript to get the message-id and the subject of the
  733. active mail in Mail.app and make a link out of it."
  734. (interactive)
  735. (insert (org-mac-message-get-links "s")))
  736. ;; The following line is for backward compatibility
  737. (defalias 'org-mac-message-insert-link 'org-mac-message-insert-selected)
  738. (defun org-mac-message-insert-flagged (org-buffer org-heading)
  739. "Asks for an org buffer and a heading within it, and replace message links.
  740. If heading exists, delete all message:// links within heading's first
  741. level. If heading doesn't exist, create it at point-max. Insert
  742. list of message:// links to flagged mail after heading."
  743. (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
  744. (with-current-buffer org-buffer
  745. (goto-char (point-min))
  746. (let ((isearch-forward t)
  747. (message-re "\\[\\[\\(message:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
  748. (if (org-goto-local-search-headings org-heading nil t)
  749. (if (not (eobp))
  750. (progn
  751. (save-excursion
  752. (while (re-search-forward
  753. message-re (save-excursion (outline-next-heading)) t)
  754. (delete-region (match-beginning 0) (match-end 0)))
  755. (insert "\n" (org-mac-message-get-links "f")))
  756. (flush-lines "^$" (point) (outline-next-heading)))
  757. (insert "\n" (org-mac-message-get-links "f")))
  758. (goto-char (point-max))
  759. (insert "\n")
  760. (org-insert-heading nil t)
  761. (insert org-heading "\n" (org-mac-message-get-links "f"))))))
  762. (provide 'org-mac-link)
  763. ;;; org-mac-link.el ends here