Help - Search - Members - Calendar
Full Version: Simple AppleScript Help
InsanelyMac Forum > Apple World > Mac Programming and Development > AppleScript and Automator
htdefiant
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
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
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
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
Quicktime would be acceptable, I think.
thePixeler
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
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 smile.gif
thePixeler
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
Leopard 10.5.2. I will try this when I return to my Mac.
htdefiant
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
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
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
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
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
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
Still getting the same error.
thePixeler
Argh. And you though this was going to be easy. blink.gif
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
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
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
Error: Can't get document "audio.wav" of application "Quicktime Player". Guide closed though.
thePixeler
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
It lives! biggrin.gif
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.