.comment-link {margin-left:.6em;}

The words are just like ... words, I guess.

Friday, April 04, 2008

First useful piece of Mac Code!

Hey everybody...


Today in response to some of my own tinkering and the forum at http://discussions.apple.com/thread.jspa?messageID=6978579#6978579 I have created my first useful program in Objective-C and MacOS. The utility is a command line application that will mount and return the path to the Leopard time machine volume.


The utility can be downloaded here http://rapidshare.de/files/39024900/GetTimeMachinePath.html


The souce code is as follows:



#import <Foundation/Foundation.h>


#import <AppKit/AppKit.h>


#import <CoreData/CoreData.h>



FSRef* getTimeMachineVolume()


{


NSData *timeMachinePList = [NSData dataWithContentsOfFile:@"/Library/Preferences/com.apple.TimeMachine.plist"];


NSString *errorText;


NSDictionary *timeMachineProperties = [NSPropertyListSerialization propertyListFromData:timeMachinePList mutabilityOption:NSPropertyListImmutable format:NULL errorDescription:&errorText];


NSData *backupAlias = [timeMachineProperties objectForKey:@"BackupAlias"];



AliasHandle aliasHandle = (AliasHandle)NewHandle([backupAlias length]);



[backupAlias getBytes: *aliasHandle];



FSRef *volumePath = malloc(sizeof(FSRef));


Boolean wasChanged;



OSErr resultOfResolve = FSResolveAlias(NULL, aliasHandle, volumePath, &wasChanged);


if (resultOfResolve != noErr)


return nil;



DisposeHandle((Handle)aliasHandle);



return volumePath;


}



int main (int argc, const char * argv[]) {


NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];



// insert code here...


char *timeMachinePOSIXPathString = malloc(1024 * sizeof(char));


FSRef *timeMachineFSRef = getTimeMachineVolume();


FSRefMakePath(timeMachineFSRef, (UInt8*)timeMachinePOSIXPathString, 1024 * sizeof(char));


free(timeMachineFSRef);



printf(timeMachinePOSIXPathString);



[pool drain];


return 0;


}




Labels:

1 Comments:

  • I wouldn't use printf directly on an unknown string: it might crash if that string contains a percent character. I'd write something like printf("%s",str) or call directly fwrite.

    By Blogger Yves, at 30/5/08 3:00 AM  

Post a Comment

Links to this post:

Create a Link

<< Home