Mac Problem with applescript

thecow

macrumors 6502
Original poster
Nov 24, 2003
400
0
Timonium MD
I'm trying to make an applescript for an alarm since none of the programs will switch to the internal speakers before playing a song from iTunes. The problem is that when it is still in the script editor, it works fine and does exactly what it is supposed to, with one exception. I'll get to that later. When I try to save it as an application to launch at the right time with cron it does one of 3 things when I launch that app:
1. it ignores all of the volume changes after the "tell iTunes to play" command. OR
2. it doesn't play at all and goes straight from switching the sound output device to the message box. OR
3. It works fine

Here's the code:
Code:
tell application "System Preferences"
	activate
	set current pane to pane "com.apple.preference.sound"
	reveal (first anchor of current pane whose name is "output")
end tell
tell application "System Events"
	get properties
	tell process "System Preferences"
		set theRows to every row of table 1 of scroll area 1 of tab group 1 of window "sound"
		set theOutputs to {} as list
		repeat with aRow in theRows
			if (value of text field 1 of aRow as text) is equal to "Internal Speakers" then
				set selected of aRow to true
				exit repeat
			end if
		end repeat
	end tell
end tell
set volume 0
tell application "iTunes" to play
set volume 1
delay 0.1
set volume 1.1
delay 0.1
set volume 1.2
delay 0.1
set volume 1.3
delay 0.1
set volume 1.4
delay 0.1
set volume 1.5
delay 0.1
set volume 1.6
delay 0.1
set volume 1.7
delay 0.1
set volume 1.8
delay 0.1
set volume 1.9
delay 0.1
set volume 2
delay 0.1
set volume 2.1
delay 0.1
set volume 2.2
delay 0.1
set volume 2.3
delay 0.1
set volume 2.4
delay 0.1
set volume 2.5
delay 0.1
set volume 2.6
delay 0.1
set volume 2.7
delay 0.1
set volume 2.8
delay 0.1
set volume 2.9
delay 0.1
set volume 3
delay 0.1
set volume 3.1
delay 0.1
set volume 3.2
delay 0.1
set volume 3.3
delay 0.1
set volume 3.4
delay 0.1
set volume 3.5
delay 0.1
set volume 3.6
delay 0.1
set volume 3.7
delay 0.1
set volume 3.8
delay 0.1
set volume 3.9
delay 0.1
set volume 4
delay 0.1
set volume 4.1
delay 0.1
set volume 4.2
delay 0.1
set volume 4.3
delay 0.1
set volume 4.4
delay 0.1
set volume 4.5
delay 0.1
set volume 4.6
delay 0.1
set volume 4.7
delay 0.1
set volume 4.8
delay 0.1
set volume 4.9
delay 0.1
set volume 5
delay 0.1
set volume 5.1
delay 0.1
set volume 5.2
delay 0.1
set volume 5.3
delay 0.1
set volume 5.4
delay 0.1
set volume 5.5
delay 0.1
set volume 5.6
delay 0.1
set volume 5.7
delay 0.1
set volume 5.8
delay 0.1
set volume 5.9
delay 0.1
set volume 6
delay 0.1
set volume 6.1
delay 0.1
set volume 6.2
delay 0.1
set volume 6.3
delay 0.1
set volume 6.4
delay 0.1
set volume 6.5
delay 0.1
set volume 6.6
delay 0.1
set volume 6.7
delay 0.1
set volume 6.8
delay 0.1
set volume 6.9
delay 0.1
set volume 7
tell application "Alarm" to activate
display dialog "Awake yet?" buttons ["Yes. Yes. Now ****!"] default button 1
if button returned of result = "Yes. Yes. Now ****!" then
	tell application "iTunes" to pause
	tell application "System Events"
		get properties
		tell process "System Preferences"
			set theRows to every row of table 1 of scroll area 1 of tab group 1 of window "sound"
			set theOutputs to {} as list
			repeat with aRow in theRows
				if (value of text field 1 of aRow as text) is equal to "Sonica Theater" then
					set selected of aRow to true
					exit repeat
				end if
			end repeat
		end tell
	end tell
