Why won't this code work?

on start_sleep_timer()
	set startTime to time of (current date)
end start_sleep_timer

on end_sleep_timer()
	set endTime to time of (current date)
end end_sleep_timer

on get_sleep_timer()
	set sleepHours to (endTime - startTime / 3600)
	return sleepHours
end get_sleep_timer

on weather(weatherconditionscontrol)
	--this is the city code. Search the code for your city on http://weather.yahoo.com/
	
	set CityCode to 11050
	
	--temperature format
	
	set t_format to "F"
	
	--voiceover format
	
	set v_format to "A"
	
	--say present condition
	
	set a_format to "Y"
	
	
	
	set IURL to "http://weather.yahooapis.com/forecastrss?w=" & CityCode
	
	
	
	--downloading the file using curl
	
	set file_content to (do shell script "curl " & IURL)
	
	--looking for the line with actual condition
	
	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
	
	
	
	--today conditions found
	
	set actual_condition to text 1 thru ((offset of "\"" in sub_1) - 1) of sub_1
	
	
	
	--looking for actual temperature temperature
	
	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
	
	
	
	--looking for today forecast
	
	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
	
	
	
	--maximum and minimum temperatures found
	
	set today_min_temp to word 9 of sub_2
	
	set today_max_temp to word 12 of sub_2
	
	if t_format is equal to "C" then
		
		set today_min_temp to (5 / 9) * (today_min_temp - 32) as integer
		
		set today_max_temp to (5 / 9) * (today_max_temp - 32) as integer
		
	end if
	
	
	
	--looking for today forecast condition (a bit tricky)
	
	set sub_3 to text ((offset of "text" in sub_2) + 1) thru -1 of sub_2
	
	set sub_4 to text ((offset of "\"" in sub_3) + 1) thru -1 of sub_3
	
	set today_forecast to text 1 thru ((offset of "\"" in sub_4) - 1) of sub_4
	
	
	
	--looking for tomorrow forecast
	
	set sub_5 to text ((offset of "yweather:forecast" in sub_4) + 1) thru -1 of sub_4
	
	set sub_6 to text ((offset of "\"" in sub_5) + 1) thru -1 of sub_5
	
	
	
	--maximum and minimum temperatures found
	
	set tomorrow_min_temp to word 9 of sub_6
	
	set tomorrow_max_temp to word 12 of sub_6
	
	if t_format is equal to "C" then
		
		set tomorrow_min_temp to (5 / 9) * (tomorrow_min_temp - 32) as integer
		
		set tomorrow_max_temp to (5 / 9) * (tomorrow_max_temp - 32) as integer
		
	end if
	
	
	
	--looking for tomorrow forecast condition (a bit tricky)
	
	set sub_7 to text ((offset of "text" in sub_6) + 1) thru -1 of sub_6
	
	set sub_8 to text ((offset of "\"" in sub_7) + 1) thru -1 of sub_7
	
	set tomorrow_forecast to text 1 thru ((offset of "\"" in sub_8) - 1) of sub_8
	
	
	if weatherconditionscontrol is "today" then
		
		return "Today the forecast is " & today_forecast & ".  Temperature: between " & today_min_temp & " and " & today_max_temp & " degrees. " & "The current temperature is " & actual_temp & " degrees. "
		
	else if weatherconditionscontrol is "tommorow" then
		
		return "Tomorrow the forecast will be " & tomorrow_forecast & ". Temperature: between " & tomorrow_min_temp & " and " & tomorrow_max_temp & " degrees"
		
		
	end if
	
	
end weather

on getlasttweet(username)
	set myURL to "http://twitter.com/" & username
	set content to do shell script "curl " & myURL
	if content is not "<html><body>You are being <a href=\"http://twitter.com/suspended\">redirected</a>.</body></html>" and content does not contain "<h2>Sorry, that page doesn't exist!</h2>" and username is not "login" and username is not "home" then
		if content contains "<a class=\"entry-date\" rel=\"bookmark\" href=\"" then
			set text item delimiters to "<a class=\"entry-date\" rel=\"bookmark\" href=\""
			set content to text item 2 of content
			set text item delimiters to "\">"
			set content to text item 1 of content
			set lasttweet to do shell script "curl " & content & " | textutil -stdin -stdout -format html -convert txt -encoding UTF-8 "
			set text item delimiters to "Login Join Twitter!

"
			set lasttweet to text item 2 of lasttweet
			set text item delimiters to "
"
			set lasttweet to text item 1 of lasttweet
		else
			return ""
		end if
	else
		display dialog "This username does not exist or is not available."
	end if
end getlasttweet


