Convert Clipboard contents to plain text

Jan 17, '12 07:30:00AM

Contributed by: benwiggy

There was a recent article on the Macworld main site about apps you can buy that will strip formatting from text on the clipboard, leaving you with plain text that you can paste into documents without it having the wrong font, etc.

Although some of them do this automatically, I thought it's a bit much to pay for such a simple utility that can be done in one line on the CLI.

So here's a shell script that will convert the contents of the clipboard to plain text.

#!/bin/bash
#
# Convert contents of clipboard to plain text.

pbpaste | textutil -convert txt -stdin -stdout -encoding 30 | pbcopy

exit 0
The encoding option specifies MacRoman (30). I find that the default, UTF-8 doesn't always cope well with things like curly quotes.

If you have the AppleScript script menulet active, you can store the script in /Library/Scripts, and you can select it from the menu. It will run without opening the Terminal, as per this hint.

[crarko adds: This seems to work as described; I had to run the script twice before it did though.]

Comments (15)


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