about photos bookshelf portfolio blog home
Begin main content

Automatic copy from X11.app to MacOS Clipboard

Well, from emacs under X11 anyway...

This has bugged me for a long lONG time. I have been doing a bit of unix coding on my laptop this weekend and it finally got to me. There MUST be a way I thought.

A lot of people suggest autocutsel but all that does is synchronise the X11 CLIPBOARD buffer (or PRIMARY buffer) with the selection buffer. I'm sure this solves issues with some X11 apps that only use one or the other and X11.app may get confused. But that's not what I'm talking about.

I'm talking about:

  1. make a selection in emacs, yank a line, do whatever you want that "copies" a piece of text to the "clipboard" (for suitably vague meanings of those terms)
  2. switch to a MacOS app
  3. paste in the text copied in step 1.
So it seems from a LOT of googling that noone has an answer to that short of hacking X11.app (which I think is open source so that's possible - if not one could hack XDarwin instead). I don't have that sort of time however. I didn't find any general solution, but I just knew I'd be able to hack pbcopy into some sort of emacs clipboard handler. Sure enough, the workld's most extensible editor obliged! If you apply the following prodding to your .emacs file, you will be in X11.app auto copy-paste nirvana :)
(if (eq window-system 'x)
	(progn
	  (defun paste-to-osx (text &optional push)
		(progn
		  (let ((process-connection-type nil)) ; use pipe
			(let ((proc (start-process "pbcopy" "*Messages*" "pbcopy")))
			  (process-send-string proc text)
			  (process-send-eof proc)))))

	  (setq interprogram-cut-function 'paste-to-osx)))
Update: After reviewing this entry on my front page it occurred to me how unlikely it is that anyone has ever hacked up a lisp code fragment directly after some visual basic! At least the foray into a little visual basic hasn't rotted my coding brain :)

03:26 AM, 30 Oct 2006 by Mark Aufflick Permalink

Add comment