[Obj-C] How can I use DiskArbitration framework in AppleScript Studio?

Hi,

about two weeks ago I released the freeware TimeMachineScheduler, a little AppleScript Studio application
to change the backup interval of Time Machine in Leopard.

I’m just working on an improved new version, which should display the (mounted) status of the specified backup volume.
Of course it’s easy to poll the contents of /Volumes to detect if the volume is available.

But this way is quite silly, because it wastes too many resources.
The second method I’m considering is to install an own launchd daemon, which does the “watchdog” job.

Then I discovered that the system has a framework, which does exactly what I need providing a callback function if a volume appears or disappears.

Here is my question:
How can I use DiskArbitration framework to change a string in a text field and assign a certain image to an image view
depending on the status of the volume?

I found this Obj-C notification snippet in the net, but I don’t know how to integrate it in my AppleScript Studio project

[code]#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);

}[/code]
Can anybody help?

Your main problem is that AppleScriptKit isn’t a proper two-way bridge like PyObjC or RubyCocoa, and while it does provide a ‘call method’ command for invoking arbitrary ObjC methods from AppleScript, it doesn’t provide a formal mechanism for invoking arbitrary AppleScript handlers from ObjC. Studio users have come up with various kludges for working around this limitation, e.g. to provide Studio-based apps with basic scripting support. Try searching here and on the AppleScript-Studio mailing list for previous discussions on calling AppleScript code from ObjC code in Studio apps.

You might also consider looking into Python+PyObjC, Ruby+RubyCocoa or even straight ObjC+Cocoa. While learning the proper Cocoa APIs does take a bit of time and effort, they’re much more power and flexible than the ASK and well worth the investment if your needs are starting to exceed Studio’s capabilities.

HTH

Thanks for your reply.

Actually I don’t need a two-way bridge.
The Obj-C part should only change the value of the text field and the image view independent from the AppleScript part

I’ve begun to read the Objective-C 2.0 Programming Language,
but honestly this overstrains me at the moment.

I hoped that it’s nothing for our experienced Obj-C cracks :wink: