Copying the GNU screen buffer to the Leopard clipboard

Quite a while back I wrote an article about how I use GNU screen’s scrollback buffer. In the article I detailed how to copy the scrollback buffer into the Mac clipboard, which made it easy to copy data to other Mac applications. Unfortunately, I recently moved to Leopard and discovered this no longer works. In this short article I will explain an updated procedure that works for Leopard.

The Problem

In Leopard, both pbcopy and pbpaste appear to crash when run from GNU screen. I did some basic investigation and found that pbcopy and pbpaste work by talking to a daemon called pboard, which is managed by launchd in Leopard. Any subprocess launched by GNU screen seems to be unable to talk to pboard, though pbcopy does work with a pboard instance launched from within screen. Unfortunately this defeats the purpose because the pboard instance launched in screen does not work with the OS.

I was not successful in uncovering the reason that pbcopy in screen can’t talk to the pboard process started by launchd. If anyone knows why this is the case I would be very, very happy to hear about it.

My Solution

I realized that if I could somehow get pbcopy running in screen to talk to the pboard running outside of screen then I could write to the Leopard clipboard from screen. The solution I came up with is to start a process outside of screen that listens on a UNIX domain socket. When it receives a message it copies it to the Leopard clipboard using pbcopy. Within screen I use a client that pushes screen’s /tmp/screen-exchange file over the UNIX domain socket.

Here are the scripts:

You will need to chmod 755 pboard_server pboard_client after downloading them. To use the new pboard client, add the following to your .screenrc:

bind b eval "writebuf" "exec ruby pboard_client"

If you used my previous tutorial, the line above should replace the old bind b ... line.

Now when you open a terminal (outside of screen), run pboard_server &. This will start the process that listens for requests over the socket. You only need to do this once when you login, and you could automate this using launchd or some shell scripting from your .bashrc.

You should now be able to start screen and copy to the clipboard!

4 Comments

  1. Andrew Wason Says:

    What a pain that pbcopy fails.

    Using osascript works, nasty but doesn’t require running a separate server:

    bind b eval “writebuf /tmp/screen-pbcopy” “exec /usr/bin/osascript -e ‘tell application \”System Events\”‘ -e ’set the clipboard to (read posix file \”/tmp/screen-pbcopy\” as text)’ -e ‘end tell’”

  2. Chris Pettitt Says:

    Andrew,

    Thanks for the script! Your solution works great on my Leopard system and it is simpler than managing a separate process.

  3. Andrew Wason Says:

    I also discovered that MacPorts gnu screen does not have this issue.

    Also if you use TextMate, the command line “mate” command fails when used within screen, but works with MacPorts screen - it’s probably a similar issue to pbcopy.

    [aw@blackflag ~] $ mate .screenrc
    mate: failed to establish connection with TextMate.

    Another problem with Leopard screen that MacPorts screen also solves is Leopard screen resets PATH (but not other env vars) to system default.

    See this for details:
    http://www.pervasivecode.com/blog/2007/12/27/leopard-upgrade-report-many-new-features-many-new-bugs/

  4. Alan Pinstein Says:

    Awesome thread, guys. I have been having this problem on 2 machines and didn’t even know where to start. The MacPorts screen fixed pbpaste and scrollback issues. Thanks!

Leave a Reply