Skip to main content

Capturing text from any Mac Application into Emacs org-mode with Automator and org-protocol

After decades of using vi and Vim I switched to Spacemacs which is an amazing vi keystroke emulation layer running on Emacs and configured with an amazing set of preconfigured layers for different tasks. I decided to give it a try after seeing Org-Mode in action and seeing it was a nice taking system with integrations with almost anything imaginable. A few weeks ago I found out about org-protocol and followed this post by Jethro on using a bookmarket to capture from the Web to Emacs. 

This page assumes a few things

  • You use Emacs on a Mac
  • You are using org and understand how to use capture and capture templates.
  • You need to yank text from random apps into Emacs
You don't need to be using Spacemacs and this should work with any install of Emacs that supports org, org-capture and org-protocol.

Creating Automator Action

Start Automator. It's this icon. I'm guessing many people have had this for years and have never used it. 

Open it and pick Quick Action

Grab the following text:

data="$@"
encoded=$(python -c "import sys, urllib; print urllib.quote(' '.join(sys.argv[1:]), safe='')" "${data[@]}")
emacsclient "org-protocol://capture?template=g&body=$encoded"

 I borrowed some of it from this stack overflow question.

You want to head over to "Utilities" and drag over the "Run Shell Script" block. It should look like this:

Save it. You can confirm by checking the installed extensions in System Preferences. I named my test copy "test foo" and then created AddToEmacs as the permanent name.

Verify Action


Configure Emacs

In Emacs, open your init file. If you aren't using org-protocol, you'll need to install it with M-x package-install and add this:

(require 'org-protocol )

Add this to your org-capture-templates section. I am using g for "Grab"

("g" "Grab" entry (file+headline "~/Documents/inbox.org" "Grab" )
"* %? \n%i\n" :prepend t :created t )

Capturing Text

Highlight text, right click and pick "Services" and then what ever you named it. I called it "test foo". 



Emacs will open in the background with the text sitting in the capture frame.

You should see something like this:



Comments

Popular posts from this blog

I can't vote for that, no can do

Let's see who you really are