using terms from application "iChat"
	
	on message received theMessage from theBuddy for theChat
		
		
		if theMessage is "current weather" then
			send weather("today") to theBuddy
			
		else if theMessage is "tommorow's weather" then
			send weather("tommorow") to theBuddy
			
		else if theMessage is "turn on bedlamp" then
			tell application "XTension"
				activate
				turnon "elijahroombedlamp"
			end tell
			send "Bedlamp has been turned on" to theBuddy
			
		else if theMessage is "turn off bedlamp" then
			tell application "XTension"
				activate
				turnoff "elijahroombedlamp"
			end tell
			send "Bedlamp has been turned off" to theBuddy
			
		else if theMessage is "turn on overheadlights" then
			tell application "XTension"
				activate
				turnon "elijahroomoverheadlights"
			end tell
			send "Overheadlights have been turned on" to theBuddy
			
		else if theMessage is "turn off overheadlights" then
			tell application "XTension"
				activate
				turnoff "elijahroomoverheadlights"
			end tell
			send "Overheadlights have been turned off" to theBuddy
			
		else if theMessage is "turn on fishtanklight" then
			tell application "XTension"
				activate
				turnon "elijahroomfishtanklight"
			end tell
			send "FishTankLight has been turned on" to theBuddy
			
		else if theMessage is "turn off fishtanklight" then
			tell application "XTension"
				activate
				turnoff "elijahroomfishtanklight"
			end tell
			send "FishTankLight has been turned off" to theBuddy
			
		else if theMessage is "feed fish" then
			do shell script "/Users/drummerboyx/Library/Scripts/arduino-serial -b 9600 -p /dev/tty.usbserial-A800ev0Z -s 1"
			
			send "Fish Have been feed " to theBuddy
			
		else if theMessage is "CNN Update" then
			send "Loading..." to theBuddy
			send getlasttweet("cnn") to theBuddy
			
		else if theMessage is "Weather Update" then
			send "Loading..." to theBuddy
			send getlasttweet("weather") to theBuddy
			
		else if theMessage is "Project Jarvis update" then
			send "Loading..." to theBuddy
			send getlasttweet("cbarraford") to theBuddy
			
		else if theMessage is "the onion update" then
			send "Loading..." to theBuddy
			send getlasttweet("theonion") to theBuddy
			
		else if theMessage is "disable recognition" then
			tell application "System Preferences"
				activate
				set current pane to pane "com.apple.preference.sound"
				reveal (first anchor of current pane whose name is "input")
			end tell
			
			tell application "System Events"
				
				launch
				tell process "System Preferences"
					set value of slider 1 of group 2 of tab group 1 of window 1 to 0
				end tell
			end tell
			tell application "System Preferences"
				quit
			end tell
			
			send "Speech recognition disabled" to theBuddy
			say "speech recognition disabled"
			beep 3
			
			
		else if theMessage is "enable recognition" then
			tell application "System Preferences"
				activate
				set current pane to pane "com.apple.preference.sound"
				reveal (first anchor of current pane whose name is "input")
			end tell
			
			tell application "System Events"
				
				launch
				tell process "System Preferences"
					set value of slider 1 of group 2 of tab group 1 of window 1 to 1
				end tell
			end tell
			tell application "System Preferences"
				quit
			end tell
			
			send "Speech recognition enabled" to theBuddy
			say "speech recognition, enabled"
			beep 3
			
		else if theMessage is "disable speech" then
			say "speech, disabled"
			beep 3
			set volume output volume 0
			send "Speech disabled" to theBuddy
			
		else if theMessage is "enable speech" then
			set volume output volume 100
			beep 3
			say "speech, enabled"
			send "Speech enabled" to theBuddy
			
		else if theMessage is "goodnight" then
			start_sleep_timer()
			beep 3
			say "speech, disabled"
			set volume output volume 0
			tell application "System Preferences"
				activate
				set current pane to pane "com.apple.preference.sound"
				reveal (first anchor of current pane whose name is "input")
			end tell
			
			tell application "System Events"
				
				launch
				tell process "System Preferences"
					set value of slider 1 of group 2 of tab group 1 of window 1 to 0
				end tell
			end tell
			tell application "System Preferences"
				quit
			end tell
			send "Speech disabled, Goodnight Elijah!" to theBuddy
			tell application "XTension"
				turnoff "Elijah's Room"
			end tell
			
		else if theMessage is "goodmorning" then
			end_sleep_timer()
			set volume output volume 100
			beep 3
			say "speech, enabled"
			tell application "System Preferences"
				activate
				set current pane to pane "com.apple.preference.sound"
				reveal (first anchor of current pane whose name is "input")
			end tell
			
			tell application "System Events"
				
				launch
				tell process "System Preferences"
					set value of slider 1 of group 2 of tab group 1 of window 1 to 100
				end tell
			end tell
			tell application "System Preferences"
				quit
			end tell
			send "Speech enabled, Goodmorning Elijah! You've been sleeping for " & get_sleep_timer() & " hours." to theBuddy
			say "Good Morning Elijah!" & "You've been sleeping for " & get_sleep_timer() & "hours"
			
		else if theMessage is "hey" then
			send "Hi!" to theBuddy
			
		else if theMessage is "hi" then
			send "Hey!" to theBuddy
			
		else if theMessage is "hello" then
			send "Hey!" to theBuddy
			
		else if theMessage is "thanks" then
			send "Your welcome sir." to theBuddy
			
		else if theMessage is "help" then
			send "disable recognition, enable recognition, disable speech, enable speech, current weather, tommorow's weather, turn on, turn off,  feed fish, cnn update, weather update, project jarvis update, the onion update, goodmorning, goodnight" to theBuddy
			
		else
			send "???" to theBuddy
		end if
	end message received
	
end using terms from

When I send goodmorning over IM after I send goodnight, I get this error:
Event: Message Received
File: ICHAT.scpt
Error: The variable endTime is not defined.

I have no idea why…I did declare endTime, but for some reason it’s not recognizing it…

Those variables are not global. Try adding “global endTime, startTime, …” to the beginning of the script.

Anyways, this looks AWESOME. I want to have your skills. Especially your arduino skills.

Thanks! I added the globals:


global endTime
global startTime

on start_sleep_timer()
	set startTime to time of (current date)
end start_sleep_timer

on end_sleep_timer()
	set endTime to time of (current date)
end end_sleep_timer

on get_sleep_timer()
	set sleepHours to (endTime - startTime / 3600)
	return sleepHours
end get_sleep_timer

