Weather Script:

Hi dylanweber

Hi noticed your script for me, kept returning the Tonight, and Tomorrow swapped around?
It seems to be the order of the text item you choose.
Also you can get the Tomorrow High from the 5day

The changes below worked.

--tomorrow description
set AppleScript's text item delimiters to " <div style=\"font-family: Arial; font-size: 12px; font-weight: normal; padding-top: 5px;\">"
set tomdesc to text item 4 of thedata15 as string
set AppleScript's text item delimiters to "</div>"
set tomdesc to text item 1 of tomdesc

--tonight description
set AppleScript's text item delimiters to " <div style=\"font-family: Arial; font-size: 12px; font-weight: normal; padding-top: 5px;\">"
set tondesc to text item 3 of thedata15 as string
set AppleScript's text item delimiters to "</div>"
set tondesc to text item 1 of tondesc

--today high/current temp
set AppleScript's text item delimiters to "<div style=\"font-family: Arial; font-size: 12px; font-weight: bold; padding-top: 10px;\">High: "
set todayhigh to text item 2 of thedata15
set AppleScript's text item delimiters to " °F&nbsp"
set todhigh to text item 1 of todayhigh as integer

Also I posted a quick page showing the growl using script .
I will work on its format a bit more when I have time.
Weather_from_Yahoo.html

The script errored for me even with Mark’s improvements when I tried them. The site pages need to be studied over the course of a day to see what changes occur.

Below is my own attempt to foolproof it[*]. I’ll clean it up and monitor its efficacy at various times over the next 24 hours or so and either develop or scrap the “Early morning” business. (During the local early hours, there are two “days” for today in the 5-day forecast. The first is for “Currently” and “Early morning”, the second for “Today” and “Tonight”.)

[*] By “foolproof it”, I mean “get it to work reliably until the site’s page code is changed”.

The script makes use of multiple delimiters, new with Snow Leopard, to cover various possibilities when parsing temperatures. Earlier systems only heed the first delimiter in the list and it might not be the right one for them. (The differences between the delimiters in each list aren’t always visible below, but they compile properly if you use the “Open this Scriplet” link.)

The AccuWeather site itself appears to be updated by some process which doesn’t update all the page elements at once. No version of the script stands a chance against that!

