Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MichaelQuick232

macrumors newbie
Original poster
Oct 14, 2011
14
0
Hello everyone.

I want to make an alarm clock that says

"Good morning, its (time). The temperature is (temp) and the forecast for Hampton is (forecast), and you have (connects to calendar and says what I have to do today)"

All whilst playing a playlist from iTunes.

Does anyone know how to help me do this in applescript? I have API keys but I have no idea how to implement them or where to implement them.

I know the bare minimum of applescript.

--MQ
 

MichaelQuick232

macrumors newbie
Original poster
Oct 14, 2011
14
0
Well I got a little bit of code done. but I can't figure out how to pull the temperature off of the API site and put it in the code.

Heres the API link, http://api.wunderground.com/api/28fc850a3c5ed668/conditions/q/06247.json
I'm trying to pull the "temp_f" section but no matter what I try I get the error

Can’t get temp_f of "\"response\":
\"version\": \"0.1\"
,\"termsof......n_time_rfc822\":\"
"

Heres the code I have tried.
--Mute Volume
set volume output volume 0

--Quit Apps

tell application "iTunes"
quit
end tell

tell application "QuickTime Player"
quit
end tell


--Get Weather Temp Link
set TID to AppleScript's text item delimiters
set webaddress to do shell script "curl http://api.wunderground.com/api/28fc850a3c5ed668/conditions/q/06247.json"

--Get Temperature
set AppleScript's text item delimiters to ¬
"<span class=\"nowrap\"><b>"
set text1 to webaddress
set AppleScript's text item delimiters to "</b>°"
set temp to temp_f of text1
set temp to round (temp)

Thanks!
 

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
There are a couple of ways to get the temperature. Here's one way :

Code:
set the_temp to do shell script "curl http://api.wunderground.com/api/28fc850a3c5ed668/conditions/q/06247.json | grep temp_f"
try
	set oldDelims to AppleScript's text item delimiters -- save their current state
	set AppleScript's text item delimiters to {":"} -- declare new delimiters
	set these_items to the text items of the_temp
	set this_item to item 2 of these_items
	set AppleScript's text item delimiters to {","}
	set these_items to the text items of this_item
	set AppleScript's text item delimiters to "°"
	set the_temp to these_items as text
	set AppleScript's text item delimiters to oldDelims -- restore them
on error
	set AppleScript's text item delimiters to oldDelims -- restore them in case something went wrong
end try
the_temp -- "72.9°"
 

MichaelQuick232

macrumors newbie
Original poster
Oct 14, 2011
14
0
There are a couple of ways to get the temperature. Here's one way :

Code:
set the_temp to do shell script "curl http://api.wunderground.com/api/28fc850a3c5ed668/conditions/q/06247.json | grep temp_f"
try
	set oldDelims to AppleScript's text item delimiters -- save their current state
	set AppleScript's text item delimiters to {":"} -- declare new delimiters
	set these_items to the text items of the_temp
	set this_item to item 2 of these_items
	set AppleScript's text item delimiters to {","}
	set these_items to the text items of this_item
	set AppleScript's text item delimiters to "°"
	set the_temp to these_items as text
	set AppleScript's text item delimiters to oldDelims -- restore them
on error
	set AppleScript's text item delimiters to oldDelims -- restore them in case something went wrong
end try
the_temp -- "72.9°"


So later on when I have speech recognition it'll look for "The_temp" and say whatever the current temperature is?
 

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
Don't know. When I ran it just a minute ago it said Sixty-Four point One degrees.

Event log :

Code:
tell current application
	do shell script "curl http://api.wunderground.com/api/28fc850a3c5ed668/conditions/q/06247.json | grep temp_f"
		"\t\t\"temp_f\":64.1,"
	say "64.1°degrees"
end tell

What version of OS X are you running? I'm on Leopard.
 

MichaelQuick232

macrumors newbie
Original poster
Oct 14, 2011
14
0
Try a space between the number and the word "degrees":
Code:
say the_temp & " degrees"

Or try getting rid of any ° symbol.

heres my current working code, I'm still working on it.

Code:
 --Mute Volume
set volume output volume 0

--Quit Apps

tell application "iTunes"
	quit
end tell

tell application "QuickTime Player"
	quit
end tell

tell application "Terminal"
	quit
end tell

