org-mac-link.el 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. ;;; org-mac-link.el --- Insert org-mode links to items selected in various Mac apps
  2. ;;
  3. ;; Copyright (c) 2010-2018 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 <http://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
  191. (< 0 (length (shell-command-to-string
  192. "mdfind kMDItemCFBundleIdentifier == 'com.evernote.Evernote'")))
  193. "Add menu option [e]vernote to grab note links from Evernote.app."
  194. :tag "Grab Evernote.app note links"
  195. :group 'org-mac-link
  196. :type 'boolean)
  197. (defcustom org-mac-evernote-path (replace-regexp-in-string (rx (* (any " \t\n")) eos)
  198. ""
  199. (shell-command-to-string
  200. "mdfind kMDItemCFBundleIdentifier == 'com.evernote.Evernote'"))
  201. "The path to the installed copy of Evernote.app. Do not escape spaces as the AppleScript call will quote this string."
  202. :tag "Path to Evernote"
  203. :group 'org-mac-link
  204. :type 'string)
  205. ;; In mac.c, removed in Emacs 23.
  206. (declare-function do-applescript "org-mac-message" (script))
  207. (unless (fboundp 'do-applescript)
  208. ;; Need to fake this using shell-command-to-string
  209. (defun do-applescript (script)
  210. (let (start cmd return)
  211. (while (string-match "\n" script)
  212. (setq script (replace-match "\r" t t script)))
  213. (while (string-match "'" script start)
  214. (setq start (+ 2 (match-beginning 0))
  215. script (replace-match "\\'" t t script)))
  216. (setq cmd (concat "osascript -e '" script "'"))
  217. (setq return (shell-command-to-string cmd))
  218. (concat "\"" (org-trim return) "\""))))
  219. ;;;###autoload
  220. (defun org-mac-grab-link ()
  221. "Prompt for an application to grab a link from.
  222. When done, go grab the link, and insert it at point."
  223. (interactive)
  224. (let* ((descriptors
  225. `(("F" "inder" org-mac-finder-insert-selected ,org-mac-grab-Finder-app-p)
  226. ("m" "ail" org-mac-message-insert-selected ,org-mac-grab-Mail-app-p)
  227. ("d" "EVONthink Pro Office" org-mac-devonthink-item-insert-selected
  228. ,org-mac-grab-devonthink-app-p)
  229. ("o" "utlook" org-mac-outlook-message-insert-selected ,org-mac-grab-Outlook-app-p)
  230. ("a" "ddressbook" org-mac-addressbook-insert-selected ,org-mac-grab-Addressbook-app-p)
  231. ("s" "afari" org-mac-safari-insert-frontmost-url ,org-mac-grab-Safari-app-p)
  232. ("f" "irefox" org-mac-firefox-insert-frontmost-url ,org-mac-grab-Firefox-app-p)
  233. ("v" "imperator" org-mac-vimperator-insert-frontmost-url ,org-mac-grab-Firefox+Vimperator-p)
  234. ("c" "hrome" org-mac-chrome-insert-frontmost-url ,org-mac-grab-Chrome-app-p)
  235. ("b" "rave" org-mac-brave-insert-frontmost-url ,org-mac-grab-Brave-app-p)
  236. ("e" "evernote" org-mac-evernote-note-insert-selected ,org-mac-grab-Evernote-app-p)
  237. ("t" "ogether" org-mac-together-insert-selected ,org-mac-grab-Together-app-p)
  238. ("S" "kim" org-mac-skim-insert-page ,org-mac-grab-Skim-app-p)
  239. ("A" "crobat" org-mac-acrobat-insert-page ,org-mac-grab-Acrobat-app-p)))
  240. (menu-string (make-string 0 ?x))
  241. input)
  242. ;; Create the menu string for the keymap
  243. (mapc (lambda (descriptor)
  244. (when (elt descriptor 3)
  245. (setf menu-string (concat menu-string
  246. "[" (elt descriptor 0) "]"
  247. (elt descriptor 1) " "))))
  248. descriptors)
  249. (setf (elt menu-string (- (length menu-string) 1)) ?:)
  250. ;; Prompt the user, and grab the link
  251. (message menu-string)
  252. (setq input (read-char-exclusive))
  253. (mapc (lambda (descriptor)
  254. (let ((key (elt (elt descriptor 0) 0))
  255. (active (elt descriptor 3))
  256. (grab-function (elt descriptor 2)))
  257. (when (and active (eq input key))
  258. (call-interactively grab-function))))
  259. descriptors)))
  260. (defun org-mac-paste-applescript-links (as-link-list)
  261. "Paste in a list of links from an applescript handler.
  262. The links are of the form <link>::split::<name>."
  263. (let* ((noquote-as-link-list
  264. (if (string-prefix-p "\"" as-link-list)
  265. (substring as-link-list 1 -1)
  266. as-link-list))
  267. (link-list
  268. (mapcar (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x)
  269. (setq x (match-string 1 x)))
  270. x)
  271. (split-string noquote-as-link-list "[\r\n]+")))
  272. split-link URL description orglink orglink-insert rtn orglink-list)
  273. (while link-list
  274. (setq split-link (split-string (pop link-list) "::split::"))
  275. (setq URL (car split-link))
  276. (setq description (cadr split-link))
  277. (when (not (string= URL ""))
  278. (setq orglink (org-make-link-string URL description))
  279. (push orglink orglink-list)))
  280. (setq rtn (mapconcat 'identity orglink-list "\n"))
  281. (kill-new rtn)
  282. rtn))
  283. ;; Handle links from Firefox.app
  284. ;;
  285. ;; This code allows you to grab the current active url from the main
  286. ;; Firefox.app window, and insert it as a link into an org-mode
  287. ;; document. Unfortunately, firefox does not expose an applescript
  288. ;; dictionary, so this is necessarily introduces some limitations.
  289. ;;
  290. ;; The applescript to grab the url from Firefox.app uses the System
  291. ;; Events application to give focus to the firefox application, select
  292. ;; the contents of the url bar, and copy it. It then uses the title of
  293. ;; the window as the text of the link. There is no way to grab links
  294. ;; from other open tabs, and further, if there is more than one window
  295. ;; open, it is not clear which one will be used (though emperically it
  296. ;; seems that it is always the last active window).
  297. (defun org-as-mac-firefox-get-frontmost-url ()
  298. (let ((result
  299. (do-applescript
  300. (concat
  301. "set oldClipboard to the clipboard\n"
  302. "set frontmostApplication to path to frontmost application\n"
  303. "tell application \"Firefox\"\n"
  304. " activate\n"
  305. " delay 0.15\n"
  306. " tell application \"System Events\"\n"
  307. " keystroke \"l\" using {command down}\n"
  308. " keystroke \"a\" using {command down}\n"
  309. " keystroke \"c\" using {command down}\n"
  310. " end tell\n"
  311. " delay 0.15\n"
  312. " set theUrl to the clipboard\n"
  313. " set the clipboard to oldClipboard\n"
  314. " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
  315. "end tell\n"
  316. "activate application (frontmostApplication as text)\n"
  317. "set links to {}\n"
  318. "copy theResult to the end of links\n"
  319. "return links as string\n"))))
  320. (car (split-string result "[\r\n]+" t))))
  321. ;;;###autoload
  322. (defun org-mac-firefox-get-frontmost-url ()
  323. (interactive)
  324. (message "Applescript: Getting Firefox url...")
  325. (org-mac-paste-applescript-links (org-as-mac-firefox-get-frontmost-url)))
  326. ;;;###autoload
  327. (defun org-mac-firefox-insert-frontmost-url ()
  328. (interactive)
  329. (insert (org-mac-firefox-get-frontmost-url)))
  330. ;; Handle links from Google Firefox.app running the Vimperator extension
  331. ;; Grab the frontmost url from Firefox+Vimperator. Same limitations are
  332. ;; Firefox
  333. (defun org-as-mac-vimperator-get-frontmost-url ()
  334. (let ((result
  335. (do-applescript
  336. (concat
  337. "set oldClipboard to the clipboard\n"
  338. "set frontmostApplication to path to frontmost application\n"
  339. "tell application \"Firefox\"\n"
  340. " activate\n"
  341. " delay 0.15\n"
  342. " tell application \"System Events\"\n"
  343. " keystroke \"y\"\n"
  344. " end tell\n"
  345. " delay 0.15\n"
  346. " set theUrl to the clipboard\n"
  347. " set the clipboard to oldClipboard\n"
  348. " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
  349. "end tell\n"
  350. "activate application (frontmostApplication as text)\n"
  351. "set links to {}\n"
  352. "copy theResult to the end of links\n"
  353. "return links as string\n"))))
  354. (replace-regexp-in-string
  355. "\s+-\s+Vimperator" "" (car (split-string result "[\r\n]+" t)))))
  356. ;;;###autoload
  357. (defun org-mac-vimperator-get-frontmost-url ()
  358. (interactive)
  359. (message "Applescript: Getting Vimperator url...")
  360. (org-mac-paste-applescript-links (org-as-mac-vimperator-get-frontmost-url)))
  361. ;;;###autoload
  362. (defun org-mac-vimperator-insert-frontmost-url ()
  363. (interactive)
  364. (insert (org-mac-vimperator-get-frontmost-url)))
  365. ;; Handle links from Google Chrome.app
  366. ;; Grab the frontmost url from Google Chrome. Same limitations as
  367. ;; Firefox because Chrome doesn't publish an Applescript dictionary
  368. (defun org-as-mac-chrome-get-frontmost-url ()
  369. (let ((result
  370. (do-applescript
  371. (concat
  372. "set frontmostApplication to path to frontmost application\n"
  373. "tell application \"Google Chrome\"\n"
  374. " set theUrl to get URL of active tab of first window\n"
  375. " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
  376. "end tell\n"
  377. "activate application (frontmostApplication as text)\n"
  378. "set links to {}\n"
  379. "copy theResult to the end of links\n"
  380. "return links as string\n"))))
  381. (replace-regexp-in-string
  382. "^\"\\|\"$" "" (car (split-string result "[\r\n]+" t)))))
  383. ;;;###autoload
  384. (defun org-mac-chrome-get-frontmost-url ()
  385. (interactive)
  386. (message "Applescript: Getting Chrome url...")
  387. (org-mac-paste-applescript-links (org-as-mac-chrome-get-frontmost-url)))
  388. ;;;###autoload
  389. (defun org-mac-chrome-insert-frontmost-url ()
  390. (interactive)
  391. (insert (org-mac-chrome-get-frontmost-url)))
  392. ;; Handle links from Brave.app
  393. ;; Grab the frontmost url from Brave. Same limitations as
  394. ;; Firefox/Chrome because Brave doesn't publish an Applescript
  395. ;; dictionary
  396. (defun org-as-mac-brave-get-frontmost-url ()
  397. (let ((result
  398. (do-applescript
  399. (concat
  400. "set frontmostApplication to path to frontmost application\n"
  401. "tell application \"Brave\"\n"
  402. " set theUrl to get URL of active tab of first window\n"
  403. " set theResult to (get theUrl) & \"::split::\" & (get name of window 1)\n"
  404. "end tell\n"
  405. "activate application (frontmostApplication as text)\n"
  406. "set links to {}\n"
  407. "copy theResult to the end of links\n"
  408. "return links as string\n"))))
  409. (replace-regexp-in-string
  410. "^\"\\|\"$" "" (car (split-string result "[\r\n]+" t)))))
  411. ;;;###autoload
  412. (defun org-mac-brave-get-frontmost-url ()
  413. (interactive)
  414. (message "Applescript: Getting Brave url...")
  415. (org-mac-paste-applescript-links (org-as-mac-brave-get-frontmost-url)))
  416. ;;;###autoload
  417. (defun org-mac-brave-insert-frontmost-url ()
  418. (interactive)
  419. (insert (org-mac-brave-get-frontmost-url)))
  420. ;; Handle links from Safari.app
  421. ;; Grab the frontmost url from Safari.
  422. (defun org-as-mac-safari-get-frontmost-url ()
  423. (do-applescript
  424. (concat
  425. "tell application \"Safari\"\n"
  426. " set theUrl to URL of document 1\n"
  427. " set theName to the name of the document 1\n"
  428. " return theUrl & \"::split::\" & theName & \"\n\"\n"
  429. "end tell\n")))
  430. ;;;###autoload
  431. (defun org-mac-safari-get-frontmost-url ()
  432. (interactive)
  433. (message "Applescript: Getting Safari url...")
  434. (org-mac-paste-applescript-links
  435. (org-as-mac-safari-get-frontmost-url)))
  436. ;;;###autoload
  437. (defun org-mac-safari-insert-frontmost-url ()
  438. (interactive)
  439. (insert (org-mac-safari-get-frontmost-url)))
  440. ;; Handle links from together.app
  441. (org-link-set-parameters "x-together-item" :follow #'org-mac-together-item-open)
  442. (defun org-mac-together-item-open (uid)
  443. "Open UID, which is a reference to an item in Together."
  444. (shell-command (concat "open -a Together \"x-together-item:" uid "\"")))
  445. (defun as-get-selected-together-items ()
  446. (do-applescript
  447. (concat
  448. "tell application \"Together\"\n"
  449. " set theLinkList to {}\n"
  450. " set theSelection to selected items\n"
  451. " repeat with theItem in theSelection\n"
  452. " set theLink to (get item link of theItem) & \"::split::\" & (get name of theItem) & \"\n\"\n"
  453. " copy theLink to end of theLinkList\n"
  454. " end repeat\n"
  455. " return theLinkList as string\n"
  456. "end tell")))
  457. ;;;###autoload
  458. (defun org-mac-together-get-selected ()
  459. (interactive)
  460. (message "Applescript: Getting Togther items...")
  461. (org-mac-paste-applescript-links (as-get-selected-together-items)))
  462. ;;;###autoload
  463. (defun org-mac-together-insert-selected ()
  464. (interactive)
  465. (insert (org-mac-together-get-selected)))
  466. ;; Handle links from Finder.app
  467. (defun as-get-selected-finder-items ()
  468. (do-applescript
  469. (concat
  470. "tell application \"Finder\"\n"
  471. " set theSelection to the selection\n"
  472. " set links to {}\n"
  473. " repeat with theItem in theSelection\n"
  474. " set theLink to \"file://\" & (POSIX path of (theItem as string)) & \"::split::\" & (get the name of theItem) & \"\n\"\n"
  475. " copy theLink to the end of links\n"
  476. " end repeat\n"
  477. " return links as string\n"
  478. "end tell\n")))
  479. ;;;###autoload
  480. (defun org-mac-finder-item-get-selected ()
  481. (interactive)
  482. (message "Applescript: Getting Finder items...")
  483. (org-mac-paste-applescript-links (as-get-selected-finder-items)))
  484. ;;;###autoload
  485. (defun org-mac-finder-insert-selected ()
  486. (interactive)
  487. (insert (org-mac-finder-item-get-selected)))
  488. ;; Handle links from AddressBook.app
  489. (org-link-set-parameters "addressbook" :follow #'org-mac-addressbook-item-open)
  490. (defun org-mac-addressbook-item-open (uid)
  491. "Open UID, which is a reference to an item in the addressbook."
  492. (shell-command (concat "open \"addressbook:" uid "\"")))
  493. (defun as-get-selected-addressbook-items ()
  494. (do-applescript
  495. (concat
  496. "tell application \"Address Book\"\n"
  497. " set theSelection to the selection\n"
  498. " set links to {}\n"
  499. " repeat with theItem in theSelection\n"
  500. " set theLink to \"addressbook://\" & (the id of theItem) & \"::split::\" & (the name of theItem) & \"\n\"\n"
  501. " copy theLink to the end of links\n"
  502. " end repeat\n"
  503. " return links as string\n"
  504. "end tell\n")))
  505. ;;;###autoload
  506. (defun org-mac-addressbook-item-get-selected ()
  507. (interactive)
  508. (message "Applescript: Getting Address Book items...")
  509. (org-mac-paste-applescript-links (as-get-selected-addressbook-items)))
  510. ;;;###autoload
  511. (defun org-mac-addressbook-insert-selected ()
  512. (interactive)
  513. (insert (org-mac-addressbook-item-get-selected)))
  514. ;; Handle links from Skim.app
  515. ;;
  516. ;; Original code & idea by Christopher Suckling (org-mac-protocol)
  517. (org-link-set-parameters "skim" :follow #'org-mac-skim-open)
  518. (defun org-mac-skim-open (uri)
  519. "Visit page of pdf in Skim"
  520. (let* ((page (when (string-match "::\\(.+\\)\\'" uri)
  521. (match-string 1 uri)))
  522. (document (substring uri 0 (match-beginning 0))))
  523. (do-applescript
  524. (concat
  525. "tell application \"Skim\"\n"
  526. "activate\n"
  527. "set theDoc to \"" document "\"\n"
  528. "set thePage to " page "\n"
  529. "open theDoc\n"
  530. "go document 1 to page thePage of document 1\n"
  531. "end tell"))))
  532. (defun as-get-skim-page-link ()
  533. (do-applescript
  534. (concat
  535. "tell application \"Skim\"\n"
  536. "set theDoc to front document\n"
  537. "set theTitle to (name of theDoc)\n"
  538. "set thePath to (path of theDoc)\n"
  539. "set thePage to (get index for current page of theDoc)\n"
  540. "set theSelection to selection of theDoc\n"
  541. "set theContent to contents of (get text for theSelection)\n"
  542. "if theContent is missing value then\n"
  543. " set theContent to theTitle & \", p. \" & thePage\n"
  544. (when org-mac-Skim-highlight-selection-p
  545. (concat
  546. "else\n"
  547. " tell theDoc\n"
  548. " set theNote to make note with properties {type:highlight note, selection:theSelection}\n"
  549. " set text of theNote to (get text for theSelection)\n"
  550. " end tell\n"))
  551. "end if\n"
  552. "set theLink to \"skim://\" & thePath & \"::\" & thePage & "
  553. "\"::split::\" & theContent\n"
  554. "end tell\n"
  555. "return theLink as string\n")))
  556. ;;;###autoload
  557. (defun org-mac-skim-get-page ()
  558. (interactive)
  559. (message "Applescript: Getting Skim page link...")
  560. (org-mac-paste-applescript-links (as-get-skim-page-link)))
  561. ;;;###autoload
  562. (defun org-mac-skim-insert-page ()
  563. (interactive)
  564. (insert (org-mac-skim-get-page)))
  565. ;; Handle links from Adobe Acrobat Pro.app
  566. ;;
  567. ;; Original code & idea by Christopher Suckling (org-mac-protocol)
  568. ;;
  569. ;; The URI format is path_to_pdf_file::page_number
  570. (org-link-set-parameters "acrobat" :follow #'org-mac-acrobat-open)
  571. (defun org-mac-acrobat-open (uri)
  572. "Visit page of pdf in Acrobat"
  573. (let* ((page (when (string-match "::\\(.+\\)\\'" uri)
  574. (match-string 1 uri)))
  575. (document (substring uri 0 (match-beginning 0))))
  576. (do-applescript
  577. (concat
  578. "tell application \"Adobe Acrobat Pro\"\n"
  579. " activate\n"
  580. " set theDoc to \"" document "\"\n"
  581. " set thePage to " page "\n"
  582. " open theDoc\n"
  583. " tell PDF Window 1\n"
  584. " goto page thePage\n"
  585. " end tell\n"
  586. "end tell"))))
  587. ;; The applescript returns link in the format
  588. ;; "adobe:path_to_pdf_file::page_number::split::document_title, p.page_label"
  589. (defun org-mac-as-get-acrobat-page-link ()
  590. (do-applescript
  591. (concat
  592. "tell application \"Adobe Acrobat Pro\"\n"
  593. " set theDoc to active doc\n"
  594. " set theWindow to (PDF Window 1 of theDoc)\n"
  595. " set thePath to (file alias of theDoc)\n"
  596. " set theTitle to (name of theWindow)\n"
  597. " set thePage to (page number of theWindow)\n"
  598. " set theLabel to (label text of (page thePage of theWindow))\n"
  599. "end tell\n"
  600. "set theResult to \"acrobat:\" & thePath & \"::\" & thePage & \"::split::\" & theTitle & \", p.\" & theLabel\n"
  601. "return theResult as string\n")))
  602. ;;;###autoload
  603. (defun org-mac-acrobat-get-page ()
  604. (interactive)
  605. (message "Applescript: Getting Acrobat page link...")
  606. (org-mac-paste-applescript-links (org-mac-as-get-acrobat-page-link)))
  607. ;;;###autoload
  608. (defun org-mac-acrobat-insert-page ()
  609. (interactive)
  610. (insert (org-mac-acrobat-get-page)))
  611. ;; Handle links from Microsoft Outlook.app
  612. (org-link-set-parameters "mac-outlook" :follow #'org-mac-outlook-message-open)
  613. (defun org-mac-outlook-message-open (msgid)
  614. "Open a message in Outlook"
  615. (do-applescript
  616. (concat
  617. "tell application \"" org-mac-outlook-path "\"\n"
  618. (format "open message id %s\n" (substring-no-properties msgid))
  619. "activate\n"
  620. "end tell")))
  621. (defun org-as-get-selected-outlook-mail ()
  622. "AppleScript to create links to selected messages in Microsoft Outlook.app."
  623. (do-applescript
  624. (concat
  625. "tell application \"" org-mac-outlook-path "\"\n"
  626. "set msgCount to count current messages\n"
  627. "if (msgCount < 1) then\n"
  628. "return\n"
  629. "end if\n"
  630. "set theLinkList to {}\n"
  631. "set theSelection to (get current messages)\n"
  632. "repeat with theMessage in theSelection\n"
  633. "set theID to id of theMessage as string\n"
  634. "set theURL to \"mac-outlook:\" & theID\n"
  635. "set theSubject to subject of theMessage\n"
  636. "set theLink to theURL & \"::split::\" & theSubject & \"\n\"\n"
  637. "copy theLink to end of theLinkList\n"
  638. "end repeat\n"
  639. "return theLinkList as string\n"
  640. "end tell")))
  641. (defun org-sh-get-flagged-outlook-mail ()
  642. "Shell commands to create links to flagged messages in Microsoft Outlook.app."
  643. (mapconcat
  644. (lambda (x) ""
  645. (concat
  646. "mac-outlook:"
  647. (mapconcat
  648. (lambda (y) "" y)
  649. (split-string
  650. (shell-command-to-string
  651. (format "mdls -raw -name com_microsoft_outlook_recordID -name kMDItemDisplayName \"%s\"" x))
  652. "\000")
  653. "::split::")
  654. "\n"))
  655. (with-temp-buffer
  656. (let ((coding-system-for-read (or file-name-coding-system 'utf-8))
  657. (coding-system-for-write 'utf-8))
  658. (shell-command
  659. "mdfind com_microsoft_outlook_flagged==1"
  660. (current-buffer)))
  661. (split-string
  662. (buffer-string) "\n" t))
  663. ""))
  664. ;;;###autoload
  665. (defun org-mac-outlook-message-get-links (&optional select-or-flag)
  666. "Create links to the messages currently selected or flagged in Microsoft Outlook.app.
  667. This will use AppleScript to get the message-id and the subject of the
  668. messages in Microsoft Outlook.app and make a link out of it.
  669. When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
  670. the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
  671. The Org-syntax text will be pushed to the kill ring, and also returned."
  672. (interactive "sLink to (s)elected or (f)lagged messages: ")
  673. (setq select-or-flag (or select-or-flag "s"))
  674. (message "Org Mac Outlook: searching mailboxes...")
  675. (org-mac-paste-applescript-links
  676. (if (string= select-or-flag "s")
  677. (org-as-get-selected-outlook-mail)
  678. (if (string= select-or-flag "f")
  679. (org-sh-get-flagged-outlook-mail)
  680. (error "Please select \"s\" or \"f\"")))))
  681. ;;;###autoload
  682. (defun org-mac-outlook-message-insert-selected ()
  683. "Insert a link to the messages currently selected in Microsoft Outlook.app.
  684. This will use AppleScript to get the message-id and the subject
  685. of the active mail in Microsoft Outlook.app and make a link out
  686. of it."
  687. (interactive)
  688. (insert (org-mac-outlook-message-get-links "s")))
  689. ;;;###autoload
  690. (defun org-mac-outlook-message-insert-flagged (org-buffer org-heading)
  691. "Asks for an org buffer and a heading within it, and replace message links.
  692. If heading exists, delete all mac-outlook:// links within
  693. heading's first level. If heading doesn't exist, create it at
  694. point-max. Insert list of mac-outlook:// links to flagged mail
  695. after heading."
  696. (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
  697. (with-current-buffer org-buffer
  698. (goto-char (point-min))
  699. (let ((isearch-forward t)
  700. (message-re "\\[\\[\\(mac-outlook:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
  701. (if (org-goto-local-search-headings org-heading nil t)
  702. (if (not (eobp))
  703. (progn
  704. (save-excursion
  705. (while (re-search-forward
  706. message-re (save-excursion (outline-next-heading)) t)
  707. (delete-region (match-beginning 0) (match-end 0)))
  708. (insert "\n" (org-mac-outlook-message-get-links "f")))
  709. (flush-lines "^$" (point) (outline-next-heading)))
  710. (insert "\n" (org-mac-outlook-message-get-links "f")))
  711. (goto-char (point-max))
  712. (insert "\n")
  713. (org-insert-heading nil t)
  714. (insert org-heading "\n" (org-mac-outlook-message-get-links "f"))))))
  715. ;; Handle links from Evernote.app
  716. (org-link-set-parameters "mac-evernote" :follow #'org-mac-evernote-note-open)
  717. (defun org-mac-evernote-note-open (noteid)
  718. "Open a note in Evernote"
  719. (do-applescript
  720. (concat
  721. "tell application \"" org-mac-evernote-path "\"\n"
  722. " set theNotes to get every note of every notebook where its local id is \"" (substring-no-properties noteid) "\"\n"
  723. " repeat with _note in theNotes\n"
  724. " if length of _note is not 0 then\n"
  725. " set _selectedNote to _note\n"
  726. " end if\n"
  727. " end repeat\n"
  728. " open note window with item 1 of _selectedNote\n"
  729. " activate\n"
  730. "end tell")))
  731. (defun org-as-get-selected-evernote-notes ()
  732. "AppleScript to create links to selected notes in Evernote.app."
  733. (do-applescript
  734. (concat
  735. "tell application \"" org-mac-evernote-path "\"\n"
  736. " set noteCount to count selection\n"
  737. " if (noteCount < 1) then\n"
  738. " return\n"
  739. " end if\n"
  740. " set theLinkList to {}\n"
  741. " set theSelection to selection\n"
  742. " repeat with theNote in theSelection\n"
  743. " set theTitle to title of theNote\n"
  744. " set theID to local id of theNote\n"
  745. " set theURL to \"mac-evernote:\" & theID\n"
  746. " set theLink to theURL & \"::split::\" & theTitle & \"\n\"\n"
  747. " copy theLink to end of theLinkList\n"
  748. " end repeat\n"
  749. " return theLinkList as string\n"
  750. "end tell\n")))
  751. ;;;###autoload
  752. (defun org-mac-evernote-note-insert-selected ()
  753. "Insert a link to the notes currently selected in Evernote.app.
  754. This will use AppleScript to get the note id and the title of the
  755. note(s) in Evernote.app and make a link out of it/them."
  756. (interactive)
  757. (message "Org Mac Evernote: searching notes...")
  758. (insert (org-mac-paste-applescript-links
  759. (org-as-get-selected-evernote-notes))))
  760. ;; Handle links from DEVONthink Pro Office.app
  761. (org-link-set-parameters "x-devonthink-item" :follow #'org-devonthink-item-open)
  762. (defun org-devonthink-item-open (uid)
  763. "Open UID, which is a reference to an item in DEVONthink Pro Office."
  764. (shell-command (concat "open \"x-devonthink-item:" uid "\"")))
  765. (defun org-as-get-selected-devonthink-item ()
  766. "AppleScript to create links to selected items in DEVONthink Pro Office.app."
  767. (do-applescript
  768. (concat
  769. "set theLinkList to {}\n"
  770. "tell application \"DEVONthink Pro\"\n"
  771. "set selectedRecords to selection\n"
  772. "set selectionCount to count of selectedRecords\n"
  773. "if (selectionCount < 1) then\n"
  774. "return\n"
  775. "end if\n"
  776. "repeat with theRecord in selectedRecords\n"
  777. "set theID to uuid of theRecord\n"
  778. "set theURL to \"x-devonthink-item:\" & theID\n"
  779. "set theSubject to name of theRecord\n"
  780. "set theLink to theURL & \"::split::\" & theSubject & \"\n\"\n"
  781. "copy theLink to end of theLinkList\n"
  782. "end repeat\n"
  783. "end tell\n"
  784. "return theLinkList as string"
  785. )))
  786. (defun org-mac-devonthink-get-links ()
  787. "Create links to the item(s) currently selected in DEVONthink Pro Office.
  788. This will use AppleScript to get the `uuid' and the `name' of the
  789. selected items in DEVONthink Pro Office.app and make links out of
  790. it/them. This function will push the Org-syntax text to the kill
  791. ring, and also return it."
  792. (message "Org Mac DEVONthink: looking for selected items...")
  793. (org-mac-paste-applescript-links (org-as-get-selected-devonthink-item)))
  794. ;;;###autoload
  795. (defun org-mac-devonthink-item-insert-selected ()
  796. "Insert a link to the item(s) currently selected in DEVONthink Pro Office.
  797. This will use AppleScript to get the `uuid'(s) and the name(s) of the
  798. selected items in DEVONthink Pro Office and make link(s) out of it/them."
  799. (interactive)
  800. (insert (org-mac-devonthink-get-links)))
  801. ;; Handle links from Mail.app
  802. (org-link-set-parameters "message" :follow #'org-mac-message-open)
  803. (defun org-mac-message-open (message-id)
  804. "Visit the message with MESSAGE-ID.
  805. This will use the command `open' with the message URL."
  806. (start-process (concat "open message:" message-id) nil
  807. "open" (concat "message://<" (substring message-id 2) ">")))
  808. (defun org-as-get-selected-mail ()
  809. "AppleScript to create links to selected messages in Mail.app."
  810. (do-applescript
  811. (concat
  812. "tell application \"Mail\"\n"
  813. "set theLinkList to {}\n"
  814. "set theSelection to selection\n"
  815. "repeat with theMessage in theSelection\n"
  816. "set theID to message id of theMessage\n"
  817. "set theSubject to subject of theMessage\n"
  818. "set theLink to \"message://\" & theID & \"::split::\" & theSubject\n"
  819. "if (theLinkList is not equal to {}) then\n"
  820. "set theLink to \"\n\" & theLink\n"
  821. "end if\n"
  822. "copy theLink to end of theLinkList\n"
  823. "end repeat\n"
  824. "return theLinkList as string\n"
  825. "end tell")))
  826. (defun org-as-get-flagged-mail ()
  827. "AppleScript to create links to flagged messages in Mail.app."
  828. (unless org-mac-mail-account
  829. (error "You must set org-mac-mail-account"))
  830. (do-applescript
  831. (concat
  832. ;; Get links
  833. "tell application \"Mail\"\n"
  834. "set theMailboxes to every mailbox of account \"" org-mac-mail-account "\"\n"
  835. "set theLinkList to {}\n"
  836. "repeat with aMailbox in theMailboxes\n"
  837. "set theSelection to (every message in aMailbox whose flagged status = true)\n"
  838. "repeat with theMessage in theSelection\n"
  839. "set theID to message id of theMessage\n"
  840. "set theSubject to subject of theMessage\n"
  841. "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
  842. "copy theLink to end of theLinkList\n"
  843. "end repeat\n"
  844. "end repeat\n"
  845. "return theLinkList as string\n"
  846. "end tell")))
  847. ;;;###autoload
  848. (defun org-mac-message-get-links (&optional select-or-flag)
  849. "Create links to the messages currently selected or flagged in Mail.app.
  850. This will use AppleScript to get the message-id and the subject of the
  851. messages in Mail.app and make a link out of it.
  852. When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
  853. the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
  854. The Org-syntax text will be pushed to the kill ring, and also returned."
  855. (interactive "sLink to (s)elected or (f)lagged messages: ")
  856. (setq select-or-flag (or select-or-flag "s"))
  857. (message "AppleScript: searching mailboxes...")
  858. (org-mac-paste-applescript-links
  859. (cond
  860. ((string= select-or-flag "s") (org-as-get-selected-mail))
  861. ((string= select-or-flag "f") (org-as-get-flagged-mail))
  862. (t (error "Please select \"s\" or \"f\"")))))
  863. ;;;###autoload
  864. (defun org-mac-message-insert-selected ()
  865. "Insert a link to the messages currently selected in Mail.app.
  866. This will use AppleScript to get the message-id and the subject of the
  867. active mail in Mail.app and make a link out of it."
  868. (interactive)
  869. (insert (org-mac-message-get-links "s")))
  870. ;; The following line is for backward compatibility
  871. (defalias 'org-mac-message-insert-link 'org-mac-message-insert-selected)
  872. ;;;###autoload
  873. (defun org-mac-message-insert-flagged (org-buffer org-heading)
  874. "Asks for an org buffer and a heading within it, and replace message links.
  875. If heading exists, delete all message:// links within heading's first
  876. level. If heading doesn't exist, create it at point-max. Insert
  877. list of message:// links to flagged mail after heading."
  878. (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
  879. (with-current-buffer org-buffer
  880. (goto-char (point-min))
  881. (let ((isearch-forward t)
  882. (message-re "\\[\\[\\(message:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
  883. (if (org-goto-local-search-headings org-heading nil t)
  884. (if (not (eobp))
  885. (progn
  886. (save-excursion
  887. (while (re-search-forward
  888. message-re (save-excursion (outline-next-heading)) t)
  889. (delete-region (match-beginning 0) (match-end 0)))
  890. (insert "\n" (org-mac-message-get-links "f")))
  891. (flush-lines "^$" (point) (outline-next-heading)))
  892. (insert "\n" (org-mac-message-get-links "f")))
  893. (goto-char (point-max))
  894. (insert "\n")
  895. (org-insert-heading nil t)
  896. (insert org-heading "\n" (org-mac-message-get-links "f"))))))
  897. (provide 'org-mac-link)
  898. ;;; org-mac-link.el ends here