else
	delay 5
	tell application "iTunes" to pause
	tell application "System Events"
		get properties
		tell process "System Preferences"
			set theRows to every row of table 1 of scroll area 1 of tab group 1 of window "sound"
			set theOutputs to {} as list
			repeat with aRow in theRows
				if (value of text field 1 of aRow as text) is equal to "Sonica Theater" then
					set selected of aRow to true
					exit repeat
				end if
			end repeat
		end tell
	end tell
end if
tell application "System Preferences" to quit
set volume 4.5
I also tried to get it to stop playing after a set time if the button on the dialog box wasn't pressed, but that doesn't work either. That is my fault though.
 

Eraserhead

macrumors G4
Nov 3, 2005
10,410
10,603
UK
Well I don't have any external speakers and so I removed all the code except the volume control stuff, so i was left with...
Code:
set volume 0
tell application "iTunes" to play
set volume 1
delay 0.1
set volume 1.1
delay 0.1
set volume 1.2
delay 0.1
set volume 1.3
delay 0.1
set volume 1.4
delay 0.1
set volume 1.5
delay 0.1
set volume 1.6
delay 0.1
set volume 1.7
delay 0.1
set volume 1.8
delay 0.1
set volume 1.9
delay 0.1
set volume 2
delay 0.1
set volume 2.1
delay 0.1
set volume 2.2
delay 0.1
set volume 2.3
delay 0.1
set volume 2.4
delay 0.1
set volume 2.5
delay 0.1
set volume 2.6
delay 0.1
set volume 2.7
delay 0.1
set volume 2.8
delay 0.1
set volume 2.9
delay 0.1
set volume 3
delay 0.1
set volume 3.1
delay 0.1
set volume 3.2
delay 0.1
set volume 3.3
delay 0.1
set volume 3.4
delay 0.1
set volume 3.5
delay 0.1
set volume 3.6
delay 0.1
set volume 3.7
delay 0.1
set volume 3.8
delay 0.1
set volume 3.9
delay 0.1
set volume 4
delay 0.1
set volume 4.1
delay 0.1
set volume 4.2
delay 0.1
set volume 4.3
delay 0.1
set volume 4.4
delay 0.1
set volume 4.5
delay 0.1
set volume 4.6
delay 0.1
set volume 4.7
delay 0.1
set volume 4.8
delay 0.1
set volume 4.9
delay 0.1
set volume 5
delay 0.1
set volume 5.1
delay 0.1
set volume 5.2
delay 0.1
set volume 5.3
delay 0.1
set volume 5.4
delay 0.1
set volume 5.5
delay 0.1
set volume 5.6
delay 0.1
set volume 5.7
delay 0.1
set volume 5.8
delay 0.1
set volume 5.9
delay 0.1
set volume 6
delay 0.1
set volume 6.1
delay 0.1
set volume 6.2
delay 0.1
set volume 6.3
delay 0.1
set volume 6.4
delay 0.1
set volume 6.5
delay 0.1
set volume 6.6
delay 0.1
set volume 6.7
delay 0.1
set volume 6.8
delay 0.1
set volume 6.9
delay 0.1
set volume 7
--tell application "Alarm" to activate
display dialog "Awake yet?" buttons ["Yes. Yes. Now ****!"] default button 1 
if button returned of result = "Yes. Yes. Now ****!" then
	tell application "iTunes" to pause
else
	delay 5
	tell application "iTunes" to pause
end if
set volume 4.5
Which worked fine as an application for me... You could try adding a delay of a second after it changes the sound output and another delay of a second after it tells iTunes to play, it might be that the system is just too slow to process everything and gets confused...

If that doesn't work you could try splitting the applescripts off and running them from a main program written in another language...

Change the dialog to
with timeout of 90 seconds
display dialog "Awake Yet?" buttons {"Yes Yes Now ****!"} default button 1 giving up after 89
end
to stop after 89 seconds. Increase the timeout to bigger than that if you want to increase it above 89 seconds otherwise Applescript will timeout (if you want to give up after less than 60 seconds the timeout stuff isn't needed.)