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

  • This bash script could be triggered directly as an individual application by creating one in the AppleScript editor. The bash part is responsible for processing input and then runs the AppleScript part using the osascript command line utility.
  • Another remark is that curl will not successfully retrieve a google search page if it does not use the –user-agent "" option. It will get a permission denied page instead.
    • Related, the URL is built to query google.co.uk. This is done in order to prevent getting a redirect page instead of the query result page.
  • The -o option to egrep is responsible for displaying only the matched text instead of the entire line and egrep is used instead of grep in order to have the advanced (add-on) regex features.

osx/growl/display_time_in_cities.txt · Last modified: 2022/04/19 08:28 by 127.0.0.1

Access website using Tor Access website using i2p Wizardry and Steamworks PGP Key


For the contact, copyright, license, warranty and privacy terms for the usage of this website please see the contact, license, privacy, copyright.