|
|
Copy and Paste from the Mac OS X Command Line?I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes, I'm a tech writer. What I'm wondering is if there's any way to actually copy and past into the Mac system wide copy/paste buffer directly from the command line? Now this is the kind of question I really dig. And what's great is that it turns out that yes, there is a very simple pair of utilities in Mac OS X that let you monkey with the copy / paste buffer (it's actually called the "Clipboard", though for some reason in the command line version they refer to it as the "pasteboard", which isn't something I've ever seen before in the Unix/Linux world). I don't know how this would work with multi-clipboard utilities like iClip since I don't use them, but I imagine that it'd simply affect one buffer while leaving all the others alone. If someone has one of these utilities installed, it'd be 90 seconds to test it out... Anyway, I started my quest with a very useful command: man -k paste, which revealed the following: Pod::Simple::DumpAsText(3pm) - -- dump Pod-parsing events as text
paste(1) - merge corresponding or subsequent lines of files pbcopy(1), pbpaste(1) - provide copying and pasting to the pasteboard (the Clipboard) from command line pboard(8) - pasteboard server text(ntcl), tk_textCopy(ntcl), tk_textCut(ntcl), tk_textPaste(ntcl) - Create and manipulate text widgets XStoreBytes(3), XStoreBuffer(3), XFetchBytes(3), XFetchBuffer(3), XRotateBuffers(3) - manipulate cut and paste buffers If you want to work on the command line, ignore anything from section 3 of the manual: we're looking for "(1)" here, and that shows the commands we seek: pbcopy and pbpaste. They turn out to be really easy to work with. Want to push the output of an "ls -l" into the Clipboard? Here's how: $ ls -l | pbcopy
There's no output. So how do you know it worked? Let's just open up a TextEdit window and use Edit --> Paste to see what happen: ![]() Very cool. To extract something from the Clipboard and have it output within the world of the command line, use pbpaste. Here's a fun command pipe that counts how many letters are in the paste buffer: $ pbpaste | wc -c
252 Easy, eh? Let's test it... $ who am i | pbcopy
$ pbpaste | wc -c 32 $ pbpaste taylor ttys000 Dec 28 17:24 $ So there you go. Turns out to be quite easy to copy and paste between Mac OS X GUI apps and the command line. Now, what to do with it... Hmmm.... Here's one thing I did: I have a script that automatically resizes images for me, spitting out the appropriate HTML for image inclusion in blog posts (like this one). Until I wrote this article and learned more about pbcopy and pbpaste, I'd just manually copy and paste the resultant HTML code. Instead, here's what I did to tweak my 'while condition; do' loop: while complicated condition do
...lots of code... done | tee $tempfile cat $tempfile | pbcopy rm -f $tempfile See what's happened here? By using the handy "tee" utility, I still see the output on the screen, but a copy is saved in the tempfile defined by the "tempfile" variable, then sent to the "pbcopy" utility and removed. The result: the output looks exactly the same, but it's auto-copied into the Clipboard. Neat.
More Useful Shell Script Programming Articles:
✔ Secretly capture screenshots on my Mac?
When I used to work on a Linux system, there was a utility we had that would let me take screen captures every...
✔ Parsing "id" strings in a Shell Script?Hello Dave. I need a Bash shell script that creates a directories with the group names automatically when user logs in to the...
✔ Copy and Paste from the Mac OS X Command Line?I am constantly running commands in Terminal.app on my MacBook and then copying and pasting the results into email messages or documents. Yes,...
✔ Script to test line lengths for Twitter compatibility?I've been tasked with writing a series of tweets for a Black Friday marketing campaign and am finding it a bit tricky because...
✔ Shell script to convert lowercase to title case?As part of a project I'm working on, I find myself deep in a Linux shell script, needing to have a subroutine that...
Let's stay in touch!
Sign up for my weekly AskDaveTaylor Newsletter and you'll receive even more tech and gadget help
right to your inbox, along with exclusive news and industry updates. It's good stuff. I promise!
Categorized:
Mac OS X Help
,
Shell Script Programming
,
Unix and Linux Help
(Article 10194,
Written by Dave Taylor)
Tagged: cli, clipboard, command line, copy paste buffer, linux programming, mac os x, mac programming, scripting, shell script programming Previous: How do I force a Windows Update in Windows 7? Next: Remove a favorite from my iPhone "favorites" list? Reader Comments To Date: 1
I do have a comment, now that you mention it!Check This Out Too... |
Recent Entries
Look for Answers
Recommended
All Our Categories
Apple iPad Help
Articles and Reviews Auctions and Online Shopping Blogs and Blogging Building Web Site Traffic Business and Management Computer and Internet Basics d) None of the Above Facebook Help Google Gmail Help Google Plus Help HTML, JavaScript and Web Site Programming Industry News and Trade Shows iPhone and Cell Phone Help iPod, Sony PSP and MP3 Player Help Kindle Fire Help Mac OS X Help Pay Per Click (PPC) Advertising Pinterest Help Search Engine Optimization (SEO) Shell Script Programming Tech Support Video Help The Writing Business Twitter, LinkedIn and Social Network Help Unix and Linux Help Video Game Tips and Help Windows PC Help Find Me on Google+ ADT on G+ |
Thanks for your post. I want to make a funky script to help me use Growly Notes better.
I want to be able to copy the NAME of a file to the clipboard such that when I paste it into Notes- it shows up as an icon- JUST like it does if I use the GUI for the entire thing sequence.
When I right-click and copy a file icon- pbpaste just shows the file path text... but there must be other attributes b/c when I send that same exact text to the clip using pbcopy--- it only pastes the string into Notes instead of the little icon... any ideas?