Cross Platform System-wide Org-Capture

Filed in BSD, Emacs, F/OSS, GNU/Linux, Org-Mode, OS X, Unix, WindowsTags: , , , , , ,

Here’s how I leverage Emacs org-capture from wherever I am in Windows, OS X, and other Unix and Unix-like operating systems.

I use F9 as my global capture key. It will either switch to or launch Emacs and then send [Ctrl]-c c to trigger org-capture. This way, no matter what I’m working on I’m one keystroke away.

Windows

Install AutoHotKey and add the following to your autohotkey.ahk script:

 

; org-capture
~F9::
If WinExist("ahk_class Emacs")
{
WinActivate
Send ^{c}
Send {c}
}
else
{
Run "runemacs.exe"
WinWaitActive, emacs
Send ^{c}
Send {c}
}
return
;

 

Save the file. Reload the script (right click on the notification icon and select “Reload This Script”). You can find the original version of this on my personal blog from 2011.

Gnu/Linux & BSD & Unix

Install autokey-gtk or autokey-qt depending on your desktop environment. In the main window create a new script and paste the following in:

 

import subprocess
command = 'wmctrl -l'
output = system.exec_command(command, getOutput=True)

if "emacs" in output:
    window.activate("emacs", switchDesktop=True)

else:
    subprocess.Popen("emacs")
    window.wait_for_exist("emacs")
    time.sleep(10)

keyboard.send_keys("<ctrl>+c")
keyboard.send_keys("c")

 

Assign F9 to the hotkey. Click Save. Thanks to the autokey-users group for their help troubleshooting my typos!

OS X

There are two ways I’ve accomplished this. The first is to use Keyboard Maestro if you already have it. Create a new macro called Org-Capture that triggers on F9. Have it Activate Emacs, simulate keystroke Control-C, and simulate keystroke C.

If you don’t have Keyboard Maestro, you can use the following Applescript in Automator:

 

on run {input, parameters}

tell application "Emacs"
activate
tell application "System Events"
keystroke "c" using {control down}
keystroke "c"
end tell
end tell

return input
end run

 

Save it as a service, and then in the Keyboard Shortcuts panel assign the service to F9.

Let me know how this works for you. Comments, suggestions, and recommendations are welcome as always.

Leave a Reply

%d bloggers like this: