Wake Up Alaram - Jarvis-Style

Here is my “Wake Up” alarm that I have set to go off at 8am every workday.


--this requires that you have Location Helper and JSON helper (both of which are free in the AppStore) installed and running
tell application "Location Helper" to launch
tell application "JSON Helper" to launch

--create greeting
set todaysdate to current date
set dateStr to date string of (todaysdate)
set currentHour to hours of (current date)
if currentHour is less than 13 then
	set greeting to "Good Morning Sir."
else if currentHour is less than 18 then
	set greeting to "Good Afternoon Sir."
else
	set greeting to "Good Evening Sir."
end if
if dateStr contains "BIRTHDAY-MONTH BIRTHDAY-DAY" then
	set whatToSay to whatToSay & ".
 " & "Happy Birthday! Sir!"
end if

--GET WEATHER INFO--
set currentLocationJSON to ""
tell application "Location Helper"
	set currentLocationJSON to (reverse geocode location) as list
	--return currentLocationJSON
	tell application "JSON Helper"
		set currentZip to short_name of item 1 of address_components of item 2 of item 1 of currentLocationJSON as number
		--return currentZip
		set currentLat to lat of location of geometry of item 2 of item 1 of currentLocationJSON as string
		--return currentLat
		set currentLong to lng of location of geometry of item 2 of item 1 of currentLocationJSON as string
		--return currentLong
	end tell
	quit
end tell

