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:
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.
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.