Clip Text File: more surprisingly handy dumb tricks

[Tweet : ADN : nvALT]

As you may know, I store a lot of information in plain text files. Among the things I enjoy about this has always been that I can grab the contents of any file straight to the clipboard — without opening it — when I’m on the command line. I just use cat FILENAME|pbcopy and boom, done. I wrote a function a long time ago to make this a little safer and more convenient. It avoids clipping non-text files accidentally and provides a little feedback.

# .bash_profile
function clip() {
	type=`file "$1"|grep -c text`
	if [ $type -gt 0 ]; then
		cat "$@"|pbcopy
		echo "Contents of $1 are in the clipboard."
	else
		echo "File \"$1\" is not plain text."
	fi
}

So this morning I finally thought to bring that convenience to Finder. With some minor modifications I made a Service that makes it easy to grab file contents with a right click from Finder or any application that exposes files and folders. It will actually run on any type of file, but it uses an almost-foolproof method of figuring out if it’s plain text or not before it tries to dump it to your clipboard. In case you’re wondering, it runs the aged Unix tool, file, and looks for the word “text” in the output. That word shows up correctly for every file I’ve tested on, even files the system considers executables and assumes are binary.

If you’re scratching your head wondering why I’d want to do this… you probably don’t have a need for it. For me, though, if I already know a file’s contents and just need to access a snippet or a saved clipping again, it’s time-consuming to open the file, select the text, copy, close. I much prefer the simple clip(), and now this System Service.

You can download the Service below. If you have growlnotify for Growl installed (or a pseudo-Growl), you’ll get response messages in Growl or Notification Center. Either way, it gives you a “Glass” or “Basso” sound on success or error, respectively.

The source for the script is on Github, and it works just fine as a shell script, too. I just don’t need Growl notifications for a command that could just output to STDOUT…

Updated 1-16-2013 to handle UTF-8 characters and not return an error if growlnotify is not installed.

Clip Text File System Service v1.1

Clip the contents of any text file to the clipboard from Finder

Updated Wed Jan 16 2013.

More info…