Table of Contents

Shortnote

The following script is an elegant way to grab the time for any city in the world and display it using the growl notification system.

Example

Here is a result of what it would look like for New Jersey.

Input:

./time.sh New Jersey

Output:

Code

time.sh
#!/bin/bash
###########################################################################
##  Copyright (C) Wizardry and Steamworks 2011 - License: GNU GPLv3      ##
##  Please see: http://www.gnu.org/licenses/gpl.html for legal details,  ##
##  rights of fair usage, the disclaimer and warranty conditions.        ##
###########################################################################
 
# Check that we have at least one argument.
if [ $# -eq 0 ]; then
  echo "Usage: `basename $0` <CITY>"
  exit 1
fi
 
# Read in the city from command line.
for arg in "$@"; do
  QUERY_CITY=$QUERY_CITY"+"$arg
  CITY=$CITY" "$arg
done
 
# Fetch from Google.
RESULT=`curl --user-agent "" "http://www.google.co.uk/search?q=time+now+in$QUERY_CITY" -s | egrep -o "<b>[0-9]+:[0-9]{2,}<\/b>"`
# Clean the bold anchors.
RESULT=`echo $RESULT | sed 's/<b>//g' | sed 's/<\/b>//g'`
 
# Make growl display the result.
osascript<<END
tell application "System Events"
	set isRunning to (count of (every process whose bundle identifier is "com.Growl.GrowlHelperApp")) > 0
end tell
 
if isRunning then
	tell application "GrowlHelperApp"
	  -- The following line was used when creating a standalone script.
		-- set the googletime to do shell script "curl --user-agent \"\" \"http://www.google.co.uk/search?q=time+now+in+new+jersey\" -s | egrep -o \"[0-9]+:[0-9]{2,}\""
		set the allNotificationsList to {"GoogleTime"}
		set the enabledNotificationsList to {"GoogleTime"}
		register as application "GoogleTime" all notifications allNotificationsList default notifications enabledNotificationsList icon of application "GrowlHelperApp"
 
		-- Send the notification...
		notify with name "GoogleTime" title $(echo "\""$CITY"\"") description $(echo "\""$RESULT"\"") application name "GoogleTime"
	end tell
end if
END

Notes