Mac OS X

Shell to Clipboard & Back Again

Many times I want to send data straight into the clipboard from a command or script and I don’t really want to have to mess with manually lasso’ing text, using Command-C, etc. What can I say, I’m kinda’ lazy (or working too much, not really sure which). Apple has been kind enough to supply us with a pair of really useful tools for interfacing with the clipboard (er, pasteboard) in Mac OS X: pbcopy and pbpaste. To use pbcopy, which copies text into the pasteboard, simply pipe data to it. For example, if you want to send a directory listing that you run in a script into the pasteboard, you could do an ls and then pipe the data to pbcopy as follows:

ls | pbcopy

At this point, you could theoretically summon up an AppleScript that opens up Word and pastes the data in (although suddenly variables might be smarter). If you are bringing data into the shell environment from another program then simply type pbpaste at the command prompt and see the output.

Nice and easy to use: pbcopy and pbpaste – hope you enjoy them!