on weather(weatherconditionscontrol)
	--this is the city code. Search the code for your city on http://weather.yahoo.com/
	
	set CityCode to 11050
	
	--temperature format
	
	set t_format to "F"
	
	--voiceover format
	
	set v_format to "A"
	
	--say present condition
	
	set a_format to "Y"
	
	
	
	set IURL to "http://weather.yahooapis.com/forecastrss?w=" & CityCode
	
	
	
	--downloading the file using curl
	
	set file_content to (do shell script "curl " & IURL)
	
	--looking for the line with actual condition
	
	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
	
	
	
	--today conditions found
	
	set actual_condition to text 1 thru ((offset of "\"" in sub_1) - 1) of sub_1
	
	
	
	--looking for actual temperature temperature
	
	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
	
	
	
	--looking for today forecast
	
	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
	
	
	
	--maximum and minimum temperatures found
	
	set today_min_temp to word 9 of sub_2
	
	set today_max_temp to word 12 of sub_2
	
	if t_format is equal to "C" then
		
		set today_min_temp to (5 / 9) * (today_min_temp - 32) as integer
		
		set today_max_temp to (5 / 9) * (today_max_temp - 32) as integer
		
	end if
	
	
	
	--looking for today forecast condition (a bit tricky)
	
	set sub_3 to text ((offset of "text" in sub_2) + 1) thru -1 of sub_2
	
	set sub_4 to text ((offset of "\"" in sub_3) + 1) thru -1 of sub_3
	
	set today_forecast to text 1 thru ((offset of "\"" in sub_4) - 1) of sub_4
	
	
	
	--looking for tomorrow forecast
	
	set sub_5 to text ((offset of "yweather:forecast" in sub_4) + 1) thru -1 of sub_4
	
	set sub_6 to text ((offset of "\"" in sub_5) + 1) thru -1 of sub_5
	
	
	
	--maximum and minimum temperatures found
	
	set tomorrow_min_temp to word 9 of sub_6
	
	set tomorrow_max_temp to word 12 of sub_6
	
	if t_format is equal to "C" then
		
		set tomorrow_min_temp to (5 / 9) * (tomorrow_min_temp - 32) as integer
		
		set tomorrow_max_temp to (5 / 9) * (tomorrow_max_temp - 32) as integer
		
	end if
	
	
	
	--looking for tomorrow forecast condition (a bit tricky)
	
	set sub_7 to text ((offset of "text" in sub_6) + 1) thru -1 of sub_6
	
	set sub_8 to text ((offset of "\"" in sub_7) + 1) thru -1 of sub_7
	
	set tomorrow_forecast to text 1 thru ((offset of "\"" in sub_8) - 1) of sub_8
	
	
	if weatherconditionscontrol is "today" then
		
		return "Today the forecast is " & today_forecast & ".  Temperature: between " & today_min_temp & " and " & today_max_temp & " degrees. " & "The current temperature is " & actual_temp & " degrees. "
		
	else if weatherconditionscontrol is "tommorow" then
		
		return "Tomorrow the forecast will be " & tomorrow_forecast & ". Temperature: between " & tomorrow_min_temp & " and " & tomorrow_max_temp & " degrees"
		
		
	end if
	
	
end weather

on getlasttweet(username)
	set myURL to "http://twitter.com/" & username
	set content to do shell script "curl " & myURL
	if content is not "<html><body>You are being <a href=\"http://twitter.com/suspended\">redirected</a>.</body></html>" and content does not contain "<h2>Sorry, that page doesn't exist!</h2>" and username is not "login" and username is not "home" then
		if content contains "<a class=\"entry-date\" rel=\"bookmark\" href=\"" then
			set text item delimiters to "<a class=\"entry-date\" rel=\"bookmark\" href=\""
			set content to text item 2 of content
			set text item delimiters to "\">"
			set content to text item 1 of content
			set lasttweet to do shell script "curl " & content & " | textutil -stdin -stdout -format html -convert txt -encoding UTF-8 "
			set text item delimiters to "Login Join Twitter!

"
			set lasttweet to text item 2 of lasttweet
			set text item delimiters to "
"
			set lasttweet to text item 1 of lasttweet
		else
			return ""
		end if
	else
		display dialog "This username does not exist or is not available."
	end if
end getlasttweet


