org-mac-link.el 27 KB

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