org-mac-link.el 37 KB

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