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

How can I play a sound using Cocoa-Applescript in Xcode?

I need to play a sound file called "click.aiff" whenever a button is clicked, that is bundled with my application. How can I do this without using QuickTimePlayer?

Posted on Sep 17, 2013 1:33 PM

Reply
Question marked as Best reply

Posted on Sep 17, 2013 8:16 PM

Check out the NSSound Class Reference. You can add a property for the sound and initialize it with something like:

set mySound to current application's NSSound's soundNamed_("your sound name")


The system will check for an installed sound file in the Sounds folders, and then in your application bundle if it is not found (note that the extension is not used). You can then play it by using the play method:

mySound's play()


Also note that if you are going to use one of the pre-installed sounds, you can assign a sound for the button in the Attributes Inspector in the Interface Editor.

8 replies
Question marked as Best reply

Sep 17, 2013 8:16 PM in response to Apple_For_The_Win

Check out the NSSound Class Reference. You can add a property for the sound and initialize it with something like:

set mySound to current application's NSSound's soundNamed_("your sound name")


The system will check for an installed sound file in the Sounds folders, and then in your application bundle if it is not found (note that the extension is not used). You can then play it by using the play method:

mySound's play()


Also note that if you are going to use one of the pre-installed sounds, you can assign a sound for the button in the Attributes Inspector in the Interface Editor.

Sep 17, 2013 8:16 PM in response to Apple_For_The_Win

If you could use one of the built-in sounds it would be easier, just


on playSound_(sender)


tellclass"NSSound"of current application

set soundInstance to its soundNamed_("Pop")

end


soundInstance's setVolume_(1.0)

soundInstance's |play|()

end

Just put the sound name you want to use in place of Pop


If you have to use the sound file then first you need to know where it is. The easiest is to have it as part of the bundle then you can use the pathToResource:ofType: method that finds files of a given type in the Resourse folder of the applications bundle.


So


tell class "NSBundle" of current application

tell its mainBundle

set soundFilePath to its pathForResource_ofType_("click", "aiff")

end tell

end tell


gets the sound file name in soundFilePath


then to play it first load it


tell class "NSSound" of current application

set soundInstance to its alloc()'s initWithContentsOfFile_byReference_(soundFilePath, true)

end tell


then play it as above


soundInstance's setVolume_(1.0)

soundInstance's |play|()

I beleive that should do it.


regards

Sep 18, 2013 7:59 AM in response to Apple_For_The_Win

clicksound is not being set, most likely it is not finding the click.aiff file.


As a test change "click" to "Pop" and see if it works. Pop is one of the default sounds. If it works then you know the problem is not being able to find your sound file.


In cases like this testiog to make sure the class was set by doing somehting like

if clicksound is equal to missing value then

display alert "missing"

else

clicksound's play()

end

is a good habit to get into.


Message was edited by: Frank Caggiano

How can I play a sound using Cocoa-Applescript in Xcode?

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