SIGPIPE 13

Clipboard Access From Shell (UTF-8)

October 11th, 2005

Update 2011-01-27: Recent versions of Mac OS X make these replacements obsolete.

Two very nice shell commands that Apple has given us are the pbcopy and pbpaste commands. These allow stdin to go to the clipboard and the clipboard to be written to stdout.

Unfortunately the commands seem to use a combination of MacRoman and question marks for non-ASCII characters, which often makes them unusable for me, since I work with non-ASCII characters.

So today I decided to write a replacement for the two commands (yes, I did also file an enhancement report). You can download them here.

There's just one source, it compiles to a command which works as pbcopy, when called under that name, otherwise pbpaste.

What I've done is place the command in ~/bin and added a symbolic link from pbpaste to pbcopy, like this:

  ln -s pbcopy ~/bin/pbpaste

And in addition ensured that my PATH contains ~/bin before anything else, i.e. by placing the following in my ~/.bash_profile (well, actually ~/.zshrc):

  export PATH="$HOME/bin:/opt/local/bin:$PATH:/Developer/Tools"

The source is included in the archive, and it's very simple. No usage instructions etc., and it links with the Application Kit, since NSPasteboard is under that and not Foundation Kit.

[by Allan Odgaard]


6 Responses to “Clipboard Access From Shell (UTF-8)”

  1. Brian Foley Says:
    October 12th, 2005 at 01:41

    Assuming your UNIX uid is 501 (hex 1f5), adding the following to your .profile makes pbcopy & pbpaste default to using UTF8.

    # Make CFString use UTF8 as default encoding
    # (Handy for pbcopy/pbpaste which uses defaultCStringEncoding)
    export __CF_USER_TEXT_ENCODING=0x1F5:0x8000100:0x8000100
    
  2. Allan Odgaard Says:
    October 12th, 2005 at 14:41

    Oh, very very nice! Thanks!

  3. Allan Odgaard Says:
    October 12th, 2005 at 14:52

    Maybe I spoke to soon.

    Seems that when the encoding is set, an exception is thrown from setValue:forKey:.

    Here's a simple example. Compile and run from shell (with encoding set) and I get:

    2005-10-12 15:48:25.730 a.out[5733] *** Uncaught exception:  Conversion to cString failed for string "test"
  4. Carl Johnson Says:
    August 28th, 2006 at 04:30

    I found that pbpaste was giving me Shift-JIS, so I just resorted to dumping it into a file and iconv, but your program is way handier. Thanks!

  5. Darick Says:
    December 31st, 2008 at 08:40

    I spent hours trying to figure out why this wouldn't work. Thanks for your help. I was able to put the export __CF_USER_TEXT_ENCODING command inside an applescript "do shell script" command and fix all my problems.

  6. Command-line Tools for the Clipboard - George V. Reilly's Technical Blog Says:
    March 31st, 2009 at 06:23

    [...] mentioned in my post on reStructuredText that I use a little command-line tool, pbcopy, to pipe the output into the clipboard. I finally found a similar tool for Linux, [...]


Leave a Reply