Skip to content

My most-useful and least-used shell script

I have a large number of small shell scripts I've either written or collected over the years. Today I had the opportunity to use my favorite one—which is rare, as I only need it a couple times a year. But when I do need it, it's a wonderful little script.

It's also a very simple-minded script, as it does just one thing: it copies my public IP address to the clipboard and shows it in a pop-up message, as seen at right. OK, so that's two things, but they're very closely related.

Clearly this isn't something I need to do often, but when I do, the script changes this…

Switch to browser, open new tab, load the DynDNS check IP page, drag mouse to select IP address, press Command-C to copy, switch back to destination app, press Command-V to paste

…into this…

Press a key combo, wait about a second, then press Command-V

This is a big timesaver, obviously, and it makes the process about as easy as it could be.

I originally wrote this up for Mac OS X Hints a few years back, but thought I'd post it here (given the changes at Macworld, I'm not sure how long the hints site may be around). I've also modified it a bit, as I no longer use growlnotify for the onscreen display of the copied IP address.

You can read the original how-to at hints, or below, where I've posted the updated version that no longer uses growlnotify.

To make this work, you'll need Terminal (included with OS X), terminal-notifier (to pop up an OS X notification panel from Terminal), and Butler or any other tool that can execute AppleScripts and assign them keyboard shortcuts.

Before you start, install the terminal-notifier add-on; I did this using the ruby version. Given ruby is built in on OS X, this should work on any Mac. Just type sudo gem install terminal-notifier and press Return. Wait for the prompt to return, then type terminal-notifier -message "My first message!" to make sure it works.

Assuming terminal-notifier works, next you need to create the script. Open Terminal, and cd into a directory where you can store scripts (I use one named bin in my home folder). Using your editor of choice, enter this text:

Save the script, then make it executable (chmod 755 copyipaddr). Then test your script (./copyipaddr if in the same directory where you saved it) to make sure it works. If it works, all you need to do is make it executable via keyboard combo. (This step is obviously optional; you could just run the Terminal command whenever you needed it, but that still requires a trip to Terminal.)

I used Butler, but you can use any app that lets you create AppleScripts and assign them keyboard shortcuts. The AppleScript is very short:

do shell script "/full/path/to/script/copyipaddr"

Obviously (I hope), replace the /full/path/to… bit with the actual path. Add a keyboard shortcut, and you're done—just press those keys when you need your IP address, and you'll both see it onscreen and have it in your clipboard, all without leaving the frontmost app.

4 thoughts on “My most-useful and least-used shell script”

  1. There are, indeed, many ways to find your public IP; I wanted a way to do it without first opening a browser or launching another app.

    -rob.

    1. Yep, your script is nifty. I found that google does it just the other day and as I used to have to search, chose a site etc the fact that google now returns it as a search result is a nice half way step if people don't want to install a script.

Comments are closed.