Wake up script

When i run this as a stay open application it says: “The variable TimeEntered is not defined.”


display dialog "What time shall i wake you up?" default answer "" buttons {"Cancel", "OK"} default button "OK"
copy result as list to {TimeEntered, Buttonpressed}
set WakeUpTime to TimeEntered * 60 * 60

on idle
	
	set SchoolDays to {Monday, Tuesday, Wednesday, Thursday, Friday}
	if SchoolDays contains ((current date)'s weekday) and ((current date)'s time = WakeUpTime) then
		tell application "iTunes" to play track 1 of playlist "Top 25 Most Played"
	end if
	return 1
end idle

But i thought it is defined, it’s the time you enter…

Hi,

the problem is, you have two scopes (the implicit run handler and the idle handler)
so the variable WakeUpTime can’t be seen in the idle handler.
You must either define a property like

property WakeUpTime : 0

or declare the variable as global.
I would also coerce TimeEntered to integer, because display dialog returns class text

set WakeUpTime to (TimeEntered as integer) * hours -- hours is the constant 3600 

btw: executing the idle handler every second for 5 times a week is an enormous waste of resources.
I recommend to set the interval to 60 or, much better, install a launchd agent which doesn’t consume any resources

Thank you very much that worked, but where can i install a launched agent?

This article discusses some of the basics of using launchd and AppleScript, and has some good links to other launchd resources.

Although launchd was introduced with Mac OS X 10.4 (Tiger), it really works most reliably on an Intel machine running 10.5 (Leopard).

Post back if you have any further questions.