using terms from application "iChat"
	
	on message received theMessage from theBuddy for theChat
		
		
		if theMessage is "current weather" then
			send weather("today") to theBuddy
			
		else if theMessage is "tommorow's weather" then
			send weather("tommorow") to theBuddy
			
		else if theMessage is "turn on bedlamp" then
			tell application "XTension"
				activate
				turnon "elijahroombedlamp"
			end tell
			send "Bedlamp has been turned on" to theBuddy
			
		else if theMessage is "turn off bedlamp" then
			tell application "XTension"
				activate
				turnoff "elijahroombedlamp"
			end tell
			send "Bedlamp has been turned off" to theBuddy
			
		else if theMessage is "turn on overheadlights" then
			tell application "XTension"
				activate
				turnon "elijahroomoverheadlights"
			end tell
			send "Overheadlights have been turned on" to theBuddy
			
		else if theMessage is "turn off overheadlights" then
			tell application "XTension"
				activate
				turnoff "elijahroomoverheadlights"
			end tell
			send "Overheadlights have been turned off" to theBuddy
			
		else if theMessage is "turn on fishtanklight" then
			tell application "XTension"
				activate
				turnon "elijahroomfishtanklight"
			end tell
			send "FishTankLight has been turned on" to theBuddy
			
		else if theMessage is "turn off fishtanklight" then
			tell application "XTension"
				activate
				turnoff "elijahroomfishtanklight"
			end tell
			send "FishTankLight has been turned off" to theBuddy
			
		else if theMessage is "feed fish" then
			do shell script "/Users/drummerboyx/Library/Scripts/arduino-serial -b 9600 -p /dev/tty.usbserial-A800ev0Z -s 1"
			
			send "Fish Have been feed " to theBuddy
			
		else if theMessage is "CNN Update" then
			send "Loading..." to theBuddy
			send getlasttweet("cnn") to theBuddy
			
		else if theMessage is "Weather Update" then
			send "Loading..." to theBuddy
			send getlasttweet("weather") to theBuddy
			
		else if theMessage is "Project Jarvis update" then
			send "Loading..." to theBuddy
			send getlasttweet("cbarraford") to theBuddy
			
		else if theMessage is "the onion update" then
			send "Loading..." to theBuddy
			send getlasttweet("theonion") to theBuddy
			
		else if theMessage is "disable recognition" then
			tell application "System Preferences"
				activate
				set current pane to pane "com.apple.preference.sound"
				reveal (first anchor of current pane whose name is "input")
			end tell
			
			tell application "System Events"
				
				launch
				tell process "System Preferences"
					set value of slider 1 of group 2 of tab group 1 of window 1 to 0
				end tell
			end tell
			tell application "System Preferences"
				quit
			end tell
			
			send "Speech recognition disabled" to theBuddy
			say "speech recognition disabled"
			beep 3
			
			
		else if theMessage is "enable recognition" then
			tell application "System Preferences"
				activate
				set current pane to pane "com.apple.preference.sound"
				reveal (first anchor of current pane whose name is "input")
			end tell
			
			tell application "System Events"
				
				launch
				tell process "System Preferences"
					set value of slider 1 of group 2 of tab group 1 of window 1 to 1
				end tell
			end tell
			tell application "System Preferences"
				quit
			end tell
			
			send "Speech recognition enabled" to theBuddy
			say "speech recognition, enabled"
			beep 3
			
		else if theMessage is "disable speech" then
			say "speech, disabled"
			beep 3
			set volume output volume 0
			send "Speech disabled" to theBuddy
			
		else if theMessage is "enable speech" then
			set volume output volume 100
			beep 3
			say "speech, enabled"
			send "Speech enabled" to theBuddy
			
		else if theMessage is "goodnight" then
			start_sleep_timer()
			beep 3
			say "speech, disabled"
			set volume output volume 0
			tell application "System Preferences"
				activate
				set current pane to pane "com.apple.preference.sound"
				reveal (first anchor of current pane whose name is "input")
			end tell
			
			tell application "System Events"
				
				launch
				tell process "System Preferences"
					set value of slider 1 of group 2 of tab group 1 of window 1 to 0
				end tell
			end tell
			tell application "System Preferences"
				quit
			end tell
			send "Speech disabled, Goodnight Elijah!" to theBuddy
			tell application "XTension"
				turnoff "Elijah's Room"
			end tell
			
		else if theMessage is "goodmorning" then
			end_sleep_timer()
			set volume output volume 100
			beep 3
			say "speech, enabled"
			tell application "System Preferences"
				activate
				set current pane to pane "com.apple.preference.sound"
				reveal (first anchor of current pane whose name is "input")
			end tell
			
			tell application "System Events"
				
				launch
				tell process "System Preferences"
					set value of slider 1 of group 2 of tab group 1 of window 1 to 100
				end tell
			end tell
			tell application "System Preferences"
				quit
			end tell
			send "Speech enabled, Goodmorning Elijah! You've been sleeping for " & get_sleep_timer() & " hours." to theBuddy
			say "Good Morning Elijah!" & "You've been sleeping for " & get_sleep_timer() & "hours"
			
		else if theMessage is "hey" then
			send "Hi!" to theBuddy
			
		else if theMessage is "hi" then
			send "Hey!" to theBuddy
			
		else if theMessage is "hello" then
			send "Hey!" to theBuddy
			
		else if theMessage is "thanks" then
			send "Your welcome sir." to theBuddy
			
		else if theMessage is "help" then
			send "disable recognition, enable recognition, disable speech, enable speech, current weather, tommorow's weather, turn on, turn off,  feed fish, cnn update, weather update, project jarvis update, the onion update, goodmorning, goodnight" to theBuddy
			
		else
			send "???" to theBuddy
		end if
	end message received
	
end using terms from

Get this error:
Event: Message Received
File: ICHAT.scpt
Error: The variable startTime is not defined.

Yep! Been working on the code for a little bit, it’s coming along quite nice! Any suggestions would be appreciated! (-:

Try

set startTime to missing value
set endTime to missing value
global endTime, startTime

Get this error:
Event: Message Received
File: ICHAT.scpt
Error: Can’t make missing value into type real.

)-:

Replace missing value with “”.

Wait! Add the lines I gave you into the “on message received” handlers.

Same error as the first time… )-: I’m gonna keep messing around with the placement…

YIPPEE!!! GOT IT!

Only, it gives me this weird number when I test it:
Speech enabled, Goodmorning Elijah! You’ve been sleeping for 7.5081E+4 hours

Round the value using the “round” command.

but the difference between the two commands was only about 10 seconds.

Hi.

Excuse me butting in on my way through. :wink:

This apparently works with run-handler variables, but it’s more normal and logical to declare globals or locals before assigning values to them:

global endTime, startTime

set startTime to missing value¨
set endTime to missing value
  1. Because of “operator precedence”, (endTime - startTime / 3600) in the last handler above is the same as endTime - (startTime / 3600). This’ll be why you’re getting your overlarge result. You need (endTime - startTime) / 3600.

  2. If sleep starts in the evening and ends the following morning, you’ll end up with a negative result. It’s better to leave out ‘time of’':

on start_sleep_timer()
	set startTime to (current date)
end start_sleep_timer

on end_sleep_timer()
	set endTime to (current date)
end end_sleep_timer

on get_sleep_timer()
	set sleepHours to (endTime - startTime) / hours
	return sleepHours
end get_sleep_timer

startTime and endTime will then have date values instead of integer values, but you’ll get the right results over midnight and with periods of more than 24 hours.

Now I get this error:

Event: Message Received
File: ICHAT.scpt
Error: Can’t make “” into type number or date.

I don’t know how you’re running the script, but I can’t see any provision in it for ensuring that start_sleep_timer(), end_sleep_timer(), and get_sleep_timer() are executed in the right order. If they are, the error you’re getting (if it’s the result of my previous suggestion) won’t occur.

The timer handlers themselves should check that they’re in the right sequence. Instead of initialising startTime and endTime to “” every time the script runs, rearrange the top of it something like this:

