org-mac-link.el 27 KB

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