(* This script probably only works with AccuWeather.com's US site. *)

property agent : "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3"

property returnTab : return & tab
property twoReturns : return & return

on displayWeather(baseURL)
	display dialog "Loading Weather Forecast; may be a few seconds!" buttons {"OK"} default button 1 giving up after 2
	
	set nowData to do shell script "curl " & "-A" & space & quoted form of agent & space & baseURL & "city-weather-forecast.asp" as string
	
	set astid to AppleScript's text item delimiters
	
	-- Location.
	set AppleScript's text item delimiters to "class=\"city_heading\">"
	set nowData to text from word 1 to -1 of (text from text item 3 to -1 of nowData)
	set city to text from word 1 to word -1 of paragraph 1 of nowData
	
	-- Local date.
	set AppleScript's text item delimiters to "<div style=\"float: left; width: 330px;\">"
	set nowData to text from text item 2 to -1 of nowData
	set {m, d, y} to words 2 thru 4 of nowData
	set today to (date "Monday 1 January 1000 00:00:00")
	set today's year to y
	set today's month to (offset of (text 1 thru 3 of m) in "JanFebMarAprMayJunJulAugSepOctNovDec") div 3 + 1
	set today's day to d
	
	-- Current temperature.
	set AppleScript's text item delimiters to "<div id=\"quicklook_current_temps\">"
	set nowData to text from text item 2 to -1 of nowData
	set AppleScript's text item delimiters to "°F</div>"
	set temperatureNow to text item 1 of nowData & "ºF"
	
	-- Current feel.
	set AppleScript's text item delimiters to "<div id=\"quicklook_current_rfval\">"
	set nowData to text from text item 2 to -1 of nowData
	set AppleScript's text item delimiters to "°F</div>"
	set feelNow to text item 1 of nowData & "ºF"
	
	-- Description for the current moment.
	set AppleScript's text item delimiters to "<div id=\"quicklook_current_wxtext\">"
	set nowData to text from text item 2 to -1 of nowData
	set AppleScript's text item delimiters to "</div>"
	set nowDescription to text item 1 of nowData
	
	-- Dialog text for the current moment.
	set nowText to "At the moment" & returnTab & nowDescription & returnTab & "Temperature: " & temperatureNow & returnTab & "Feels like: " & feelNow & twoReturns
	
	-- 5-day forecast curl.
	set fiveDayData to do shell script "/usr/bin/curl" & space & ¬
		"-A" & space & quoted form of agent & space & ¬
		"-d" & space & quoted form of "partner=accuweather" & space & quoted form of baseURL & "forecast.asp?"
	
	-- Data for today and tomorrow.
	set dayAfterTomorrow to today + 2 * days
	set AppleScript's text item delimiters to "<div style=\"font-family: Arial; font-size: 14px; font-weight: bold; padding: 5px 0px 0px 5px;\">" & (dayAfterTomorrow's weekday) & "</a>, " & text 1 thru 3 of (dayAfterTomorrow's month as text) & " " & (dayAfterTomorrow's day) & "</div>"
	set twoDayData to text item 1 of fiveDayData
	
	set tomorrow to today + days
	set AppleScript's text item delimiters to "<div style=\"font-family: Arial; font-size: 14px; font-weight: bold; padding: 5px 0px 0px 5px;\">" & (tomorrow's weekday) & "</a>, " & text 1 thru 3 of (tomorrow's month as text) & " " & (tomorrow's day) & "</div>"
	set {todayData, tomorrowData} to text items of twoDayData
	
	-- High and low temperatures for today and tomorrow.
	set AppleScript's text item delimiters to {" °F", " °F", "°F"}
	if (todayData contains "Currently at") and (todayData does not contain "Early morning") then
		set todayHigh to ""
	else
		set todayHigh to text item -5 of todayData
	end if
	set todayLow to text item -3 of todayData
	set tomorrowHigh to text item 1 of tomorrowData
	set tomorrowLow to text item 3 of tomorrowData
	
	set AppleScript's text item delimiters to {"High: ", "High: "}
	if ((count todayHigh) > 0) then set todayHigh to text item 2 of todayHigh & "ºF"
	set tomorrowHigh to text item 2 of tomorrowHigh & "ºF"
	
	set AppleScript's text item delimiters to {"Low: ", "Low: "}
	set todayLow to text item 2 of todayLow & "ºF"
	set tomorrowLow to text item 2 of tomorrowLow & "ºF"
	
	-- Descriptions for today, tonight, and tomorrow.
	set AppleScript's text item delimiters to " <div style=\"font-family: Arial; font-size: 12px; font-weight: normal; padding-top: 5px;\">"
	if (todayData contains "Currently at") and (todayData does not contain "Early morning") then
		-- set earlyDescription to ""
		set todayDescription to ""
	else
		(* if (todayData contains "Early Morning") then
			set earlyDescription to text item -3 of todayData & return
		else
			set earlyDescription to ""
		end if *)
		set todayDescription to text item -2 of todayData
	end if
	set tonightDescription to text item -1 of todayData
	set tomorrowDescription to text item 2 of tomorrowData
	
	set AppleScript's text item delimiters to "</div>"
	if ((count todayDescription) > 0) then set todayDescription to text item 1 of todayDescription
	set tonightDescription to text item 1 of tonightDescription
	set tomorrowDescription to text item 1 of tomorrowDescription
	
	-- Dialog text for today, tonight, and tomorrow.
	if ((count todayDescription) + (count todayHigh) is 0) then
		set todayText to ""
	else
		set todayText to "Today" & returnTab & todayDescription & returnTab & "High: " & todayHigh & twoReturns
	end if
	set tonightText to "Tonight" & returnTab & tonightDescription & returnTab & "Low: " & todayLow & twoReturns
	set tomorrowText to "Tomorrow" & returnTab & tomorrowDescription & returnTab & "High: " & tomorrowHigh & returnTab & "Low: " & tomorrowLow
	
	set AppleScript's text item delimiters to astid
	-----------------------------------------------------------------------------------------
	set dialog to "Weather for " & city & return & (today's date string) & twoReturns & nowText & todayText & tonightText & tomorrowText
	display dialog dialog buttons {"OK"} default button 1 with title "Weather"
end displayWeather

displayWeather("http://www.accuweather.com/us/il/highland-park/60035/")
displayWeather("http://www.accuweather.com/us/ny/new-york/10001/")

I did not know about that, how does that work?

I think I going to keep to my script and yahoo’s page. It been working with no faults.
I even have a version that displays the current weathers icon in the growl window.

Well, my Accuweather script works fine. All 24 hours of the day:

property agent : "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3"
display dialog "Loading Weather Forcast; may a few seconds!" buttons {"OK"} default button 1 giving up after 2
set thedata to do shell script "curl " & "-A" & space & quoted form of agent & space & "http://www.accuweather.com/us/il/highland-park/60035/city-weather-forecast.asp" as string

--temp
set AppleScript's text item delimiters to "<div id=\"quicklook_current_temps\">"
set temp to text item 2 of thedata
set AppleScript's text item delimiters to "°F</div>\r\t\t\t\t\t\t<div id=\"quicklook_current_rf\">"
set temp to text item 1 of temp as integer

--feels like
set AppleScript's text item delimiters to "<div id=\"quicklook_current_rfval\">"
set feeltemp to text item 2 of thedata
set AppleScript's text item delimiters to "°F</div>\r\t\t\t\t\t\t<div class=\"textsmall\" style=\"margin-top:3px;\">"
set feeltemp to text item 1 of feeltemp as integer

--Description
set AppleScript's text item delimiters to "<div id=\"quicklook_current_wxtext\">"
set desc to text item 2 of thedata
set AppleScript's text item delimiters to "</div>\r\t\t\t\t<div id=\"quicklook_current_wxdataleft\">"
set desc to text item 1 of desc

--tomorrow high
set AppleScript's text item delimiters to "<div class=\"quicklook_day_tempdata\">"
set tomorrowdata1 to text item 3 of thedata
try
	set AppleScript's text item delimiters to "\r\t\t\t\t\t\tHigh: "
	set onetomorrowdata to text item 2 of tomorrowdata1
on error
	set AppleScript's text item delimiters to "<div class=\"quicklook_day_tempdata\">"
	set tomorrowdata1 to text item 2 of thedata
	set AppleScript's text item delimiters to "\r\t\t\t\t\t\tHigh: "
	set onetomorrowdata to text item 2 of tomorrowdata1
end try
set AppleScript's text item delimiters to "°F<br/>"
set tomhigh to text item 1 of onetomorrowdata as integer

--15 day forcast curl
set thedata15 to do shell script "/usr/bin/curl" & space & ¬
	"-A" & space & quoted form of agent & space & ¬
	"-d" & space & quoted form of "partner=accuweather" & space & quoted form of "http://www.accuweather.com/us/il/highland%20park/60035/forecast.asp?"

--today low

set AppleScript's text item delimiters to "<div style=\"font-family: Arial; font-size: 12px; font-weight: bold; padding-top: 10px;\">Low: "
set todaydata to text item 2 of thedata15
set AppleScript's text item delimiters to " °F&nbsp"
set low to text item 1 of todaydata as integer

--tomorrow low

set AppleScript's text item delimiters to "<div style=\"font-family: Arial; font-size: 12px; font-weight: bold; padding-top: 10px;\">Low: "
set tomorrowdata2 to text item 3 of thedata15
set AppleScript's text item delimiters to " °F&nbsp"
set tomlow to text item 1 of tomorrowdata2 as integer

--tomorrow description
set AppleScript's text item delimiters to " <div style=\"font-family: Arial; font-size: 12px; font-weight: normal; padding-top: 5px;\">"
set tomdesc to text item 4 of thedata15 as string
set AppleScript's text item delimiters to "</div>"
set tomdesc to text item 1 of tomdesc

--tonight description
set AppleScript's text item delimiters to " <div style=\"font-family: Arial; font-size: 12px; font-weight: normal; padding-top: 5px;\">"
set tondesc to text item 3 of thedata15 as string
set AppleScript's text item delimiters to "</div>"
set tondesc to text item 1 of tondesc

--today high/current temp
set AppleScript's text item delimiters to "<div style=\"font-family: Arial; font-size: 12px; font-weight: bold; padding-top: 10px;\">High: "
set todayhigh to text item 2 of thedata15
set AppleScript's text item delimiters to " °F&nbsp"
set todhigh to text item 1 of todayhigh as integer
todhigh & " & " & tomdesc & " & " & tondesc
-----------------------------------------------------------------------------------------
set dialog to "Weather for " & (current date) & return & return & "Today" & return & tab & desc & return & "\tNow: " & temp & "ºF" & return & "\tFeels like: " & feeltemp & "ºF" & return & "\tHigh: " & todhigh & "ºF" & return & "\tLow: " & low & "ºF" & return & return & "Tonight" & return & tab & tondesc & return & return & "Tomorrow" & return & tab & tomdesc & return & tab & "High: " & tomhigh & "ºF" & return & tab & "Low: " & tomlow & "ºF"
display dialog dialog buttons {"OK"} default button 1 with title "Weather"

As at 14:40 GMT (08:40 your time, I guess), it’s displaying an incorrect high temperature for tomorrow. (29 instead of the 28 shown on the Web page.)

set txt to "Now is the time for all good men to come to the aid of the party."

-- In SL, a text can be split using several delimiters at once:
set AppleScript's text item delimiters to {"is", "to", "."}
set tis to txt's text items
--> {"Now ", " the time for all good men ", " come ", " the aid of the party", ""}

-- Earlier systems only use the first delimiter:
--> {"Now ", " the time for all good men to come to the aid of the party."}

-- However, even with SL, only the first delimiter is used when coercing a list to text:
tis as text
--> "Now is the time for all good men is come is the aid of the partyis"

I just realised, I posted the thing about getting the tomhigh from the 5 day,

but did not post it … doh

This returns 28

--tomorrow high

set AppleScript's text item delimiters to "<div style=\"font-family: Arial; font-size: 12px; font-weight: bold; padding-top: 10px;\">High:"
set tomorrowdata3 to text item 3 of thedata15
set AppleScript's text item delimiters to " °F&nbsp"
set tomhigh to text item 1 of tomorrowdata3 as integer

Ah I see, many thanks Nigel.

As at 17:16 today, Highland Park time, the “Sunday, Feb 14” section of the Highand Park 5-day forecast page has changed to “Currently at 5:16 PM”. The Dylan/Mark script is now returning the current temperature as today’s high, tomorrow’s description as tonight’s, tomorrow night’s description as tomorrow’s, and Tuesday’s high temperature as tomorrow’s. My script is so far holding its own.

My textutil/yahoo one holding its own also. :wink:

As at 01:34, Highland Park time, the AccuWeather 5-day forecast for Highland Park is showing (as I reported yesterday) three and two half days. The two half days are “Currently at 1:34 AM”/“Early Morning Mon, Feb 15” and “Monday, Feb 15”/“Monday Night, Feb 15”. The Dylan/Mark version of the script gives the “Early Morning” low temperature as that for the day, the day-time description as tonight’s, tonight’s description as tomorrow’s, and tonight’s low temperature as tomorrow’s. My own version ” I’m relieved to find ” has again got everything right.

Here’s a tidied-up rendition of mine, which also shows the “Early Morning” data when they’re there. It display’s the site-local date/time rather than the machine-local one.

It’s a lot less cold in New York City than it is in Highland Park!

(* This script probably only works with AccuWeather.com's US site. *)

property agent : "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3"

property returnTab : return & tab
property twoReturns : return & return

on displayWeather(baseURL)
	display dialog "Loading Weather Forecast; may be a few seconds!" buttons {"OK"} default button 1 giving up after 2
	
	set nowData to do shell script "curl " & "-A" & space & quoted form of agent & space & quoted form of (baseURL & "city-weather-forecast.asp")
	
	set astid to AppleScript's text item delimiters
	
	-- Location.
	set AppleScript's text item delimiters to "class=\"city_heading\">"
	set nowData to text from word 1 to -1 of (text from text item 3 to -1 of nowData)
	set city to text from word 1 to word -1 of paragraph 1 of nowData
	
	-- Local date and time.
	set AppleScript's text item delimiters to "<div style=\"float: left; width: 330px;\">"
	set nowData to text from text item 2 to -1 of nowData
	set {m, d, y} to words 2 thru 4 of nowData
	set today to (date "Monday 1 January 1000 00:00:00")
	set today's year to y
	set today's month to (offset of (text 1 thru 3 of m) in "JanFebMarAprMayJunJulAugSepOctNovDec") div 3 + 1
	set today's day to d
	set AppleScript's text item delimiters to ">Currently</span> At "
	set nowData to text from text item 2 to -1 of nowData
	set AppleScript's text item delimiters to "</div>"
	set t to text item 1 of nowData
	tell t to set today's time to ((text 1 thru -6) mod 12 + ((text -2 thru -1 is "PM") as integer) * 12) * hours + (text -4 thru -3) * minutes
	
	-- Current temperature.
	set AppleScript's text item delimiters to "<div id=\"quicklook_current_temps\">"
	set nowData to text from text item 2 to -1 of nowData
	set AppleScript's text item delimiters to "°F</div>"
	set temperatureNow to text item 1 of nowData & "ºF"
	
	-- Current feel.
	set AppleScript's text item delimiters to "<div id=\"quicklook_current_rfval\">"
	set nowData to text from text item 2 to -1 of nowData
	set AppleScript's text item delimiters to "°F</div>"
	set feelNow to text item 1 of nowData & "ºF"
	
	-- Description of current conditions.
	set AppleScript's text item delimiters to "<div id=\"quicklook_current_wxtext\">"
	set nowData to text from text item 2 to -1 of nowData
	set AppleScript's text item delimiters to "</div>"
	set nowDescription to text item 1 of nowData
	
	-- Dialog text for the current moment.
	set nowText to "Currently" & returnTab & nowDescription & returnTab & "Temperature: " & temperatureNow & returnTab & "Feels like: " & feelNow & twoReturns
	
	-- 5-day forecast curl.
	set fiveDayData to do shell script "/usr/bin/curl" & space & ¬
		"-A" & space & quoted form of agent & space & ¬
		"-d" & space & quoted form of "partner=accuweather" & space & quoted form of (baseURL & "forecast.asp?")
	
	-- Data for today and tomorrow.
	set dayAfterTomorrow to today + 2 * days
	set AppleScript's text item delimiters to "<div style=\"font-family: Arial; font-size: 14px; font-weight: bold; padding: 5px 0px 0px 5px;\">" & (dayAfterTomorrow's weekday) & "</a>, " & text 1 thru 3 of (dayAfterTomorrow's month as text) & " " & (dayAfterTomorrow's day) & "</div>"
	set twoDayData to text item 1 of fiveDayData
	
	set tomorrow to today + days
	set AppleScript's text item delimiters to "<div style=\"font-family: Arial; font-size: 14px; font-weight: bold; padding: 5px 0px 0px 5px;\">" & (tomorrow's weekday) & "</a>, " & text 1 thru 3 of (tomorrow's month as text) & " " & (tomorrow's day) & "</div>"
	set {todayData, tomorrowData} to text items of twoDayData
	
	-- High and low temperatures for today and tomorrow.
	set AppleScript's text item delimiters to {" °F", " °F", "°F"}
	set earlyLow to ""
	set todayHigh to text item -5 of todayData
	if (todayData contains "Early morning") then
		set earlyLow to text item -7 of todayData
	else if (todayData contains "Currently at") then
		set todayHigh to ""
	end if
	set todayLow to text item -3 of todayData
	set tomorrowHigh to text item 1 of tomorrowData
	set tomorrowLow to text item 3 of tomorrowData
	
	set AppleScript's text item delimiters to {"High: ", "High: "}
	if ((count todayHigh) > 0) then set todayHigh to text item 2 of todayHigh & "ºF"
	set tomorrowHigh to text item 2 of tomorrowHigh & "ºF"
	
	set AppleScript's text item delimiters to {"Low: ", "Low: "}
	if ((count earlyLow) > 0) then set earlyLow to text item 2 of earlyLow & "ºF"
	set todayLow to text item 2 of todayLow & "ºF"
	set tomorrowLow to text item 2 of tomorrowLow & "ºF"
	
	-- Descriptions for today, tonight, and tomorrow.
	set AppleScript's text item delimiters to " <div style=\"font-family: Arial; font-size: 12px; font-weight: normal; padding-top: 5px;\">"
	set earlyDescription to ""
	set todayDescription to text item -2 of todayData
	if (todayData contains "Early Morning") then
		set earlyDescription to text item -3 of todayData & return
	else if (todayData contains "Currently at") then
		set todayDescription to ""
	end if
	set tonightDescription to text item -1 of todayData
	set tomorrowDescription to text item 2 of tomorrowData
	
	set AppleScript's text item delimiters to "</div>"
	if ((count earlyDescription) > 0) then set earlyDescription to text item 1 of earlyDescription
	if ((count todayDescription) > 0) then set todayDescription to text item 1 of todayDescription
	set tonightDescription to text item 1 of tonightDescription
	set tomorrowDescription to text item 1 of tomorrowDescription
	
	-- Dialog text for today, tonight, and tomorrow.
	if ((count todayDescription) + (count todayHigh) > 0) then
		if ((count earlyDescription) is 0) then
			set todayText to "Today"
		else
			set todayText to "Early morning" & returnTab & earlyDescription & returnTab & "Low: " & earlyLow & twoReturns & "Later"
		end if
		set todayText to todayText & returnTab & todayDescription & returnTab & "High: " & todayHigh & twoReturns
	else
		set todayText to ""
	end if
	set tonightText to "Tonight" & returnTab & tonightDescription & returnTab & "Low: " & todayLow & twoReturns
	set tomorrowText to "Tomorrow" & returnTab & tomorrowDescription & returnTab & "High: " & tomorrowHigh & returnTab & "Low: " & tomorrowLow
	
	set AppleScript's text item delimiters to astid
	-----------------------------------------------------------------------------------------
	set dialog to "Weather for " & city & return & today & twoReturns & nowText & todayText & tonightText & tomorrowText
	display dialog dialog buttons {"OK"} default button 1 with title "Weather"
end displayWeather

displayWeather("http://www.accuweather.com/us/il/highland-park/60035/")
displayWeather("http://www.accuweather.com/us/ny/new-york/10001/")

Eh I think Dylan can take full credit for his script. It is not mine I only offered some changes to try and Help:D
not that they work well due to the accuweather sites vagaries.

My script is the textutil one using yahoo weather pages rather than the accuweather site. which has never got the wrong info…
I have been updating my script, and will most likely post the results. just changing the format of the display.
I like having the two growl windows, but I want to strip some of the info and rearrange some. It is easy to do, due to the way I grab the page. just need a moment to do it.

I think I’ll start fresh with a new site later.