property startTime : missing value
property endTime : missing value

on start_sleep_timer()
	-- (Re)initialise both startTime and endTime for a new sleep period.
	set startTime to (current date)
	set endTime to missing value
end start_sleep_timer

on end_sleep_timer()
	-- Only set the end time if the start time's been set.
	if (startTime is not missing value) then set endTime to (current date)
end end_sleep_timer

on get_sleep_timer()
	if (startTime is missing value) or (endTime is missing value) then
		-- The sleep timer either hasn't been started or hasn't been stopped.
		-- Rewrite this bit to do what you want under these circumstances.
		set sleepHours to 0
	else
		-- The time variables have both been set.
		set sleepHours to (endTime - startTime) / hours
	end if
	-- Zap both time values.
	set startTime to missing value
	set endTime to missing value
	
	return sleepHours
end get_sleep_timer

facepalm
I forgot all about properties. It even works between runs. :stuck_out_tongue:

Now this error:

Event: Message Received
File: ICHAT.scpt
Error: Can’t make date “Wednesday, May 19, 2010 4:00:52 PM” into type real.

Post your script.

Accually, I just figured it out! I had to include the “to time of (current date)” instead of just (current date)

Now the problem is when I send goodnight, and then goodmorning, (about 10 seconds apart) it responds to me saying i was sleeping for 16 hours, and the number won’t change.

Here’s my code:

property startTime : missing value
property endTime : missing value

on start_sleep_timer()
	-- (Re)initialise both startTime and endTime for a new sleep period.
	set startTime to time of (current date)
	set endTime to missing value
end start_sleep_timer

on end_sleep_timer()
	-- Only set the end time if the start time's been set.
	if (startTime is not missing value) then set endTime to time of (current date)
end end_sleep_timer

on get_sleep_timer()
	if (startTime is missing value) or (endTime is missing value) then
		-- The sleep timer either hasn't been started or hasn't been stopped.
		-- Rewrite this bit to do what you want under these circumstances.
		set sleepHours to 0
	else
		-- The time variables have both been set.
		set sleepHours to round ((endTime - startTime) / hours)
	end if
	-- Zap both time values.
	set startTime to missing value
	set endTime to missing value
	
	return sleepHours
end get_sleep_timer

on weather(weatherconditionscontrol)
	--this is the city code. Search the code for your city on http://weather.yahoo.com/
	
	set CityCode to 11050
	
	--temperature format
	
	set t_format to "F"
	
	--voiceover format
	
	set v_format to "A"
	
	--say present condition
	
	set a_format to "Y"
	
	
	
	set IURL to "http://weather.yahooapis.com/forecastrss?w=" & CityCode
	
	
	
	--downloading the file using curl
	
	set file_content to (do shell script "curl " & IURL)
	
	--looking for the line with actual condition
	
	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
	
	
	
	--today conditions found
	
	set actual_condition to text 1 thru ((offset of "\"" in sub_1) - 1) of sub_1
	
	
	
	--looking for actual temperature temperature
	
	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
	
	
	
	--looking for today forecast
	
	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
	
	
	
	--maximum and minimum temperatures found
	
	set today_min_temp to word 9 of sub_2
	
	set today_max_temp to word 12 of sub_2
	
	if t_format is equal to "C" then
		
		set today_min_temp to (5 / 9) * (today_min_temp - 32) as integer
		
		set today_max_temp to (5 / 9) * (today_max_temp - 32) as integer
		
	end if
	
	
	
	--looking for today forecast condition (a bit tricky)
	
	set sub_3 to text ((offset of "text" in sub_2) + 1) thru -1 of sub_2
	
	set sub_4 to text ((offset of "\"" in sub_3) + 1) thru -1 of sub_3
	
	set today_forecast to text 1 thru ((offset of "\"" in sub_4) - 1) of sub_4
	
	
	
	--looking for tomorrow forecast
	
	set sub_5 to text ((offset of "yweather:forecast" in sub_4) + 1) thru -1 of sub_4
	
	set sub_6 to text ((offset of "\"" in sub_5) + 1) thru -1 of sub_5
	
	
	
	--maximum and minimum temperatures found
	
	set tomorrow_min_temp to word 9 of sub_6
	
	set tomorrow_max_temp to word 12 of sub_6
	
	if t_format is equal to "C" then
		
		set tomorrow_min_temp to (5 / 9) * (tomorrow_min_temp - 32) as integer
		
		set tomorrow_max_temp to (5 / 9) * (tomorrow_max_temp - 32) as integer
		
	end if
	
	
	
	--looking for tomorrow forecast condition (a bit tricky)
	
	set sub_7 to text ((offset of "text" in sub_6) + 1) thru -1 of sub_6
	
	set sub_8 to text ((offset of "\"" in sub_7) + 1) thru -1 of sub_7
	
	set tomorrow_forecast to text 1 thru ((offset of "\"" in sub_8) - 1) of sub_8
	
	
	if weatherconditionscontrol is "today" then
		
		return "Today the forecast is " & today_forecast & ".  Temperature: between " & today_min_temp & " and " & today_max_temp & " degrees. " & "The current temperature is " & actual_temp & " degrees. "
		
	else if weatherconditionscontrol is "tommorow" then
		
		return "Tomorrow the forecast will be " & tomorrow_forecast & ". Temperature: between " & tomorrow_min_temp & " and " & tomorrow_max_temp & " degrees"
		
		
	end if
	
	
end weather

