Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 25, 2012, 12:02 AM   #1
MichaelQuick232
macrumors newbie
 
Join Date: Oct 2011
AppleScript Alarm Help

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 is offline   0 Reply With Quote
Old Jun 25, 2012, 02:18 PM   #2
MichaelQuick232
Thread Starter
macrumors newbie
 
Join Date: Oct 2011
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/28fc...s/q/06247.json
I'm trying to pull the "temp_f" section but no matter what I try I get the error

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

Heres the code I have tried.
Quote:
--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!
MichaelQuick232 is offline   0 Reply With Quote
Old Jun 25, 2012, 03:24 PM   #3
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: 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°"
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Jun 25, 2012, 03:31 PM   #4
MichaelQuick232
Thread Starter
macrumors newbie
 
Join Date: Oct 2011
Quote:
Originally Posted by kryten2 View Post
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?
MichaelQuick232 is offline   0 Reply With Quote
Old Jun 25, 2012, 03:45 PM   #5
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
Turn up your volume and put this as last line :

Code:
say the_temp & "degrees"
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Jun 25, 2012, 04:41 PM   #6
MichaelQuick232
Thread Starter
macrumors newbie
 
Join Date: Oct 2011
Quote:
Originally Posted by kryten2 View Post
Turn up your volume and put this as last line :

Code:
say the_temp & "degrees"

Got it, Thanks!

How do I make it say "Seventy-Nine Point Five" instead of "Seven Nine Dot Five"
MichaelQuick232 is offline   0 Reply With Quote
Old Jun 25, 2012, 05:18 PM   #7
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: 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.
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Jun 25, 2012, 05:30 PM   #8
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by MichaelQuick232 View Post
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":
Code:
say the_temp & " degrees"
Or try getting rid of any ° symbol.
chown33 is offline   0 Reply With Quote
Old Jun 25, 2012, 05:44 PM   #9
MichaelQuick232
Thread Starter
macrumors newbie
 
Join Date: Oct 2011
Quote:
Originally Posted by chown33 View Post
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
MichaelQuick232 is offline   0 Reply With Quote
Old Jun 25, 2012, 05:50 PM   #10
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
Looks like you've found this link useful : http://forums.macrumors.com/showthread.php?t=537002
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Jun 25, 2012, 06:38 PM   #11
MichaelQuick232
Thread Starter
macrumors newbie
 
Join Date: Oct 2011
Quote:
Originally Posted by kryten2 View Post
Looks like you've found this link useful : http://forums.macrumors.com/showthread.php?t=537002
Yes.
MichaelQuick232 is offline   0 Reply With Quote
Old Aug 16, 2012, 12:07 AM   #12
ckeyes888
macrumors newbie
 
Join Date: Aug 2012
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
ckeyes888 is offline   0 Reply With Quote
Old Aug 16, 2012, 08:21 AM   #13
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
Quote:
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.
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Aug 16, 2012, 11:22 AM   #14
ckeyes888
macrumors newbie
 
Join Date: Aug 2012
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
ckeyes888 is offline   0 Reply With Quote
Old Aug 16, 2012, 12:51 PM   #15
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: Belgium
Register for Weather API at http://www.wunderground.com/weather/api/ or http://www.worldweatheronline.com/fr...ther-feed.aspx
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Aug 16, 2012, 01:20 PM   #16
ckeyes888
macrumors newbie
 
Join Date: Aug 2012
Thanks. So I need to register with them to get a temperature for Banff, Canada?

Carl
ckeyes888 is offline   0 Reply With Quote
Old Aug 16, 2012, 03:21 PM   #17
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: 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.
Attached Thumbnails
Click image for larger version

Name:	Picture 2.png
Views:	30
Size:	229.8 KB
ID:	353277   Click image for larger version

Name:	Picture 1.png
Views:	93
Size:	127.0 KB
ID:	353279  
Attached Images
 
__________________
Space Corps Directive 34124

Last edited by kryten2; Aug 16, 2012 at 03:32 PM.
kryten2 is offline   0 Reply With Quote
Old Aug 16, 2012, 09:59 PM   #18
ckeyes888
macrumors newbie
 
Join Date: Aug 2012
Got it. Works perfectly!

Really appreciate all the help.

Thanks,

Carl

Last edited by ckeyes888; Aug 16, 2012 at 10:17 PM.
ckeyes888 is offline   0 Reply With Quote
Old Aug 17, 2012, 01:00 PM   #19
kryten2
macrumors 6502a
 
Join Date: Mar 2012
Location: 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.
__________________
Space Corps Directive 34124
kryten2 is offline   0 Reply With Quote
Old Oct 25, 2012, 12:44 AM   #20
christianboyce
macrumors newbie
 
Join Date: Jun 2007
Another way to do it

I made my own script to read the calendar and blogged about it here:

http://christianboyce.blogspot.com/2...-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.
christianboyce is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
iPhone: Alarm problem... Volume button cancels alarm after jailbreak Lockon Jailbreaks and iOS Hacks 4 Jan 17, 2014 05:49 AM
Timeline Alarm - Quick alarm and timer for iOS7 (universal) maka iPhone and iPod touch Apps 0 Nov 20, 2013 08:33 AM
AppleScript Alarm Clock Broken in Mavericks blahbrah OS X Mavericks (10.9) 0 Oct 28, 2013 09:12 AM
No alarm sound with stock app or iHandy alarm Makaveli6103 iOS 6 0 Apr 14, 2013 11:27 AM
Using iCal as an Alarm via AppleScript hauntvictim OS X 0 Jan 5, 2013 04:12 PM

Forum Jump

All times are GMT -5. The time now is 05:34 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC