AppleScript Alarm Help

Discussion in 'Mac Programming' started by MichaelQuick232, Jun 24, 2012.

  1. macrumors newbie

    Joined:
    Oct 14, 2011
    #1
    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
     
  2. thread starter macrumors newbie

    Joined:
    Oct 14, 2011
    #2
    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

    "

    Heres the code I have tried.
    Thanks!
     
  3. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #3
    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°"
     
  4. thread starter macrumors newbie

    Joined:
    Oct 14, 2011
    #4

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

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #5
    Turn up your volume and put this as last line :

    Code:
    say the_temp & "degrees"
     
  6. thread starter macrumors newbie

    Joined:
    Oct 14, 2011
    #6

    Got it, Thanks!

    How do I make it say "Seventy-Nine Point Five" instead of "Seven Nine Dot Five"
     
  7. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #7
    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.
     
  8. macrumors 603

    Joined:
    Aug 9, 2009
    #8
    Try a space between the number and the word "degrees":
    Code:
    say the_temp & " degrees"
    
    Or try getting rid of any ° symbol.
     
  9. thread starter macrumors newbie

    Joined:
    Oct 14, 2011
    #9
    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
    
     
  10. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #10
  11. thread starter macrumors newbie

    Joined:
    Oct 14, 2011
    #11
  12. macrumors newbie

    Joined:
    Aug 15, 2012
    #12
    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
     
  13. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #13
    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.
     
  14. macrumors newbie

    Joined:
    Aug 15, 2012
    #14
  15. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
  16. macrumors newbie

    Joined:
    Aug 15, 2012
    #16
    Thanks. So I need to register with them to get a temperature for Banff, Canada?

    Carl
     
  17. kryten2, Aug 16, 2012
    Last edited: Aug 16, 2012

    macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #17
    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.
     

    Attached Files:

  18. ckeyes888, Aug 16, 2012
    Last edited: Aug 16, 2012

    macrumors newbie

    Joined:
    Aug 15, 2012
    #18
    Got it. Works perfectly!

    Really appreciate all the help.

    Thanks,

    Carl
     
  19. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #19
    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.
     
  20. macrumors newbie

    Joined:
    Jun 19, 2007
    #20
    Another way to do it

    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.
     

Share This Page