--open curtains
tell application "Terminal"
	activate
	tell application "System Events"
		keystroke "heyu start"
		keystroke return
		delay 3
		keystroke "heyu on A9"
		delay 2
		quit
	end tell
	
	--Get Temperature
	set the_temp to do shell script "curl http://api.wunderground.com/api/28fc850a3c5ed668/conditions/q/06247.json | grep temp_f"
	try
		set oldDelims to AppleScript's text item delimiters -- save their current state
		set AppleScript's text item delimiters to {":"} -- declare new delimiters
		set temp_item to the text items of the_temp
		set this_item to item 2 of temp_item
		set AppleScript's text item delimiters to {","}
		set temp_item to the text items of this_item
		set AppleScript's text item delimiters to ""
		set the_temp to temp_item as text
		set AppleScript's text item delimiters to oldDelims -- restore them
	on error
		set AppleScript's text item delimiters to oldDelims -- restore them in case something went wrong
	end try
	the_temp --
	set the_temp to round (the_temp)
	
	--Check new mail
	tell application "Mail"
		check for new mail
	end tell
end tell

-- get the time
on thetime()
	
	-- hours
	set timeStr to time string of (current date)
	set Pos to offset of ":" in timeStr
	set theHour to characters 1 thru (Pos - 1) of timeStr as string
	set timeStr to characters (Pos + 1) through end of timeStr as string
	
	-- minutes
	set Pos to offset of ":" in timeStr
	set theMin to characters 1 thru (Pos - 1) of timeStr as string
	set timeStr to characters (Pos + 1) through end of timeStr as string
	
	
	return (theHour & ":" & theMin & "") as string
end thetime

--Turn on lights and open curtains
tell application "Terminal"
	activate
	tell application "System Events"
		keystroke "heyu start"
		keystroke return
		delay 3
		keystroke "heyu on A9"
		keystroke return
	end tell
	delay 3
	quit
end tell

--Say good morning
set d to (date string of (current date))
set volume output volume 100
say "Good morning. it's" & thetime()
say "on" & d
say "and the temperature in Hampton this morning is" & the_temp
say "degrees fahrenheit"
delay 1
-- say "The weather outlook for today is" & theForecast
-- say "Your schedule for the day consists of" & e_vents
say "Here is your morning playlist."
set volume output volume 17
 

ckeyes888

macrumors newbie
Aug 15, 2012
4
0
Can the script be adapted for Canadian postal codes, or possibly weather station codes? Would love to be able to get the temp in Banff.

Thanks,

Carl
 

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
Would love to be able to get the temp in Banff.

At first I was a little stumped by your question. Temperature is measured in Celsius, Fahrenheit, Kelvin and probably some other scale too. I guess you mean Banff in Alberta, Canada? Please provide an URL where you get the weather forecast for Banff. Preferably an API link like the one MichaelQuick232 used.
 

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
This will give you the temperature from the link you provided eg http://www.wunderground.com/global/stations/71122.html. It's ugly and error-prone.

Code:
set the_temp to do shell script "curl http://www.wunderground.com/global/stations/71122.html | grep og:title"
try
	set oldDelims to AppleScript's text item delimiters -- save their current state
	set AppleScript's text item delimiters to {"|"} -- declare new delimiters
	set these_items to the text items of the_temp
	set the_temp to item 2 of these_items as string
	set AppleScript's text item delimiters to oldDelims -- restore them
	set the_temp to characters 2 thru 3 of the_temp as string
	set AppleScript's text item delimiters to oldDelims -- restore them
on error
	set AppleScript's text item delimiters to oldDelims -- restore them in case something went wrong
end try
-- the log line is just to show the result in the event log
log the_temp
the_temp

If you register you'll be able to generate a much cleaner and less error-prone report for your location like the example one in the attached tumbnail.
 

Attachments

  • Picture 2.png
    Picture 2.png
    229.8 KB · Views: 147
  • Picture 1.png
    Picture 1.png
    127 KB · Views: 194
  • Picture 3.png
    Picture 3.png
    14.4 KB · Views: 469
Last edited:

ckeyes888

macrumors newbie
Aug 15, 2012
4
0
Got it. Works perfectly!

Really appreciate all the help.

Thanks,

Carl
 
Last edited:

kryten2

macrumors 65816
Mar 17, 2012
1,114
99
Belgium
Better to change this line :

Code:
set the_temp to characters 2 thru 3 of the_temp as string

to this :

Code:
set the_temp to characters 2 thru ((offset of "&" in the_temp) - 1) of the_temp as string

This will catch negative temperatures in Winter as well.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.