Programatically Clipboarding in Windows

My last article showed how to interface with the clipboard in Mac OS X. Windows 7 comes with the same feature, but instead of pbcopy it’s simply clip. Since you don’t ls, we’ll pipe the output of dir into the clipboard: dir | clip Enjoy & no more complaining that I like one platform more than the other – you know who you are!

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!