Let’s start from the top and work our way down. I need a way, outside of Emacs, to run a command to copy the selected region to the clipboard, and then use emacsclient to start a function to copy that information into the currently clocked in task.

I use Alfred to start a Workflow, as it will allow me to trigger these scripts in succession as shown in this diagram:

The trigger (in this case, just about every meta-key on a laptop), will start the first script that basically issues the Command-C to copy the selected text to the clipboard:

tell application "System Events" to keystroke "c" using command down

This works with any Mac application, including browsers.

The next script basically takes the contents of the clipboard (as HTML), render that to an org-compatible format with pandoc (which you’ll need to install), and then use emacsclient to call my org-capture routine with the “C” selection, so that the contents go directly to my clocked in task. My first attempt was a modified version from Roland Crosby:

query =$( osascript -e 'the clipboard as "HTML"' | \ perl -ne 'print chr foreach unpack("C*",pack("H*",substr($_,11,-3)))' | \ /usr/local/bin/pandoc -f html -t org | \ sed 's/"//g' | sed 's/^/ /' ) /usr/local/bin/emacsclient -c -n \ -e "(progn (org-capture-string \"${query}\" \"C\") (delete-frame))"

While the above code works well from a browser, if I copy text from something else (like Slack), the as "HTML" bit throws an error, as the clipboard contains plain text. After working on a fairly involved Perl script (that didn’t handle Unicode characters well), I ended up writing the entire part in Emacs Lisp. Now the shell script is nothing more than:

/usr/local/bin/emacsclient -c -n -e "(ha/external-capture-to-org)"

Unpacking Apple’s encoding plus dealing with both HTML and Text content was a bit more involved, but you can see the functions on Gitlab. The end result is great. After selecting some text on the homepage at orgmode.org, and clocked this header as my current task, I ended up with this getting pasted:

The stable version of Org is *9.1.13*, as of May 2018. See the [[https://orgmode.org/Changes.html][release notes]]. Get it with =M-x package-install RET org RET= (see [[https://orgmode.org/elpa.html][Org ELPA]]). Or download it as a [[https://orgmode.org/org-9.1.13.tar.gz][tar.gz]] or [[https://orgmode.org/org-9.1.13.zip][zip]] archives.

Keep in mind, that this copy/pasting business happens completely in the background while I am still surfin’ the web.