Get Weather Data

Hi. I am making a simple alarm clock application, and I am trying to make it say the current temperature, and possibly the condition (sunny, partly cloudy, rain). I have searched a while and have still not found an easy solution. If somebody could possibly give me some code for a SOAP call or whatever you think would be best, I would really appreciate it.

Thank You,
Cody J

Maybe somebody could suggest a (hopefully free) application that downloads weather info, that I could script and retrieve the data from that?

google to the rescue? ‘“os x” weather’

http://heat-meteo.sourceforge.net/

Caveat: i’ve never used it.

Thanks for the suggestion, waltr, but that doesn’t really help me. I need to get the temperature into my app, like as a variable.

Assuming you are in the USA, you might try this http://www.weather.gov/xml/

Adam: That seems to be exactly what I needed. It would be great if somebody could explain how to use SOAP to get thew data in to an applescript app though.

There’s an example in /Library/Scripts/Internet Services/ called Current Temperature by Zipcode. It doesn’t work any more, but it does outline how it’s done.

I’ve tried to find some information on replacing the service used in the script recommended by Adam Bell, but I can’t find any free ones. I suppose I will just have to check it on my screen, but I was hoping I could have my mac speak it.

Thanks for all the help, Adam.

You’re welcome, and I can’t either (nor can I unscramble the syntax for a request). Where I live, there’s a TV station that has a weather block in it’s cover page - it’s easy to grab the temperature from the source of that.

For Halifax, Nova Scotia, I can get this:


set lead to "<strong>Today: </strong><nobr>"
set tail to "°"

set cbc to (do shell script "curl [url=http://www.cbc.ca/ns/)]http://www.cbc.ca/ns/")[/url]
set text item delimiters to lead
set part_1 to text item 2 of cbc
set text item delimiters to tail
set high to text item 1 of part_1
set text item delimiters to ""
say "The high for today is " & high & " degrees C"

Or this one (found by Googling “Current Temperature in Halifax”):


set leader to "Current Temperature"
set T to do shell script "curl [url=http://www.cita.utoronto.ca/~rjh/halifax/]http://www.cita.utoronto.ca/~rjh/halifax/"[/url]
set text item delimiters to leader
set part_1 to last text item of T
set Temp to paragraph 1 of part_1
set text item delimiters to ""
display dialog "The Halifax temperature is now" & Temp & (ASCII character 188) & "C"
-- or
say "The current temperature is" & Temp & " Celcius"

Or for Central Park, New York City, NY:

set head_1 to "Temperature </FONT></FONT></B></TD>

<TD><FONT FACE=\"Arial,Helvetica\">  "
set head_2 to "Relative Humidity </FONT></FONT></B></TD>

<TD><FONT FACE=\"Arial,Helvetica\">  "
set W to do shell script "curl [url=http://weather.noaa.gov/weather/current/KNYC.html]http://weather.noaa.gov/weather/current/KNYC.html"[/url]
set text item delimiters to head_1
set T to paragraph 1 of W's text item 2
set text item delimiters to head_2
set H to paragraph 1 of W's text item 2
set text item delimiters to ""

display dialog "The temperature is now " & T & return & "with a relative humidity of " & H

Thank a lot! With your scripts I got it to work perfectly (great idea BTW, didn’t think of extracting it from a webpage). The only one that I could get to work on my machine was the middle one that used http://www.cita.utoronto.ca/~rjh/halifax/. I modified it to use the boston.com weather for Delray Beach, found using google.

The procedure for anyone else who wishes to get the current temperature into their script is:

  1. Go to weather.boston.com and search for your city.
  2. Once you find your city’s weather at boston.com, make a note of the code at the end of the URL (with the URL Delray Beach, FL weather | Boston.com, the code would be 3X1)
  3. Put the code in the code variable at the top of the script below:
--Your city's boston.com code here:
set code to "3X1"

set lead to "<span class=\"currenttemp\">"
set tail to "°"
set T to do shell script "curl [url=http://weather.boston.com/?code=]http://weather.boston.com/?code="[/url] & code
set text item delimiters to lead
set part_1 to text item 2 of T
set text item delimiters to tail
set temperature to text item 1 of part_1
set text item delimiters to ""
display dialog "The current temperature is " & temperature & (ASCII character 188) & " F."

4.Build and run your script.

Once again, thank you very much, Adam.

Because my wife and I often travel around Northern New England, I used your link to produce this:

set Weather to ""
set Places to {"Bangor, ME", "Augusta, ME", "South Portland, ME", "Portsmouth, NH", "Manchester, NH", "Laconia, NH"}
set codes to {"BGR", "AUG", "SPMX", "PSM", "MHT", "LCI"}
set temp to "<span class=\"currenttemp\">"
set hum to "humidity</strong>: "
set cond to "currentcondition\"><strong>"
set wind to "Wind speed</strong>: "
repeat with k from 1 to count codes
	set wcode to item k of codes
	set tPlace to item k of Places
	set T to (do shell script "curl [url=http://weather.boston.com/?code=]http://weather.boston.com/?code="[/url] & wcode)
	set text item delimiters to cond
	set tCond to first word of text item 2 of T
	set text item delimiters to temp
	set tTemp to first word of text item 2 of T
	set text item delimiters to hum
	set tHum to first word of text item 2 of T
	set text item delimiters to wind
	set tWind to first word of text item 2 of T
	set text item delimiters to ""
	set Weather to Weather & "---" & return & tPlace & return & tCond & " and " & tTemp & (ASCII character 188) & "F," & " Humidity " & tHum & "%, " & "Wind " & tWind & " mph" & return
end repeat
set Weather to Weather & "---"
display dialog Weather