# iTunes autoplay on wake - AppleScript help



## profx (Mar 4, 2003)

well, slightly more complicated than that:

I have written two applescripts, one that is executed just before system sleep the other when the system wakes.

The first checks if itunes is running and if it is playing.
The second then resumes playing if itunes was playing before sleep.(notice the classy fade in)

on sleep: itunesSleep.scpt

```
try
	do shell script "top -l 1 | grep 'iTunes'"
	tell application "iTunes"
		if player state as string is "playing" then do shell script "/bin/echo play >~/itunesStatus.txt"
	end tell
end try
```

on wake: itunesWake.scpt

```
try
	tell application "iTunes"
		do shell script "/bin/cat ~/itunesStatus.txt"
		set stat to result as string
		set vol to sound volume as number
		set sound volume to 0
		if stat is "play" then play
		repeat with i from 1 to vol
			set sound volume to i
			set i to i + 1
			delay 0.4
		end repeat
		do shell script "/bin/rm ~/itunesStatus.txt"
	end tell
end try
```

these work perfectly when run from script editor but when run from terminal as
_osascript -l Applescript itunesSleep.scpt_
it packs a sad and fails to write the itunesStatus.txt file because the player status is
_<<constant ****kPSP>>_ when run from terminal no matter what state the player is actually in.

Anyone have any ideas as to what is going on??

By the way i am planning on using sleepwatcher to run the scripts, but it can only run unix scripts, hence the need to use osascript.


Cheers.


----------



## profx (Mar 4, 2003)

someone?


----------



## jove (Mar 16, 2003)

Is there anybody out there (PF)...

Tried it myself. Same problem. Looked on the ol' internet. Don't use string! playing is a usable constant. I changed the check routine to look for "playing". And I put a dot in front of the status file name.

	tell application "iTunes"
		if player state is playing then
			do shell script "/bin/echo playing >~/.itunesStatus.txt"
		else
			do shell script "/bin/echo stopped >~/.itunesStatus.txt"
		end if
	end tell


----------



## jove (Mar 16, 2003)

The scripts work flawlessly. Sleep and wakeup do not.

The scripts are saved as applications in ~/Library/scripts. The .wakeup and .sleep scripts call "open ~/Library/Scripts/..."

The sleep application doesn't perform until wakeup. The wakeup application doesn't perform until a couple seconds later!

We need to be able to tell the system "Don't sleep until Applescript is done".


----------



## profx (Mar 17, 2003)

i have given up on getting the playing status of itunes before sleep, instead i just execute one script on wake up

```
tell application "Finder"
	if processes contains {process "iTunes"} then
		-- iTunes is running
		tell application "iTunes"
			set vol to sound volume as number
			set sound volume to 0
			back track
			play
			repeat with i from 1 to vol
				set sound volume to i
				set i to i + 1
				delay 0.4
			end repeat
		end tell
	end if
end tell
```

along with the shell script that executes the applecript

%osascript scriptname.scpt

dont forget to "chomd +x" your shell script file

This works perfectly for me.




on another note i often play my mac into a stereo so i find this script comes in handy:

```
tell application "Dock"
	activate
	display dialog "Choose Volume setting" buttons {"Stereo", "Normal"} default button 2
	set var to the button returned of the result
	if var is "Stereo" then
		tell application "iTunes"
			back track
			play
			set sound volume to 100
			set volume 10
		end tell
	else
		tell application "iTunes"
			next track
			play
			set sound volume to 15
			set volume 1
		end tell
	end if
end tell
```
it tells dock, so it dosent interupt your current app, eg telling finder would bring finder to the front and open a new window, telling dock leaves you in your current app.

An improvement to this script would be to get the system volume and check if full volume change to min volume, and visa versa


----------

