Archive for the ‘macosx’ Category

Playing sounds from the command line

Tuesday, June 5th, 2007

On the NeXT machine, there was a command called sndplay that would play .snd files from the command line.

It’s not too tough to put together a similar one for Mac OS X. We can play many more kinds of sounds than the old NeXT sndplay command did. On the other hand, since NSSound uses QuickTime to play some media formats, a run loop is required for sounds to keep playing, so that requires a few gyrations.

sndplay.m

(See also this cocoa-dev message)

Command-line compiling of Cocoa code

Wednesday, May 30th, 2007

I find that tools like Xcode are often too heavyweight when trying out little fragments of code. In these cases, it can be simpler to use the traditional Unix tools to edit, compile, and run tiny test programs. (Of course, it might be simpler for me because I’m used to the traditional Unix way.)

For instance, say that you have created a category on NSMutableArray that adds a method to reverse the contents of the array, and you’re ready to test it out.

You could create an Xcode project for this (you’d use the Foundation Tool template), but it’s also possible to use a Unix text editor (like emacs or vi) to create the file that contains your category, together with a simple main() function. (Here is an example file.)

There are basically two tricks to know. The first is that you need to create an autorelease pool before you call any methods, or else you’ll see warning messages about leaking objects. The other trick is how to compile the file, and that is simply

cc file.m -framework Foundation

Now, just run a.out. Debug, edit source, re-compile, repeat.

Determining the default route

Monday, February 5th, 2007

A subscriber on cocoa-dev@lists.apple.com was asking whether there was some way to programmatically determine the default route.

The cheap and sleazy way would be to read the output of netstat, but that’s not very aesthetically appealing.

Here is some sample code, derived from the source for netstat, which will find and print the IPv4 default route.

Getting disk insertion/removal notifications

Monday, February 5th, 2007

On Mac OS X, there is a daemon called diskarbitrationd that can notifiy interested clients of the appearance of disks and filesystems. Users talk to diskarbitrationd via the Disk Arbitration framework.

For some reason, the current developer documentation doesn’t have much to say about this framework. It therefore appears to be necessary to grovel through the headers to figure out how to use it. Fortunately, the headers are well-commented.

I was able to throw together a trivial program in about 15 or 20 minutes from first looking at DiskArbitration.h. Maybe this will help someone out there.

#include <stdio.h>
#include <DiskArbitration/DiskArbitration.h>

void hello_disk(DADiskRef disk, void *context)
{
    printf("disk %s appeared\n", DADiskGetBSDName(disk));
}

void goodbye_disk(DADiskRef disk, void *context)
{
    printf("disk %s disappeared\n", DADiskGetBSDName(disk));
}

main()
{
    DASessionRef session;

    session = DASessionCreate(kCFAllocatorDefault);

    DARegisterDiskAppearedCallback(session, NULL, hello_disk, NULL);
    DARegisterDiskDisappearedCallback(session, NULL, goodbye_disk, NULL);

    DASessionScheduleWithRunLoop(session,
        CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);

    CFRunLoopRun();

    CFRelease(session);
    exit(0);
}