--YOU NEED TO GET YOUR OWN KEY AT [url=http://HTTP://WWW.WUNDERGROUND.COM/WEATHER/API/?REF=TWC]HTTP://WWW.WUNDERGROUND.COM/WEATHER/API/?REF=TWC[/url]
--IT IS FREE

--this goes and retreives the necessary data to proceed to the next step
set IURL to "http://api.wunderground.com/api/APIKEY/geolookup/q/" & currentZip & ".json"

tell application "JSON Helper"
	set currentLocationList to fetch JSON from IURL as list
	--return currentLocationInfo
	
	set currentCountry to country_iso3166 of location of currentLocationList as string
	--return currentCountry
	set currentState to state of location of currentLocationList as string
	--return currentState
	set currentCity to city of location of currentLocationList as string
	--return currentCity
	
	--make a call to the underground API to retreive weather data
	set CUTURL to "http://api.wunderground.com/api/APIKEY/conditions/q/" & currentCountry & "/" & currentState & "/" & currentCity & ".json"
	
	set currentTempList to fetch JSON from CUTURL as list
	--return currentTempList
	
	--looking for current temperature
	set currentTemp to temp_f of current_observation of currentTempList as string
	--return currentTemp
	
	--looking for today forecast
	set FORURL to "http://api.wunderground.com/api/APIKEY/forecast/q/" & currentCountry & "/" & currentState & "/" & currentCity & ".json"
	
	set todayForecastList to fetch JSON from FORURL as list
	--return todayForecastList
	
	--get current conditions
	set currentWeatherConditions to conditions of item 1 of forecastday of simpleforecast of forecast of todayForecastList as string
	--return currentWeatherConditions
	
	--get forecast description
	set currentForecastDescriptionHigh to fahrenheit of high of item 1 of forecastday of simpleforecast of forecast of todayForecastList as number
	--return currentForecastDescriptionHigh
	
	set currentForecastDescriptionLow to fahrenheit of low of item 2 of forecastday of simpleforecast of forecast of todayForecastList as number
	--return currentForecastDescriptionLow
	
end tell
tell application "JSON Helper" to quit

--I use this to retrieve my current location's WOEID
set WOEIDURL to "http://woeid.rosselliot.co.nz/lookup/" & currentCity & "%20" & currentState & "%20" & currentCountry & "%20" & currentZip

set startHere to "United States" & return
set endHere to return & "Place Info" & return
set parseHTML to (do shell script "curl " & WOEIDURL & " | textutil -stdin -stdout -format html -convert txt -encoding UTF-8 ")

--return parseHTML
set blurb0 to parseHTML
set TID to text item delimiters
set text item delimiters to startHere
set blurb1 to text item 2 of blurb0
set text item delimiters to TID
set CityCode to paragraph 1 of blurb1
--return CityCode

set WURL to "http://weather.yahooapis.com/forecastrss?w=" & CityCode

set t_format to "F"
set v_format to "S"
set a_format to "Y"

set file_content to (do shell script "curl " & WURL)

set theText to text ((offset of "yweather:condition" in file_content) + 1) thru -1 of file_content
set sub_1 to text ((offset of "\"" in theText) + 1) thru -1 of theText
set actual_condition to text 1 thru ((offset of "\"" in sub_1) - 1) of sub_1

set sub_1a to text ((offset of "temp=" in sub_1)) thru -1 of sub_1
set sub_1b to text ((offset of "\"" in sub_1a) + 1) thru -1 of sub_1a
set actual_temp to text 1 thru ((offset of "\"" in sub_1b) - 1) of sub_1b

if t_format is equal to "C" then
	set actual_temp to (5 / 9) * (actual_temp - 32) as integer
end if
set theText to text ((offset of "yweather:forecast" in file_content) + 1) thru -1 of file_content
set sub_2 to text ((offset of "\"" in theText) + 1) thru -1 of theText

--says the weather
set whatToSay to greeting & " The current time is " & getTimeInHoursAndMinutes() & ".. The current weather in " & currentCity & " is " & currentTemp & " degrees. With a low of " & currentForecastDescriptionLow & " and a high of " & currentForecastDescriptionHigh & " today.."
if a_format is equal to "Y" then
	set whatToSay to whatToSay & " The surface conditions are " & actual_condition & ".."
end if


-- APPSTORE UPDATES --
set the plistfile_path to ("~/Library/Preferences/com.apple.appstore.plist")
set numberUpdates to 0
tell application "System Events"
	set p_list to property list file (plistfile_path)
	set numberUpdates to (count value of property list item "availableUpdates" of p_list)
	if numberUpdates is 1 then
		set whatToSay to whatToSay & ".
 				" & " There is only " & numberUpdates & " App Store Update.."
	else if numberUpdates is greater than 1 then
		set whatToSay to whatToSay & ".
 				" & " There are " & numberUpdates & " App Store Updates.."
	else
		set whatToSay to whatToSay & ".
 				" & " There are no App Store Updates.."
	end if
end tell
--CALENDAR EVENTS--
set {year:y, month:m, day:d} to todaysdate
set str to (m as string) & " " & (d as string) & " " & (y as string)
set today to date str
set tomorrow to today + 60 * 60 * 24
log today & tomorrow
tell application "Calendar"
	activate
	count every window
	set windowMiniaturized to get miniaturized of item 1 of every window
	if windowMiniaturized = false then
		set miniaturized of item 1 of every window to true
	end if
	set allCalendar to every calendar --whose name is not "CALENDAR-NAME-TO-EXCLUDE" and name is not "CALENDAR-NAME-TO-EXCLUDE" and name is not "CALENDAR-NAME-TO-EXCLUDE"
	set eventsNumber to 0
	repeat with eachCalendar in allCalendar
		set curr to (every event whose start date is greater than or equal to today and start date is less than tomorrow) of eachCalendar
		set eventsNumber to eventsNumber + (count curr)
		repeat with eachCurr in curr
			if (allday event of eachCurr) then
				set whatToSay to whatToSay & ".
						 " & " Today is " & summary of eachCurr & ".."
			else
				set eventDate to start date of eachCurr
				set eventTime to time string of eventDate
				set whatToSay to whatToSay & ".
						 " & " Today you have " & summary of eachCurr & " at " & eventTime & ".."
			end if
		end repeat
	end repeat
	if eventsNumber is 0 then
		set whatToSay to whatToSay & ".
				 " & " There is nothing on your calendar for today.."
	end if
end tell
set List_Mail to ""
--GET MAIL--
tell application "Mail"
	--find all of the unread emails
	set theMessages to (messages of inbox whose read status is false)
	--set message_count to the total number of unread messages
	set message_count to number of items in theMessages
	--if there is only one unread email
	if message_count = 1 then
		--append the following line to List_Mail
		set List_Mail to List_Mail & ", you have an email" & return
		--loop through the number of items in the var theMessages
		repeat with i from 1 to number of items in theMessages
			--set thisMessage to a specific message
			set thisMessage to item i of theMessages
			--extract the name of the sender and store it in the_sender
			set the_sender to extract name from (sender of thisMessage as string)
			--extract the subject line and store it in th_subject
			try
				--set the subject line of the email
				set the_subject to (subject of thisMessage as string)
				--if the subject line is blank
				if the_subject is "" then error
				-- error out if subject line is blank
			on error
				--create subject lin	e
				set the_subject to "No Subject"
			end try
			--append the_sender and the_subject of thisMessage to List_Mail 
			set List_Mail to List_Mail & ", Message from: " & the_sender & ", Subject: " & the_subject & "." & return
		end repeat
		--if there is more than one unread email
	else if message_count > 1 then
		--append the following line to List_Mail
		-- use unred to make JARVIS sound more natural
		set List_Mail to List_Mail & ", There are, " & message_count & ", unred emails" & return
		repeat with i from 1 to number of items in theMessages
			set thisMessage to item i of theMessages
			set the_sender to extract name from (sender of thisMessage as string)
			try
				set the_subject to (subject of thisMessage as string)
				if the_subject is "" then error
			on error
				set the_subject to "No Subject"
			end try
			set List_Mail to List_Mail & ", Message from: " & the_sender & ", Subject: " & the_subject & "." & return
		end repeat
		--if there are no unread emails
	else
		--append this to List_Mail
		set List_Mail to List_Mail & ", There are, " & message_count & ", unred emails." & return
	end if
end tell
--CONTACTS INFO--
tell application "Contacts"
	activate
	count every window
	set windowMiniaturized to get miniaturized of item 1 of every window
	if windowMiniaturized = false then
		set miniaturized of item 1 of every window to true
	end if
	set allContacts to every person whose (birth date is not missing value)
	repeat with eachContact in allContacts
		set {month:bdayM, day:bdayD} to birth date of eachContact
		if bdayM is equal to m and bdayD is equal to d then
			set firstName to first name of eachContact
			set lastName to last name of eachContact
			set whatToSay to whatToSay & ".
 " & "Today is " & firstName & " " & lastName & "'s birthday.."
		end if
	end repeat
end tell
tell application "Contacts" to quit

--START ALARM--
tell application "iTunes"
	activate
	set the sound volume to 40
	play track "alarm" of playlist "Alarm"
end tell
delay 5
say whatToSay
delay 5
say List_Mail
delay 30
--say "I am supposed to remind you about:"
--says notes
--say (do shell script "cat '" & "/Users/USERNAME/Documents/PATH/TO/FILE.txt" & "'")"

tell application "iTunes" to activate

on getTimeInHoursAndMinutes()
	-- Get the "hour"
	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
	
	-- Get the "minute"
	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 getTimeInHoursAndMinutes

This looks like it would mesh really well with one of my scripts that ensures I have enough time between waking up and the first appointment of my day. I’m sure I left it lying around online somewhere…

http://macscripter.net/viewtopic.php?id=34538

Old version, I may pull out the latest version if you’re interested, but should have the main functionality at the top of the idle loop of the main script.

I have reconfigured part of the script following

tell application "JSON Helper" 
set API_Key to "xyz"

and then set the IURL to the following:

set IURL to "http://api.wunderground.com/api/" & API_Key & "/geolookup/q/" & currentZip & ".json"

The fetch JSON from commands retrieves information correctly.
However when I set

set CUTURL to "http://api.wunderground.com/api/" & API_Key & "/conditions/q/" & currentCountry & "/" & currentState & "/" & currentCity & ".json"

although

open location CUTURL 

works

set currentTempList to fetch JSON from CUTURL as list  

does not.
The fetch JSON retrieves nothing. What might I do to enable fetch JSON to fetch correctly?

Browser: Safari 537.59
Operating System: Mac OS X (10.8)