on getlasttweet(username)
	set myURL to "http://twitter.com/" & username
	set content to do shell script "curl " & myURL
	if content is not "<html><body>You are being <a href=\"http://twitter.com/suspended\">redirected</a>.</body></html>" and content does not contain "<h2>Sorry, that page doesn't exist!</h2>" and username is not "login" and username is not "home" then
		if content contains "<a class=\"entry-date\" rel=\"bookmark\" href=\"" then
			set text item delimiters to "<a class=\"entry-date\" rel=\"bookmark\" href=\""
			set content to text item 2 of content
			set text item delimiters to "\">"
			set content to text item 1 of content
			set lasttweet to do shell script "curl " & content & " | textutil -stdin -stdout -format html -convert txt -encoding UTF-8 "
			set text item delimiters to "Login Join Twitter!

"
			set lasttweet to text item 2 of lasttweet
			set text item delimiters to "
"
			set lasttweet to text item 1 of lasttweet
		else
			return ""
		end if
	else
		display dialog "This username does not exist or is not available."
	end if
end getlasttweet


using terms from application "iChat"
	
	on message received theMessage from theBuddy for theChat
		
		global endTime, startTime
		set startTime to ""
		set endTime to ""
		
		if theMessage is "current weather" then
			send weather("today") to theBuddy
			
		else if theMessage is "tommorow's weather" then
			send weather("tommorow") to theBuddy
			
		else if theMessage is "turn on bedlamp" then
			tell application "XTension"
				activate
				turnon "elijahroombedlamp"
			end tell
			send "Bedlamp has been turned on" to theBuddy
			
		else if theMessage is "turn off bedlamp" then
			tell application "XTension"
				activate
				turnoff "elijahroombedlamp"
			end tell
			send "Bedlamp has been turned off" to theBuddy
			
		else if theMessage is "turn on overheadlights" then
			tell application "XTension"
				activate
				turnon "elijahroomoverheadlights"
			end tell
			send "Overheadlights have been turned on" to theBuddy
			
		else if theMessage is "turn off overheadlights" then
			tell application "XTension"
				activate
				turnoff "elijahroomoverheadlights"
			end tell
			send "Overheadlights have been turned off" to theBuddy
			
		else if theMessage is "turn on fishtanklight" then
			tell application "XTension"
				activate
				turnon "elijahroomfishtanklight"
			end tell
			send "FishTankLight has been turned on" to theBuddy
			
		else if theMessage is "turn off fishtanklight" then
			tell application "XTension"
				activate
				turnoff "elijahroomfishtanklight"
			end tell
			send "FishTankLight has been turned off" to theBuddy
			
		else if theMessage is "feed fish" then
			do shell script "/Users/drummerboyx/Library/Scripts/arduino-serial -b 9600 -p /dev/tty.usbserial-A800ev0Z -s 1"
			
			send "Fish Have been feed " to theBuddy
			
		else if theMessage is "CNN Update" then
			send "Loading..." to theBuddy
			send getlasttweet("cnn") to theBuddy
			
		else if theMessage is "Weather Update" then
			send "Loading..." to theBuddy
			send getlasttweet("weather") to theBuddy
			
		else if theMessage is "Project Jarvis update" then
			send "Loading..." to theBuddy
			send getlasttweet("cbarraford") to theBuddy
			
		else if theMessage is "the onion update" then
			send "Loading..." to theBuddy
			send getlasttweet("theonion") to theBuddy
			
		else if theMessage is "disable recognition" then
			tell application "System Preferences"
				activate
				set current pane to pane "com.apple.preference.sound"
				reveal (first anchor of current pane whose name is "input")
			end tell
			
			tell application "System Events"
				
				launch
				tell process "System Preferences"
					set value of slider 1 of group 2 of tab group 1 of window 1 to 0
				end tell
			end tell
			tell application "System Preferences"
				quit
			end tell
			
			send "Speech recognition disabled" to theBuddy
			say "speech recognition disabled"
			beep 3
			
			
		else if theMessage is "enable recognition" then
			tell application "System Preferences"
				activate
				set current pane to pane "com.apple.preference.sound"
				reveal (first anchor of current pane whose name is "input")
			end tell
			
			tell application "System Events"
				
				launch
				tell process "System Preferences"
					set value of slider 1 of group 2 of tab group 1 of window 1 to 1
				end tell
			end tell
			tell application "System Preferences"
				quit
			end tell
			
			send "Speech recognition enabled" to theBuddy
			say "speech recognition, enabled"
			beep 3
			
		else if theMessage is "disable speech" then
			say "speech, disabled"
			beep 3
			set volume output volume 0
			send "Speech disabled" to theBuddy
			
		else if theMessage is "enable speech" then
			set volume output volume 100
			beep 3
			say "speech, enabled"
			send "Speech enabled" to theBuddy
			
		else if theMessage is "goodnight" then
			start_sleep_timer()
			beep 3
			say "speech, disabled"
			set volume output volume 0
			tell application "System Preferences"
				activate
				set current pane to pane "com.apple.preference.sound"
				reveal (first anchor of current pane whose name is "input")
			end tell
			
			tell application "System Events"
				
				launch
				tell process "System Preferences"
					set value of slider 1 of group 2 of tab group 1 of window 1 to 0
				end tell
			end tell
			tell application "System Preferences"
				quit
			end tell
			send "Speech disabled, Goodnight Elijah!" to theBuddy
			tell application "XTension"
				turnoff "Elijah's Room"
			end tell
			
		else if theMessage is "goodmorning" then
			end_sleep_timer()
			set volume output volume 100
			beep 3
			say "speech, enabled"
			tell application "System Preferences"
				activate
				set current pane to pane "com.apple.preference.sound"
				reveal (first anchor of current pane whose name is "input")
			end tell
			
			tell application "System Events"
				
				launch
				tell process "System Preferences"
					set value of slider 1 of group 2 of tab group 1 of window 1 to 100
				end tell
			end tell
			tell application "System Preferences"
				quit
			end tell
			
			send "Speech enabled, Goodmorning Elijah! You've been sleeping for " & get_sleep_timer() & " hours." to theBuddy
			say "Good Morning Elijah!" & "You've been sleeping for " & get_sleep_timer() & "hours"
			
		else if theMessage is "hey" then
			send "Hi!" to theBuddy
			
		else if theMessage is "hi" then
			send "Hey!" to theBuddy
			
		else if theMessage is "hello" then
			send "Hey!" to theBuddy
			
		else if theMessage is "thanks" then
			send "Your welcome sir." to theBuddy
			
		else if theMessage is "help" then
			send "disable recognition, enable recognition, disable speech, enable speech, current weather, tommorow's weather, turn on, turn off,  feed fish, cnn update, weather update, project jarvis update, the onion update, goodmorning, goodnight" to theBuddy
			
		else
			send "???" to theBuddy
		end if
	end message received
	
