Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Play Sound Effects Through AppleScript

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

Posted on Oct 26, 2009 10:12 AM

Reply
Question marked as Best reply

Posted on Oct 26, 2009 11:55 AM

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
3 replies
Question marked as Best reply

Oct 26, 2009 11:55 AM in response to nate12345

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

Oct 26, 2009 2:14 PM in response to Budgie

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

Play Sound Effects Through AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.