Skip to content

Instantly share code, notes, and snippets.

@genegoykhman
Last active August 25, 2016 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save genegoykhman/357bd6f64263e541c6f7 to your computer and use it in GitHub Desktop.
Save genegoykhman/357bd6f64263e541c6f7 to your computer and use it in GitHub Desktop.
My QuickCursor replacement, opening the selected text (or full document) in Emacs client for editing
#!/usr/bin/osascript
-- open-in-emacs.applescript
-- https://gist.github.com/genegoykhman/357bd6f64263e541c6f7
--
-- By Gene Goykhman, 2016
-- No warranties expressed or implied: use at your own risk.
--
-- When triggered by a system-wide hotkey, opens the selected text (if any) or the full document in your frontmost application in an Emacs client buffer. When you exit the buffer the edited text replaces the selection (or full document).
--
-- Installation
--
-- Copy script into a convenient folder (e.g. ~/bin)
-- Make it user executable with chmod u+x open-in-emacs.applescript
-- Map a global hotkey using the tool of your choice to execute this script (asynchronously if possible). I use https://pqrs.org/osx/karabiner/ but I'm sure https://www.keyboardmaestro.com/main/ would work too
-- If prompted by OS X, allow the application assistive access over your desktop session
--
-- Uses an approach described in http://apple.stackexchange.com/questions/174802/assistive-access-when-script-is-launched-by-agent/174806#174806
tell application "System Events"
set applicationName to name of first application process whose frontmost is true
tell application (applicationName)
delay 0.2
tell application "System Events"
tell application process (applicationName)
click menu item "Select All" of menu "Edit" of menu bar item "Edit" of menu bar 1
delay 0.2
click menu item "Copy" of menu "Edit" of menu bar item "Edit" of menu bar 1
end tell
set the clipboard to «class ktxt» of ((the clipboard as text) as record)
do shell script "pbpaste > /tmp/temp.emacs && /Applications/Emacs.app/Contents/MacOS/bin/emacsclient /tmp/temp.emacs && cat /tmp/temp.emacs | pbcopy && rm /tmp/temp.emacs"
end tell
end tell
end tell
tell application (applicationName) to activate
delay 0.2
tell application "System Events"
tell application process (applicationName)
click menu item "Paste" of menu "Edit" of menu bar item "Edit" of menu bar 1
end tell
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment