How to play sounds in your AppleScript

Back in December Dr. Drang posted an interesting article about Playing sounds in AppleScript using the free Play Sound software from Microcosm Software

Once you have installed Play Sounds in your Applications folder, it is super easy and fun to play with its sound commands, as you can see in the code below which plays every sound file found in ‘/Systems/Library/Sounds’.

If you want to have a look at its AppleScript dictionary, just open the Read Me document included with the download or drag and drop the Play Sound app onto the Script Editor.

 
-- created: 04.12.2007 

property myname : "MacLibSounds 1"

on run
	my main()
end run

-- plays all sound files found in '/System/Library/Sounds' 
on main()
	try
		-- searching for the sound files 
		set soundfiles to {}
		set itemnames to paragraphs of (do shell script "ls -a /System/Library/Sounds")
		repeat with itemname in itemnames
			if itemname ends with ".aiff" then
				set soundfile to (POSIX file ("/System/Library/Sounds/" & itemname)) as Unicode text
				set soundfiles to soundfiles & soundfile
			end if
		end repeat
		-- playing the sound files using Play Sound 
		repeat with soundfile in soundfiles
			tell application "Play Sound"
				play soundfile
			end tell
		end repeat
		-- catching unexpected errors 
	on error errmsg number errnum
		tell me
			activate
			display dialog "Sorry, an error occured" & return & return & errmsg & " (" & errnum & ")" buttons {"OK"} default button 1 with icon caution with title myname
		end tell
	end try
end main 

But what if you do not want or cannot install an additional software like Play Sound? Is it still possible to play a sound file from within an AppleScript?

Yes, it is. At least if your script runs under Mac OS X 10.5 Leopard. The AppleScript presented below also plays all available system sounds, but uses a Python script for playback which utilizes the newly introduced Scripting Bridge and NSSound. No external software, just pure and built-in Mac OS X 10.5 technologies. To test and inspect the script on your fine Mac running Mac OS X 10.5 please download the complete script here.

Important: Opening and saving the below script code in Script Editor won’t result in a usable AppleScript! That is because the AppleScript internally relies on a little Python helper script, which is located inside its Application bundle. Therefor please download the complete script here.


-- created: 14.05.2008

property myname : "MacLibSounds 2"

on run
	my main()
end run

-- plays all sound files found in '/System/Library/Sounds' 
on main()
	try
		-- searching for the sound files 
		set soundfiles to ""
		set itemnames to paragraphs of (do shell script "ls -a /System/Library/Sounds")
		repeat with itemname in itemnames
			if itemname ends with ".aiff" then
				set soundfile to (POSIX file ("/System/Library/Sounds/" & itemname)) as Unicode text
				set soundfiles to soundfiles & space & quoted form of (POSIX path of soundfile)
			end if
		end repeat
		-- playing the sound files using a Python script utilizing NSSound
		set pyscript to POSIX path of (((path to me) as Unicode text) & "Contents:Resources:macsound.py")
		set command to "python " & quoted form of pyscript & soundfiles
		set command to command as «class utf8»
		do shell script command
		-- catching unexpected errors 
	on error errmsg number errnum
		tell me
			activate
			display dialog "Sorry, an error occured" & return & return & errmsg & " (" & errnum & ")" buttons {"OK"} default button 1 with icon caution with title myname
		end tell
	end try
end main

I am very interested in what you wrote, but I want to include sound in my Applescript Application in the resource folder in another folder named “Sounds”. I did this to locate the folder that the sound was in:

(POSIX path of (path to me) & POSIX path of ":Contents:Resources:Sounds:")

When I launch the script, no error shows up, it just quits. Doesn’t crash. How can I play sounds like the demo app does, but make it play sounds inside of the “Resources” folder? Please help immediately!

Hi,

The following sample code will play all sound files (*.aiff) found in the bundle of the script itself (/Contents/Resources/Sounds/):


property myname : "MacLibSounds 2"

on run
	my main()
end run

-- plays all sound files found in '/System/Library/Sounds' 
on main()
	try
		-- searching for the sound files inside my own bundle
		set soundsfolderpath to POSIX path of (((path to me) as Unicode text) & "Contents:Resources:Sounds:")
		set soundfiles to ""
		set itemnames to paragraphs of (do shell script "ls -a " & quoted form of soundsfolderpath)
		repeat with itemname in itemnames
			if itemname ends with ".aiff" then
				set soundfile to (POSIX file ("/System/Library/Sounds/" & itemname)) as Unicode text
				set soundfiles to soundfiles & space & quoted form of (POSIX path of soundfile)
			end if
		end repeat
		-- playing the sound files using a Python script utilizing NSSound
		set pyscript to POSIX path of (((path to me) as Unicode text) & "Contents:Resources:macsound.py")
		set command to "python " & quoted form of pyscript & soundfiles
		set command to command as «class utf8»
		do shell script command
		-- catching unexpected errors 
	on error errmsg number errnum
		tell me
			activate
			display dialog "Sorry, an error occured" & return & return & errmsg & " (" & errnum & ")" buttons {"OK"} default button 1 with icon caution with title myname
		end tell
	end try
end main

Hope that helps :smiley:

Playing a sound file in AppleScript is easier than that; use afplay:

do shell script "afplay '/System/Library/Sounds/Hero.aiff'"

Great tip, thanks a lot!

Oh. “afplay” works a lot better. Thanks.

Must be missing something here. This script won’t compile or run.

Carl

It’s running for me.
Perhaps your ‘Hero’ sound is localized under a different name?
The useable names can be found in System Preferences → Sound.

I like what the rate option does to Frog:

	do shell script "afplay '/System/Library/Sounds/Frog.aiff'"
	do shell script "afplay -r  .1 '/System/Library/Sounds/Frog.aiff'"