Set pbcopy to use UTF-8 by default

Jan 05, '09 07:30:00AM

Contributed by: darick

After much frustration, I realized that the shell command pbcopy was destroying non-ASCII characters in an AppleScript that I was trying to write. Although pbcopy is an extremely convenient way to take the standard input and place it on the clipboard, I also needed access to accented characters.

Luckily, I found this blog post that explained everything. The important part was a comment there that explained how to set a default encoding by adding the following line to my .profile file:

export __CF_USER_TEXT_ENCODING=0x1F5:0x8000100:0x8000100
While this fixed the problem while working in the Terminal, it did not fix the problem within AppleScript. I did find that I could add the aforementioned export command inside the do shell script, and everything seemed to work as expected. An example is shown below.
do shell script "export __CF_USER_TEXT_ENCODING=0x1F5:0x8000100:0x8000100; cat -s " & quoted form of (fileToCopy) & " | pbcopy"
This has been working great for me, but one of the posts on the blog does claim a problem. The blog author also wrote a replacement for pbcopy and pbpaste that is also supposed to alleviate these issues. I have not yet tried it, however.

Comments (14)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20081231012753422