PDA

View Full Version : AppleScript Alarm Help




MichaelQuick232
Jun 25, 2012, 12:02 AM
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
Jun 25, 2012, 02:18 PM
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
Jun 25, 2012, 03:24 PM
There are a couple of ways to get the temperature. Here's one way :

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
Jun 25, 2012, 03:31 PM
There are a couple of ways to get the temperature. Here's one way :

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
Jun 25, 2012, 03:45 PM
Turn up your volume and put this as last line :

say the_temp & "degrees"

MichaelQuick232
Jun 25, 2012, 04:41 PM
Turn up your volume and put this as last line :

say the_temp & "degrees"


Got it, Thanks!

How do I make it say "Seventy-Nine Point Five" instead of "Seven Nine Dot Five"

kryten2
Jun 25, 2012, 05:18 PM
Don't know. When I ran it just a minute ago it said Sixty-Four point One degrees.

Event log :

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.

chown33
Jun 25, 2012, 05:30 PM
How do I make it say "Seventy-Nine Point Five" instead of "Seven Nine Dot Five"

Try a space between the number and the word "degrees":
say the_temp & " degrees"


Or try getting rid of any ° symbol.

MichaelQuick232
Jun 25, 2012, 05:44 PM
Try a space between the number and the word "degrees":
say the_temp & " degrees"


Or try getting rid of any ° symbol.

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

--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

kryten2
Jun 25, 2012, 05:50 PM
Looks like you've found this link useful : http://forums.macrumors.com/showthread.php?t=537002

MichaelQuick232
Jun 25, 2012, 06:38 PM
Looks like you've found this link useful : http://forums.macrumors.com/showthread.php?t=537002

Yes. :D

ckeyes888
Aug 16, 2012, 12:07 AM
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
Aug 16, 2012, 08:21 AM
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.

ckeyes888
Aug 16, 2012, 11:22 AM
Weather Underground has Canadian weather. Here's he link:

http://www.wunderground.com/global/stations/71122.html

Thanks a bunch for checking it out!

Carl

kryten2
Aug 16, 2012, 12:51 PM
Register for Weather API at http://www.wunderground.com/weather/api/ or http://www.worldweatheronline.com/free-weather-feed.aspx

ckeyes888
Aug 16, 2012, 01:20 PM
Thanks. So I need to register with them to get a temperature for Banff, Canada?

Carl

kryten2
Aug 16, 2012, 03:21 PM
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.

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.

ckeyes888
Aug 16, 2012, 09:59 PM
Got it. Works perfectly!

Really appreciate all the help.

Thanks,

Carl

kryten2
Aug 17, 2012, 01:00 PM
Better to change this line :

set the_temp to characters 2 thru 3 of the_temp as string

to this :

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.

christianboyce
Oct 25, 2012, 12:44 AM
I made my own script to read the calendar and blogged about it here:

http://christianboyce.blogspot.com/2009/09/your-appointments-sir_4544.html

Additionally, I use "Weatherman" (which is scriptable) to get the weather in two locations. I extract the information I need from Weatherman and have my Mac speak that too. If you're interested in the Weatherman script let me know.