(*
Google Weather Handler Library
Copyright © 2012 hXm's Project
Feedback to hxm@librepensamiento.es
This is a script for GeekTool.
GeekTool lets you display various kinds of information
on your desktop using floating windows.
http://itunes.apple.com/es/app/geektool/id456877552?mt=12
TODO:
Use custom weather icons, not only google's ones.
Geolocation feature. Probably not.
Status bar icon.
May change the wind's direction letter for a cool UTF-8 arrow.
Let various locations at same time.
FIX:
Remove the temp file using AS.
Next errors that will appear.
*)
on WriteResult(gklet, info)
-- I felt better calling the GeekTool app itself than storing temporary files to load the result
tell application "GeekTool Helper"
set googleweatherglets to every geeklet whose name is gklet
repeat with glet in googleweatherglets
tell glet to set command to "printf " & "\"" & info & "\""
end repeat
end tell
end WriteResult
on ShowImage(gklet, URLname)
-- Thank you, GeekTool. I'm glad you can load images directly from URL
set currentTheme to readFile("~/Library/Scripts/hXmWeather/iconSet.txt")
if currentTheme is "Classic" then
set URLname to "http://www.google.com" & URLname
else
set userName to (get short user name of (system info))
set URLname to "file:///Users/" & userName & "/Library/Scripts/hXmWeather/iconSet/" & currentTheme & URLname
end if
tell application "GeekTool Helper"
set googleweatherglets to every geeklet whose name is gklet
repeat with glet in googleweatherglets
tell glet to set image url to URLname
end repeat
end tell
end ShowImage
on Split(theString, theDelimiter)
set AppleScript's text item delimiters to theDelimiter
set theArray to every text item of theString
return last item of theArray
end Split
on readFile(unixPath)
return (do shell script "cat " & unixPath)
end readFile
on charReplace(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end charReplace
on unitConvert(FromThis, ToThis, ThisDegree)
if ToThis is "Automatic" then
return ThisDegree
end if
if FromThis is "SI" and ToThis is "Fahrenheit" then
return round ((ThisDegree * 1.8) + 32)
end if
if FromThis is "SI" and ToThis is "Celsius" then
return ThisDegree
end if
if FromThis is "US" and ToThis is "Fahrenheit" then
return ThisDegree
end if
if FromThis is "US" and ToThis is "Celsius" then
return round ((ThisDegree - 32) / 1.8)
end if
end unitConvert
on run
-- Get the local system language and split it in this way:
-- en_US -> en
set locale to user locale of (get system info)
set locale to my Split(locale, "_")
-- Read the location stored in main.scpt -> Install()
set location to (my readFile("~/Library/Scripts/hXmWeather/hXmWeather.txt"))
set location to (my charReplace(location, " ", "+"))
-- Let's download the XML file from google weather api
-- and convert it to UTF-8 encoding system or XML file library would fail.
set weatherquery to do shell script "[COLOR="Red"][SIZE="3"]***[/SIZE][/COLOR]/usr/bin/curl -s 'http://www.google.com/ig/api?weather=" & location & "&hl=" & locale & "' | iconv -f ISO-8859-1 -t UTF-8 > /tmp/WeatherGoogleApi.xml"
tell application "System Events"
-- Load the XML file in a variable, then parse all those tags from it
set file1 to XML file "/tmp/WeatherGoogleApi.xml"
set useDegree to (my readFile("~/Library/Scripts/hXmWeather/degree.txt"))
if useDegree is not "Automatic" and useDegree is not "Celsius" and useDegree is not "Fahrenheit" then
set useDegree to "Automatic"
end if
tell file1
tell XML element "xml_api_reply"
tell XML element "weather"
tell XML element "forecast_information"
tell XML element "unit_system"
set realUnit to get value of XML attribute "data"
if useDegree is "Automatic" then
set unit to get value of XML attribute "data"
else if useDegree is "Celsius" then
set unit to "SI"
else if useDegree is "Fahrenheit" then
set unit to "US"
end if
end tell
tell XML element "city"
set city to get value of XML attribute "data"
my WriteResult("GoogleWeather_City", city)
end tell
end tell
-- Loop the forecast tags to show all of them
set forecasts to (every XML element whose name is "forecast_conditions")
set incr to 1
repeat with I in forecasts
tell I
tell XML element "day_of_week"
set dayOfWeek to get value of XML attribute "data"
my WriteResult("GoogleWeather_Info_" & incr, dayOfWeek)
end tell
tell XML element "icon"
set icon to get value of XML attribute "data"
my ShowImage("GoogleWeather_Icon_" & incr, icon)
end tell
tell XML element "low"
set ltemp to get value of XML attribute "data"
my WriteResult("GoogleWeather_LTemp_" & incr, my unitConvert(realUnit, useDegree, ltemp) & "º")
end tell
tell XML element "high"
set ltemp to get value of XML attribute "data"
my WriteResult("GoogleWeather_HTemp_" & incr, my unitConvert(realUnit, useDegree, ltemp) & "º")
end tell
end tell
set incr to incr + 1
end repeat
tell XML element "current_conditions"
tell XML element "humidity"
set humidity to get value of XML attribute "data"
end tell
tell XML element "condition"
set condition to get value of XML attribute "data"
end tell
if unit is "SI" then
tell XML element "temp_c"
set temp to get value of XML attribute "data"
my WriteResult("GoogleWeather_Temp", temp & " ºC")
end tell
else
tell XML element "temp_f"
set temp to get value of XML attribute "data"
my WriteResult("GoogleWeather_Temp", temp & "ºF")
end tell
end if
tell XML element "wind_condition"
set wind to get value of XML attribute "data"
end tell
tell XML element "icon"
set currentIcon to get value of XML attribute "data"
set currentIcon to currentIcon
my ShowImage("GoogleWeather_Icon", currentIcon)
end tell
end tell
end tell
end tell
end tell
end tell
my WriteResult("GoogleWeather_Info", condition & "
" & wind & "
" & humidity & "%")
end run