Copy public IP address to clipboard via keyboard shortcut
Jan 20, '11 07:30:00AM • Contributed by: robg
Jan 20, '11 07:30:00AM • Contributed by: robg
A friend asked me for a way to put his current public IP address (which changes regularly, it seems) on the clipboard via a keyboard shortcut, and to alert him as to what value was copied. Using a combination of a shell script, Butler, and Growl (specifically, the shell command growlnotify), I was able to put together a solution for him. Note that Butler (disclaimer: I'm part of Many Tricks, Butler's developers) can be replaced by any program that lets you execute shell scripts via a hot key. You could also put the shell script in Automator, and go that route ... but I digress.
Here's the script:
I made this executable and saved it as copyip. The MYIP line was culled from some combination of old hints and comments here; I wasn't able to figure out exactly where I pulled it from, though. It basically uses curl to grab the "What's my IP address?" page from dyndns.org, then uses sed to process out the actual IP number. The second line puts the IP address onto the clipboard, courtesy of pbcopy. Finally, the last line calls growlnotify to display an onscreen notification; the full path was used as I found it required for the next bit).
The last step was to assign this script to a hot key. To do that, I used Butler's ability to run AppleScript code. From the plus-sign icon at the bottom of the Butler window, I chose Smart Item » AppleScript » AppleScript. In the Source Code section of the new Smart Item, I put this:
[crarko adds: I haven't tested this one.]
Here's the script:
#!/bin/bash MYIP=`curl -s 'http://checkip.dyndns.org' | sed 's/.*Current IP Address: \([0-9\.\.]*\).*/\1/g'` echo $MYIP | pbcopy /usr/local/bin/growlnotify 'IP address copied' -m "Address was: $MYIP"
The last step was to assign this script to a hot key. To do that, I used Butler's ability to run AppleScript code. From the plus-sign icon at the bottom of the Butler window, I chose Smart Item » AppleScript » AppleScript. In the Source Code section of the new Smart Item, I put this:
do shell script /path/to/copyipIn the Triggers tab, I assigned a keyboard shortcut, and I was done. My friend can now have his public IP placed on the clipboard with a simple keyboard shortcut, and get a visual confirmation that it was done correctly. Note that there's no error handling of any sort in my script; if it fails, it's going to fail ugly. You have been warned.
[crarko adds: I haven't tested this one.]
•
[7,386 views]
