itunes sleep/wake script - how to improve it???

Hi, I like to fall asleep to itunes and wake up to it also, I have made a couple of scripts for this purpose. The wakup script is simple enough, and it is launched at login in the usual way. However I would like to make it able to run after the mac wakes up too. Also, the sleep script has options to sleep or shut down after a certain song is played. However sometime when it gets to that song and tries to shut down the script won’t quit and stops the stystem from shutting down. Subsequently it will not wake me up in the morning.

here are the scripts, can anyone suggest a better way to do achieve what I want to do?

wakeup script (launched at login):


tell application "iTunes"
	try
		set current EQ preset to EQ preset "Flat"
	end try
	
	try
		play playlist "***Wakeup"
	on error
		play some playlist
	end try
	
end tell

sleep script (launched manually):


property sleep_var : ""
property time_var : ""
property prop_var : ""

property action_var : ""




display dialog "Action?" buttons {"Cancel", "Sleep", "Shut Down"} default button 3 giving up after 21
set action_var to button returned of the result

if action_var is "" then set action_var to "Shutdown"



tell application "iTunes"
	
	try
		set endtrack to name of selection as string
		
		tell me
			display dialog "Your Computer will " & action_var & "after song " & "'" & endtrack & "'" & " is played" giving up after 5
		end tell
		
		try
			repeat until name of current track = endtrack
				delay 2
				
			end repeat
			
		on error
			tell me
				display dialog "iTunes is not playing" buttons "Cancel" default button 1 giving up after 5 with icon 2
				my quitout()
			end tell
		end try
		
	on error
		tell me
			display dialog "No trak is selected" buttons "Cancel" default button 1 giving up after 5 with icon 2
			my quitout()
		end tell
	end try
	my checktime()
	
end tell


on showsong()
	display dialog "Your Computer will " & action_var & "after song " & "'" & endtrack & "'" & "is played" giving up after 2
end showsong


my checktime()


on checktime()
	
	
	
	tell application "iTunes"
		delay 2
		if player state is not playing then my action_time()
		try
			set time_var to (time of current track) as string
			set min_utes to (first word of time_var) * 60
			set delay_time to min_utes + (second word of time_var)
			set playpoint to player position + 1
			
			set time_var to delay_time - playpoint
			
		end try
	end tell
	
	delay (time_var)
	
	
	try
		tell application "iTunes"
			stop
			
		end tell
	on error
		my action_time
	end try
	
	my action_time()
	
end checktime

on action_time()
	if action_var is "Shut Down" then tell application "Finder" to shut down
	if action_var is "Sleep" then tell application "Finder" to sleep
	
	my quitout
	
end action_time



on quitout()
	tell me to quit
end quitout


To summarise what I want:

¢ to choose a track in an iTunes playlist, and have the mac either shutdown, or sleep once that track has finished playing
¢ if sleep was the option above, I want a certain iTunes playlist to be played once the machine wakes up

the problem I have with the current scripts is I don’t know how to activate it after wakeup, and the script (saved as an app) seems to freeze up and stop the mac from shutting down.

any help mucho appreciated.

thanks,

arum

There’s another solution: Don’t shut the Mac down. Then you could use an idle handler to wake you up with the playlist you want.

For an idle handler to work, the script must be an application (save as “application”) and you must select “stay open” when you save the application.

An idle handler runs as often as you want/need. Each time it is run, it should return a number that is the number of seconds before it should be run again. For example, if you know what time you want to wake up, you can set that as the time to run the playlist. Then it’s a simple matter of comparing the current time to the alarm time.

Here’s an example. It’s my own Sleep Music script:

property endTime : current date
property quitWhenDone : true

display dialog "How many minutes should I run the music?" default answer "20"
if button returned of the result is "OK" then set playTime to (text returned of the result) as integer
tell application "iTunes" to set musicLists to name of every playlist
set thePlayList to choose from list musicLists
display dialog "When time is up, should iTunes quit or just stop playing?" buttons {"Quit", "Stop Playing"} default button "Stop Playing"
if the button returned of the result is "Quit" then
	set quitWhenDone to true
else
	set quitWhenDone to false
end if

tell application "iTunes" to play playlist (thePlayList as Unicode text)
set beginTime to current date
set endTime to beginTime + playTime * 60

on idle
	if (current date) is greater than endTime then
		if quitWhenDone then
			tell application "iTunes" to quit
		else
			tell application "iTunes" to stop
		end if
		quit me
		return 0
	else
		return 10
	end if
end idle

Does that help?

thanks Kevin.

I’ve never really understood the on idle handler (not that I’ve tried too hard) but it looks like that might do the trick :smiley: I’ll check it out. thanks very much for your reply… :slight_smile: