Zipcode Lookup

Another gem from Apple. OS X includes a text file for US ZIP codes. This script provides you a quick way to search it by either the ZIP code itself or by city. It is not ZIP+4 and may or may not be out of date–I haven’t checked it against anything from the USPS. Anyway, here 'tis:

OS version: OS X

property last_lookup : ""

set message_add to ""
repeat
	set the_lookup to text returned of (display dialog message_add & "Enter a City or a ZIP Code to lookup:" default answer last_lookup buttons {"Cancel", "Lookup"} default button 2 with icon 1)
	set last_lookup to the_lookup
	set the_zip to ""
	try
		set the_zip to do shell script "grep '" & the_lookup & "' /usr/share/misc/zipcodes"
	end try
	if the_zip = "" then
		set message_add to (the_lookup & " was not found. Please try again." & return & return)
	else
		set the_zip to my make_zip_list(the_zip)
		if (count of the_zip) = 1 then
			set the_zip to (item 1 of the_zip)
			set the_button to button returned of (display dialog the_zip buttons {"Quit", "Copy to Clipboard", "Lookup Another"} default button 3 with icon 1)
			if the_button = "Quit" then
				beep 2
				return
			else if the_button = "Copy to Clipboard" then
				set the clipboard to the_zip
				beep 2
				return
			end if
		else
			set the_zip to (choose from list the_zip with prompt (the_lookup & " produced multiple listings. Please choose from the following list to copy it to the clipboard:") default items {item 1 of the_zip} OK button name "Copy" cancel button name "Quit") as string
			if the_zip = "false" then
				return
			else
				set the clipboard to the_zip
				beep 2
				return
			end if
		end if
	end if
end repeat

on make_zip_list(the_zip)
	set AppleScript's text item delimiters to {":"}
	set the_zip to (every text item of the_zip) as list
	set AppleScript's text item delimiters to {": "}
	set the_zip to the_zip as string
	set AppleScript's text item delimiters to {ASCII character 13}
	set the_zip to (every text item of the_zip) as list
	set AppleScript's text item delimiters to {""}
	return the_zip
end make_zip_list