htdefiant
Apr 27 2008, 03:36 AM
Hello all,
I am brand new to AppleScripting, but I don't have the time to dedicate right now to actually learning the language - I just need to get one script done. Here is what it needs to do:
1. Once the program is launched, a 16 second audio clip will play.
2. All currently running programs will be forced to quit with no save options.
3. The computer will be shut down.
Seeing as I have literally never scripted before, what do you think the best way of going about this is?
Thanks
thePixeler
Apr 30 2008, 02:30 PM
AppleScript by itself can't play audio files. Would it be OK it QuickTime played the audio clip? Or you could use something like
Play Sound.
htdefiant
Apr 30 2008, 08:19 PM
I'd rather not use PlaySound, only because I want this script to be able to run on more then just my Mac. Would making this an application be better?
thePixeler
Apr 30 2008, 11:01 PM
What about QuickTime? Every Mac has QuickTime installed. Or do you not want the user to see the QuickTime window. if you don't, then making a Cocoa app is the only way I can think of that will do what you want.
htdefiant
Apr 30 2008, 11:22 PM
Quicktime would be acceptable, I think.
thePixeler
May 1 2008, 06:35 PM
OK then use this script.
CODE
set audioFile to "/path/to/your/audio/file.mp3"
tell application "QuickTime Player"
activate
open audioFile
end tell
tell application "Finder"
set visible of process "QuickTime Player" to false
end tell
delay 16
tell application "System Events"
set theProcesses to every process whose visible is true and name is not "Finder" and name is not (name of me)
end tell
repeat with i from 1 to (count of theProcesses)
set thisProcess to item i of theProcesses
try
tell application (name of thisProcess) to quit without saving
end try
end repeat
tell application "Finder" to shut down
Don't forget to edit the first line to point to the audio file you want QuickTime to play and to save it as an application.
htdefiant
May 1 2008, 11:46 PM
Hey,
The file did not autoplay, Quicktime player just opened. When I manually played the file to its completion, the computer did not shut down.
Thanks for your help
thePixeler
May 3 2008, 01:19 AM
Try using this.
CODE
set audioFile to "/path/to/your/audio/file.mp3"
tell application "QuickTime Player"
activate
open audioFile
play the front document
end tell
tell application "Finder"
set visible of process "QuickTime Player" to false
end tell
delay 16
tell application "System Events"
set theProcesses to every process whose visible is true and name is not "Finder" and name is not (name of me)
end tell
repeat with i from 1 to (count of theProcesses)
set thisProcess to item i of theProcesses
try
tell application (name of thisProcess) to quit without saving
end try
end repeat
tell application "Finder" to shut down
What OS are you running? Also note that the computer will only shutdown if you save this script as an application and run the app. If you just click "Run" in Script Editor it wont work.
htdefiant
May 3 2008, 03:52 PM
Leopard 10.5.2. I will try this when I return to my Mac.
htdefiant
May 3 2008, 05:24 PM
I got it to work, once. However, when I wanted to show a friend, running the application for the second time, the Quicktime file failed to autoplay. Also, the Quicktime browser windows opened.
Thanks.
thePixeler
May 3 2008, 07:25 PM
I works fine for me. AppleScript is can be so picky sometimes.
Try
this one.
CODE
set audioFile to "Macintosh HD:Users:yourUserName:pathTo:theAudioFile.aif" as alias
tell application "Finder" to set theName to (name of audioFile)
tell application "QuickTime Player"
activate
open audioFile
play document theName
end tell
delay 16
tell application "System Events" to set theProcesses to every process whose visible is true and name is not "Finder" and name is not (name of me)
repeat with i from 1 to (count theProcesses)
set thisProcess to item i of theProcesses
try
tell application (name of thisProcess) to quit without saving
end try
end repeat
tell application "Finder" to shut down
htdefiant
May 3 2008, 08:09 PM
Got it working. Now is there anyway to export it so that it includes the audio file (so I don't have to have the file sitting on my desktop)? In other words, is there any way to make this a packaged app?
thePixeler
May 3 2008, 09:27 PM
Yes there is.
Change the top line in the script to this…
CODE
set audioFile to ((path to me as string) & "Contents:Resources:" & "audioFile.aif") as alias
When you save the script select "Application bundle" as the file format.

In the Finder, right click the newly saved app and select "Show Package Contents". Navigate to "Contents>Resources" and place your audio file there (inside the "Resources" folder).
Now it's self contained.
htdefiant
May 3 2008, 09:54 PM
OK, the program now reads:
CODE
set audioFile to ((path to me as string) & "Contents:Resources:" & "audio.wav") as alias
tell application "Finder" to set theName to (name of audioFile)
tell application "QuickTime Player"
activate
open audioFile
play document theName
end tell
delay 16
tell application "System Events" to set theProcesses to every process whose visible is true and name is not "Finder" and name is not (name of me)
repeat with i from 1 to (count theProcesses)
set thisProcess to item i of theProcesses
try
tell application (name of thisProcess) to quit without saving
end try
end repeat
tell application "Finder" to shut down
However, when I launch it, it says "CAn't get document "audio.wav" of application "Quicktime Player". Oddly, once I click OK on that error message, the file is present (not autoplaying) and I can press play manually. The script aborts though.
'
thePixeler
May 3 2008, 10:24 PM
Hmm.... OK. Try adding a one or two second delay between the open command and the play command. So it'll look like this.
CODE
open audioFile
delay 2
play document theName
htdefiant
May 3 2008, 11:57 PM
Still getting the same error.
thePixeler
May 4 2008, 12:57 AM
Argh. And you though this was going to be easy.

OK 6th time's the charm. Replace the whole QuickTime tell block with this new one.
CODE
tell application "QuickTime Player"
activate
open audioFile
set openDocuments to 0
set timer to 0
repeat while (count openDocuments) < 2 and timer < 10
set openDocuments to the name of every document
repeat with i from 1 to (count openDocuments)
play document (item i of openDocuments)
end repeat
set timer to (timer + 1)
delay 1
end repeat
end tell
htdefiant
May 4 2008, 01:06 AM
Error: Can't get document "promo.mov" of application "Quicktime Player". Audio does play though. Seems to be a problem with the Quicktime Guide that launches. If you can get the content guide not to load, this might work. 7th time?
thePixeler
May 4 2008, 01:18 AM
Does the content guide at least close now?
CODE
tell application "QuickTime Player"
activate
delay 2
set openDocuments to (every document)
repeat with i from 1 to (count openDocuments)
close (item i of openDocuments)
end repeat
open audioFile
play document "audio.wav"
end tell
htdefiant
May 4 2008, 02:26 AM
Error: Can't get document "audio.wav" of application "Quicktime Player". Guide closed though.
thePixeler
May 4 2008, 03:34 AM
Replace: play document "audio.wav"
With:
play the front document
If that doesn't work then I'm out of ideas. Maybe your computer is cursed, I dunno.
htdefiant
May 4 2008, 06:56 PM
It lives!

Thank you very much for your help.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please
click here.