Playing a sound

Hi

How can I load and play a (system) sound?

Heiner

You can do this in AppleScript with afplay:

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

Try something like:

 current application's NSSound's soundNamed_("Submarine")'s play()

Thanks to Caleb531 and Shane!

This works:

But that is odd:

The compiler doesn’t like it: “Expected expression but found command name.”
But when I build the app nevertheless, it sounds. (???)

Perhaps they messed up for once. :stuck_out_tongue:

The same thing happens with the compiler for setting a title for a dialog window such as:
display dialog “Test” with title “Test”

It looks like “play” is a reserved AppleScript word, so you need to add pipes around it:

		current application's NSSound's soundNamed_("Submarine")'s |play|()

That looks like a straight terminology conflict. You can get around it using the raw terms for just the title:

display dialog "Test" given «class appr»:"Test"

Shane,

first the good news:

Indeed, ‘play’ is a reserved word of AS. The error message is gone after inserting the pipe.

The bad news:

Silence around.

Heiner

Are you sure you put the pipes in the right place? I can hear the submarine here…

Sorry Shane,

I’m a fool. I’ve forgotten to add the recource to the project. Now it sounds!!!

Heiner

Actually, you shouldn’t have to add it to the project, as long as it’s a standard sound. From the docs:

I didn’t add it my app, and it still played. But maybe you’re actually using a non-system sound.

Shane, thanks for your remarks!

I found out, that my system sounds are only in /System/Library/Sounds. All the others folders are empty. I don’t know why, I repaired it.

Heiner

That should be enough for the app to find them.