3 Replies Latest reply: Oct 26, 2009 2:55 PM by Budgie
nate12345 Level 2 Level 2 (495 points)
Hi, everyone.

I'm looking for a way to play a sound (preferably the Glass sound effect, located in /System/Library/Sounds) in an AppleScript.

I'd like to do this as an audible way to tell that the script is done.
Is there a way to do this?

On the Web, I found info on scripting additions that make it easier, but I need this to be self-contained and self-reliant, so an extra utility won't work.

I was thinking that maybe, if need be, it could work something like this:

set PreviousDefault to (do shell script defauls read com.apple.systemsound …) --I don't know how to use the defaults read/write command, but I do know that this is where the name of the default sound effect is stored.
do shell script defaults write com.apple.systemsound … --Set the default to Glass.aiff
beep
do shell script defaults write com.apple.systemsound PreviousDefault


So, this would read the default sound effect and store it in a variable, set the default sound effect to Glass, beep (with glass), and reset the default sound effect to what it was before.

Would that work? I don't know how to use the defaults… commands.
Ideally, there would be an easier way to work this than what I came up with.

Thanks in advance!
-Nate

MacBook 2.4 ghz 4gb ram, Mac OS X (10.6.1), MacBook: June 2008, 2.4ghz Core 2 Duo, 4GB RAM. iPod Nano 3G. AirPort Extreme 20
  • Budgie Level 4 Level 4 (1,120 points)
    Hi Nate
    under osx 10.5 and on you can use "afplay"


    tell application "YOUR APPLICATION"
    activate
    set the_file to "PATH TO YOUR SOOUND FILE.aif"
    set file_path to quoted form of (POSIX path of (the_file as alias))
    do shell script ("afplay " & file_path & " > /dev/null 2>&1 &")
    end tell



    or you could use the nifty little app Play Sound from http://microcosmsoftware.com/playsound/ to do the work for you under 10.4 etc, not your preferred but a choice.

    tell application "Play Sound"
    activate
    set soundFile to "PATH TO YOUR SOUND FILE" as alias
    tell application "Play Sound"
    play soundFile
    end tell
    delay 2
    quit "Play Sound" --use this as this app stays running in the background
    end tell


    Budgie
  • nate12345 Level 2 Level 2 (495 points)
    Thanks a million!
    Here's what I've got:

    set donesound to quoted form of (POSIX path of ((path to me as string) & "Contents:Resources:Glass.aiff" as alias))
    do shell script ("afplay " & donesound & " > /dev/null 2>&1 &")

    That works perfectly!

    Just to make sure:
    This doesn't require the user to have anything special (XCode, Dev tools, etc.), right?
    And it only works in 10.5 and 10.6.

    Thanks again!

    Message was edited by: nate12345
  • Budgie Level 4 Level 4 (1,120 points)
    great, glad that helped

    no need for xcode etc

    afplay doesnt work under 10.4, does under 10.5, 10.6 I have no idea as do have have that installed yet


    Budgie