Last active
November 17, 2020 15:52
-
-
Save takaxp/14fa032808424c391c77e5ec68947331 to your computer and use it in GitHub Desktop.
Additional descriptor to grab a link to a paper stored in Papers.app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(with-eval-after-load "org-mac-link" | |
(defcustom org-mac-grab-Papers-app-p t | |
"Add menu option [P]apers to grab links from the Papers.app." | |
:tag "Grab Papers.app links" | |
:group 'org-mac-link | |
:type 'boolean) | |
(defun org-mac-papers-insert-front-most-paper-link () | |
(interactive) | |
(let ((result (org-mac-papers-get-frontmost-paper-link))) | |
(if result | |
(insert result) | |
(message "Please open Paers.app and select a paper.")))) | |
(defun org-mac-papers-get-frontmost-paper-link () | |
(interactive) | |
(message "Applescript: Getting Papers link...") | |
(let ((result (org-as-mac-papers-get-paper-link))) | |
(if (or (eq result nil) (string= result "")) | |
nil | |
(org-mac-paste-applescript-links result)))) | |
(defun org-as-mac-papers-get-paper-link () | |
(do-applescript | |
(concat | |
"if application \"Papers\" is running then\n" | |
" tell application \"Papers\" to activate\n" | |
" delay 0.3\n" | |
" set the clipboard to \"\"\n" | |
" tell application \"System Events\" to tell process \"Papers\"\n" | |
" keystroke \"l\" using {command down, shift down}\n" | |
" end tell\n" | |
" delay 0.2\n" | |
" set aLink to the clipboard\n" | |
" tell application \"System Events\" to tell process \"Papers\"\n" | |
" keystroke \"c\" using {command down, shift down\}\n" | |
" end tell\n" | |
" delay 0.2\n" | |
" set aName to the clipboard\n" | |
" tell application \"Emacs\" to activate\n" | |
" return (get aLink) & \"::split::\" & (get aName) as string\n" | |
"else\n" | |
" return\n" | |
"end if\n"))) | |
(defcustom org-mac-link-descriptors | |
`(("F" "inder" org-mac-finder-insert-selected ,org-mac-grab-Finder-app-p) | |
("m" "ail" org-mac-message-insert-selected ,org-mac-grab-Mail-app-p) | |
("d" "EVONthink Pro Office" org-mac-devonthink-item-insert-selected | |
,org-mac-grab-devonthink-app-p) | |
("o" "utlook" org-mac-outlook-message-insert-selected ,org-mac-grab-Outlook-app-p) | |
("a" "ddressbook" org-mac-addressbook-insert-selected ,org-mac-grab-Addressbook-app-p) | |
("s" "afari" org-mac-safari-insert-frontmost-url ,org-mac-grab-Safari-app-p) | |
("f" "irefox" org-mac-firefox-insert-frontmost-url ,org-mac-grab-Firefox-app-p) | |
("v" "imperator" org-mac-vimperator-insert-frontmost-url ,org-mac-grab-Firefox+Vimperator-p) | |
("c" "hrome" org-mac-chrome-insert-frontmost-url ,org-mac-grab-Chrome-app-p) | |
("e" "evernote" org-mac-evernote-note-insert-selected ,org-mac-grab-Evernote-app-p) | |
("t" "ogether" org-mac-together-insert-selected ,org-mac-grab-Together-app-p) | |
("S" "kim" org-mac-skim-insert-page ,org-mac-grab-Skim-app-p) | |
("A" "crobat" org-mac-acrobat-insert-page ,org-mac-grab-Acrobat-app-p)) | |
"Descriptors to select an action." | |
:tag "A list of descriptors" | |
:group 'org-mac-link' | |
:type 'symbol) | |
(add-to-list 'org-mac-link-descriptors | |
`("P" "apers" org-mac-papers-insert-front-most-paper-link ,org-mac-grab-Papers-app-p) t) | |
(defun org-mac-grab-link () | |
"Prompt for an application to grab a link from. | |
When done, go grab the link, and insert it at point." | |
(interactive) | |
(let* ((descriptors org-mac-link-descriptors) | |
(menu-string (make-string 0 ?x)) | |
input) | |
;; Create the menu string for the keymap | |
(mapc (lambda (descriptor) | |
(when (elt descriptor 3) | |
(setf menu-string (concat menu-string | |
"[" (elt descriptor 0) "]" | |
(elt descriptor 1) " ")))) | |
descriptors) | |
(setf (elt menu-string (- (length menu-string) 1)) ?:) | |
;; Prompt the user, and grab the link | |
(message menu-string) | |
(setq input (read-char-exclusive)) | |
(mapc (lambda (descriptor) | |
(let ((key (elt (elt descriptor 0) 0)) | |
(active (elt descriptor 3)) | |
(grab-function (elt descriptor 2))) | |
(when (and active (eq input key)) | |
(call-interactively grab-function)))) | |
descriptors))) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment