Okay. I was looking for a neat way to list system sounds that are played with [NSSound soundNamed: ] - and to me it seemed that API does not have list of available sounds.
I also searched this with google and found some pretty old and partially already deprecated sources. If application should have a soundpicker - what would be a best way to get list of system provided sounds?
This is my implementation.
NSSound (systemSounds.h):
#import <AppKit/AppKit.h>
@interface NSSound (systemSounds)
+ (NSArray *) systemSounds;
@end
NSSound (systemSounds.m):
#import "NSSound (systemSounds).h"
@implementation NSSound (systemSounds)
static NSArray *systemSounds = nil;
+ (NSArray *) systemSounds
{
if ( !systemSounds )
{
NSMutableArray *returnArr = [[NSMutableArray alloc] init];
NSEnumerator *librarySources = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSAllDomainsMask, YES) objectEnumerator];
NSString *sourcePath;
while ( sourcePath = [librarySources nextObject] )
{
NSEnumerator *soundSource = [[NSFileManager defaultManager] enumeratorAtPath: [sourcePath stringByAppendingPathComponent: @"Sounds"]];
NSString *soundFile;
while ( soundFile = [soundSource nextObject] )
if ( [NSSound soundNamed: [soundFile stringByDeletingPathExtension]] )
[returnArr addObject: [soundFile stringByDeletingPathExtension]];
}
systemSounds = [[NSArray alloc] initWithArray: [returnArr sortedArrayUsingSelector:@selector(compare:)]];
[returnArr release];
}
return systemSounds;
}
@end
Freely usable and available for anyone for any purpose and if you enhance it, please share :)