This is a pretty old program, goes all the way back to System 7 sounds, but it still works fine on El Cap. Except, I’m getting an error in this script which I can’t resolve. I’m trying to get a reference to the Desktop that will work on any Desktop and not just mine.
set SoundsFolder to ((path to desktop as string) & "ChordMate:ChordMateSounds:") as alias
--return SoundsFolder = alias "Mac-5K:Users:roy:Desktop:ChordMate:ChordMateSounds:"
tell application "Play Sound" --Play Sound is always running in the background
--play "SoundsFolder" & "qq_Launcher 6.aiff"
--error "Play Sound got an error: File some object wasn't found." number -43
-- but this works fine
play "Mac-5K:Users:roy:Desktop:ChordMate:ChordMateSounds:qq_Launcher 6.aiff"
end tell
Thanks, Stefan, but I’m still getting the same error.
set SoundsFolder to (path to desktop as string) & "ChordMate:ChordMateSounds:"
tell application "Play Sound"
play "SoundsFolder" & "qq_Launcher 6.aiff"
end tell
error "Play Sound got an error: File some object wasn't found." number -43
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AVFoundation"
set posixPath to POSIX path of (choose file of type {"public.audio"})
set theURL to current application's class "NSURL"'s fileURLWithPath:posixPath
set thePlayer to current application's AVAudioPlayer's alloc()'s initWithContentsOfURL:theURL |error|:(missing value)
thePlayer's play()
Or if you want fade outs, something like this (untested) as a stay-open script:
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AVFoundation"
property thePlayer : missing value
property idleTime : missing value
set posixPath to POSIX path of (choose file of type {"public.audio"})
set theURL to current application's class "NSURL"'s fileURLWithPath:posixPath
set my thePlayer to current application's AVAudioPlayer's alloc()'s initWithContentsOfURL:theURL |error|:(missing value)
thePlayer's play()
set theDuration to thePlayer's duration()
set my idleTime to theDuration / 99
thePlayer's setVolume:1.0
on idle
if thePlayer's isPlaying() as boolean then
set theVolume to thePlayer's volume() as real
if theVolume > 0.1 then
thePlayer's setVolume:(theVolume - 0.01)
else
thePlayer's |stop|()
tell me to quit
end if
return idleTime
else
tell me to quit
end if
end idle
on quit
continue quit
end quit
As for Play Sound alternatives, it would take a killer program to wean me away from Play Sound. Here are some of the things it can do (from the author’s site). You can play an unlimited number sounds concurrently or one at a time. You can loop sounds indefinitely, repeat sounds a certain number of times, play specified portions of sounds, control the volume of sounds played, and press the Escape or Command-Period keys to abort sound play. Play Sound is fully scriptable.
For me, the feature that really stands out is volume control. In the same line of code that calls the sound, just supply a number from 1-255 for volume. It works great. Fade in/out by looping the script and incrementing the volume number.
I have my prefs set to open all double clicked sound files in Play Sound, which is always running in the background. This is faster than Quick Look and doesn’t quit playing just because I click on something else. Also, it doesn’t cutter up my iTunes Library by opening double clicked sound snippets in iTunes.
As long as it keeps working, go for it – I was really just pointing out that there’s an alternative to afplay these days. But unless you want to play sounds within a script, an app is definitely the best way to go.
However, if something does happen, AVAudioPlayer can be wrapped as a stay-open script and do all the things you mention.