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