end using terms from

Here. You don’t need globals.

property startTime : missing value
property endTime : missing value

on start_sleep_timer()
	-- (Re)initialise both startTime and endTime for a new sleep period.
	set startTime to time of (current date)
	set endTime to missing value
end start_sleep_timer

on end_sleep_timer()
	-- Only set the end time if the start time's been set.
	if (startTime is not missing value) then set endTime to time of (current date)
end end_sleep_timer

on get_sleep_timer()
	if (startTime is missing value) or (endTime is missing value) then
		-- The sleep timer either hasn't been started or hasn't been stopped.
		-- Rewrite this bit to do what you want under these circumstances.
		set sleepHours to 0
	else
		-- The time variables have both been set.
		set sleepHours to round ((endTime - startTime) / hours)
	end if
	-- Zap both time values.
	set startTime to missing value
	set endTime to missing value
	
	return sleepHours
end get_sleep_timer

on weather(weatherconditionscontrol)
	--this is the city code. Search the code for your city on http://weather.yahoo.com/
	
	set CityCode to 11050
	
	--temperature format
	
	set t_format to "F"
	
	--voiceover format
	
	set v_format to "A"
	
	--say present condition
	
	set a_format to "Y"
	
	
	
	set IURL to "http://weather.yahooapis.com/forecastrss?w=" & CityCode
	
	
	
	--downloading the file using curl
	
	set file_content to (do shell script "curl " & IURL)
	
	--looking for the line with actual condition
	
	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
	
	
	
	--today conditions found
	
	set actual_condition to text 1 thru ((offset of "\"" in sub_1) - 1) of sub_1
	
	
	
	--looking for actual temperature temperature
	
	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
	
	
	
	--looking for today forecast
	
	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
	
	
	
	--maximum and minimum temperatures found
	
	set today_min_temp to word 9 of sub_2
	
	set today_max_temp to word 12 of sub_2
	
	if t_format is equal to "C" then
		
		set today_min_temp to (5 / 9) * (today_min_temp - 32) as integer
		
		set today_max_temp to (5 / 9) * (today_max_temp - 32) as integer
		
	end if
	
	
	
	--looking for today forecast condition (a bit tricky)
	
	set sub_3 to text ((offset of "text" in sub_2) + 1) thru -1 of sub_2
	
	set sub_4 to text ((offset of "\"" in sub_3) + 1) thru -1 of sub_3
	
	set today_forecast to text 1 thru ((offset of "\"" in sub_4) - 1) of sub_4
	
	
	
	--looking for tomorrow forecast
	
	set sub_5 to text ((offset of "yweather:forecast" in sub_4) + 1) thru -1 of sub_4
	
	set sub_6 to text ((offset of "\"" in sub_5) + 1) thru -1 of sub_5
	
	
	
	--maximum and minimum temperatures found
	
	set tomorrow_min_temp to word 9 of sub_6
	
	set tomorrow_max_temp to word 12 of sub_6
	
	if t_format is equal to "C" then
		
		set tomorrow_min_temp to (5 / 9) * (tomorrow_min_temp - 32) as integer
		
		set tomorrow_max_temp to (5 / 9) * (tomorrow_max_temp - 32) as integer
		
	end if
	
	
	
	--looking for tomorrow forecast condition (a bit tricky)
	
	set sub_7 to text ((offset of "text" in sub_6) + 1) thru -1 of sub_6
	
	set sub_8 to text ((offset of "\"" in sub_7) + 1) thru -1 of sub_7
	
	set tomorrow_forecast to text 1 thru ((offset of "\"" in sub_8) - 1) of sub_8
	
	
	if weatherconditionscontrol is "today" then
		
		return "Today the forecast is " & today_forecast & ".  Temperature: between " & today_min_temp & " and " & today_max_temp & " degrees. " & "The current temperature is " & actual_temp & " degrees. "
		
	else if weatherconditionscontrol is "tommorow" then
		
		return "Tomorrow the forecast will be " & tomorrow_forecast & ". Temperature: between " & tomorrow_min_temp & " and " & tomorrow_max_temp & " degrees"
		
		
	end if
	
	
end weather

on getlasttweet(username)
	set myURL to "http://twitter.com/" & username
	set content to do shell script "curl " & myURL
	if content is not "<html><body>You are being <a href=\"http://twitter.com/suspended\">redirected</a>.</body></html>" and content does not contain "<h2>Sorry, that page doesn't exist!</h2>" and username is not "login" and username is not "home" then
		if content contains "<a class=\"entry-date\" rel=\"bookmark\" href=\"" then
			set text item delimiters to "<a class=\"entry-date\" rel=\"bookmark\" href=\""
			set content to text item 2 of content
			set text item delimiters to "\">"
			set content to text item 1 of content
			set lasttweet to do shell script "curl " & content & " | textutil -stdin -stdout -format html -convert txt -encoding UTF-8 "
			set text item delimiters to "Login Join Twitter!

"
			set lasttweet to text item 2 of lasttweet
			set text item delimiters to "
"
			set lasttweet to text item 1 of lasttweet
		else
			return ""
		end if
	else
		display dialog "This username does not exist or is not available."
	end if
