Posts RSS Comments RSS 37 Posts and 56 Comments till now

Archive for August, 2007

Check your current IP address with AppleScript

This script will get your current “real world” IP address. It’s very useful when you’re using a router giving out internal addresses and you need to know the address the world is seeing for you. It will also store that IP address so you can have this script run on a schedule and only report if your IP address has changed.

Click here to download the script

property theIP : ""
set TheResult to (do shell script "curl -f http://checkip.dyndns.org")
set Olddelim to AppleScript's text item delimiters
try
	set AppleScript's text item delimiters to "Current IP Address: "
	set LongIP to item 2 of every text item of TheResult as text
	set AppleScript's text item delimiters to ""
	set stopPoint to (offset of "< " in LongIP)
	set myip to characters 1 through (stopPoint - 1) of LongIP as text
        if theIP is "" then
		set theIP to myip
		display dialog "Your IP is : " & myip
	end if
	if myip is not equal to theIP then
		display dialog "Your IP is : " & myip
		set theIP to myip
	end if
	set AppleScript's text item delimiters to Olddelim
on error
	set AppleScript's text item delimiters to Olddelim
end try

Delete printers using AppleScript

I recently had a request for a script that would delete printers from a lab build. The user wanted to make one system build for all of his labs with all of the printers installed. Then, just run a script to select the correct printer for that lab and delete the others. Here’s the script I came up with. With this script you can choose more then one printer if that is more appropriate.


--get the name of every printer
tell application "Printer Setup Utility"
set theList to the name of every printer as list
end tell

--present the list to choose the printer from
choose from list theList with title "Lab Printer" with prompt "Pick the correct printer for this lab" with multiple selections allowed
set theItem to the result as list

--loop through the list and delete any printer not in the list theItem
repeat with x from 1 to the count of theList
if theItem does not contain (item x of theList) then
do shell script "lpadmin -x " & (item x of theList)
end if
end repeat

Pinging a server using AppleScript

I’ve recently had a machine that suddenly seems to lose it’s network connection. I can’t figure out why but it always comes back after a reboot. The big problem with this is it’s my backup server and having that off the network is not good. So, I came up with an AppleScript that runs each hour to see if we’re still talking to the world. If not then I reboot the machine.

Here’s the basic script:


try
set thePing to do shell script "/sbin/ping -o -c 5 www.google.com"
on error
delay 60
try
set thePing to do shell script "/sbin/ping -o -c 5 www.google.com"
on error
do shell script "/sbin/shutdown -r now"
end try
end try

Breaking down the script

set thePing to do shell script "/sbin/ping -o -c 5 www.google.com"

This executes the command line version of ping to see if we’re on the network. The “-o” means quit pinging as soon as we get a reply. Don’t want to flood Google with pings! The “-c 5″ says only send 5 pings at the most.

If there is no response for what ever reason it throws an error. So the “on error” part of the script catches that and tells the script to delay itself for 60 seconds and then continue.

After 60 seconds we try again. If we get an error again the we tell the machine to restart itself using “shutdown -r”.

I run this script as a System cron job, running once an hour. By running as system I don’t have to include any passwords and if it needs to shutdown most likely no active processes can get in the way. I use Cronnix to set up my cron jobs. Saving the job as “ping.scpt” the cron job would look like this:

/usr/bin/osascript /path/to/ping.scpt