org-mac-link.el 33 KB

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