end getlasttweet


using terms from application "iChat"
	
	on message received theMessage from theBuddy for theChat
		
		set startTime to missing value
		set endTime to missing value
		
		if theMessage is "current weather" then
			send weather("today") to theBuddy
			
		else if theMessage is "tommorow's weather" then
			send weather("tommorow") to theBuddy
			
		else if theMessage is "turn on bedlamp" then
			tell application "XTension"
				activate
				turnon "elijahroombedlamp"
			end tell
			send "Bedlamp has been turned on" to theBuddy
			
		else if theMessage is "turn off bedlamp" then
			tell application "XTension"
				activate
				turnoff "elijahroombedlamp"
			end tell
			send "Bedlamp has been turned off" to theBuddy
			
		else if theMessage is "turn on overheadlights" then
			tell application "XTension"
				activate
				turnon "elijahroomoverheadlights"
			end tell
			send "Overheadlights have been turned on" to theBuddy
			
		else if theMessage is "turn off overheadlights" then
			tell application "XTension"
				activate
				turnoff "elijahroomoverheadlights"
			end tell
			send "Overheadlights have been turned off" to theBuddy
			
		else if theMessage is "turn on fishtanklight" then
			tell application "XTension"
				activate
				turnon "elijahroomfishtanklight"
			end tell
			send "FishTankLight has been turned on" to theBuddy
			
		else if theMessage is "turn off fishtanklight" then
			tell application "XTension"
				activate
				turnoff "elijahroomfishtanklight"
			end tell
			send "FishTankLight has been turned off" to theBuddy
			
		else if theMessage is "feed fish" then
			do shell script "/Users/drummerboyx/Library/Scripts/arduino-serial -b 9600 -p /dev/tty.usbserial-A800ev0Z -s 1"
			
			send "Fish Have been feed " to theBuddy
			
		else if theMessage is "CNN Update" then
			send "Loading..." to theBuddy
			send getlasttweet("cnn") to theBuddy
			
		else if theMessage is "Weather Update" then
			send "Loading..." to theBuddy
			send getlasttweet("weather") to theBuddy
			
		else if theMessage is "Project Jarvis update" then
			send "Loading..." to theBuddy
			send getlasttweet("cbarraford") to theBuddy
			
		else if theMessage is "the onion update" then
			send "Loading..." to theBuddy
			send getlasttweet("theonion") to theBuddy
			
		else if theMessage is "disable recognition" then
			tell application "System Preferences"
				activate
				set current pane to pane "com.apple.preference.sound"
				reveal (first anchor of current pane whose name is "input")
			end tell
			
			tell application "System Events"
				
				launch
				tell process "System Preferences"
					set value of slider 1 of group 2 of tab group 1 of window 1 to 0
				end tell
			end tell
			tell application "System Preferences"
				quit
			end tell
			
			send "Speech recognition disabled" to theBuddy
			say "speech recognition disabled"
			beep 3
			
			
		else if theMessage is "enable recognition" then
			tell application "System Preferences"
				activate
				set current pane to pane "com.apple.preference.sound"
				reveal (first anchor of current pane whose name is "input")
			end tell
			
			tell application "System Events"
				
				launch
				tell process "System Preferences"
					set value of slider 1 of group 2 of tab group 1 of window 1 to 1
				end tell
			end tell
			tell application "System Preferences"
				quit
			end tell
			
			send "Speech recognition enabled" to theBuddy
			say "speech recognition, enabled"
			beep 3
			
		else if theMessage is "disable speech" then
			say "speech, disabled"
			beep 3
			set volume output volume 0
			send "Speech disabled" to theBuddy
			
		else if theMessage is "enable speech" then
			set volume output volume 100
			beep 3
			say "speech, enabled"
			send "Speech enabled" to theBuddy
			
		else if theMessage is "goodnight" then
			start_sleep_timer()
			beep 3
			say "speech, disabled"
			set volume output volume 0
			tell application "System Preferences"
				activate
				set current pane to pane "com.apple.preference.sound"
				reveal (first anchor of current pane whose name is "input")
			end tell
			
			tell application "System Events"
				
				launch
				tell process "System Preferences"
					set value of slider 1 of group 2 of tab group 1 of window 1 to 0
				end tell
			end tell
			tell application "System Preferences"
				quit
			end tell
			send "Speech disabled, Goodnight Elijah!" to theBuddy
			tell application "XTension"
				turnoff "Elijah's Room"
			end tell
			
		else if theMessage is "goodmorning" then
			end_sleep_timer()
			set volume output volume 100
			beep 3
			say "speech, enabled"
			tell application "System Preferences"
				activate
				set current pane to pane "com.apple.preference.sound"
				reveal (first anchor of current pane whose name is "input")
			end tell
			
			tell application "System Events"
				
				launch
				tell process "System Preferences"
					set value of slider 1 of group 2 of tab group 1 of window 1 to 100
				end tell
			end tell
			tell application "System Preferences"
				quit
			end tell
			
			send "Speech enabled, Goodmorning Elijah! You've been sleeping for " & get_sleep_timer() & " hours." to theBuddy
			say "Good Morning Elijah!" & "You've been sleeping for " & get_sleep_timer() & "hours"
			
		else if theMessage is "hey" then
			send "Hi!" to theBuddy
			
		else if theMessage is "hi" then
			send "Hey!" to theBuddy
			
		else if theMessage is "hello" then
			send "Hey!" to theBuddy
			
		else if theMessage is "thanks" then
			send "Your welcome sir." to theBuddy
			
		else if theMessage is "help" then
			send "disable recognition, enable recognition, disable speech, enable speech, current weather, tommorow's weather, turn on, turn off,  feed fish, cnn update, weather update, project jarvis update, the onion update, goodmorning, goodnight" to theBuddy
			
		else
			send "???" to theBuddy
		end if
	end message received
	
end using terms from