aboutsummaryrefslogtreecommitdiffstats
path: root/BatchDMG.m
diff options
context:
space:
mode:
authorFilipp Lepalaan <filipp@mac.com>2010-11-10 02:01:47 +0200
committerFilipp Lepalaan <filipp@mac.com>2010-11-10 02:01:47 +0200
commitf07a6bbec16df415fcfe2309612805194da052e3 (patch)
tree998e8aa9aa5d0cb1e4db402cacb5a3bf09abd74a /BatchDMG.m
parent633ea77816a9c82b32ac85f7a6741cc4e4810971 (diff)
downloadBatchDMG-f07a6bbec16df415fcfe2309612805194da052e3.tar.gz
BatchDMG-f07a6bbec16df415fcfe2309612805194da052e3.tar.bz2
BatchDMG-f07a6bbec16df415fcfe2309612805194da052e3.zip
rewrite in objective c
Diffstat (limited to 'BatchDMG.m')
-rwxr-xr-xBatchDMG.m126
1 files changed, 126 insertions, 0 deletions
diff --git a/BatchDMG.m b/BatchDMG.m
new file mode 100755
index 0000000..6eebaca
--- /dev/null
+++ b/BatchDMG.m
@@ -0,0 +1,126 @@
+#import <Foundation/Foundation.h>
+#import <AppKit/AppKit.h>
+
+@interface Imager : NSObject
+{
+ BOOL g_observing;
+ NSString *destination;
+}
+
+- (void)observeWorkspace: (NSNotification*) notification;
+- (void)checkTaskStatus: (NSNotification*) notification;
+
+@end
+
+@implementation Imager
+
+- (id) init
+{
+ [super init];
+
+ // observer for disk mounts
+ if (!g_observing)
+ {
+ NSLog(@"Waiting for media...");
+ NSNotificationCenter* center;
+
+ center = [[NSWorkspace sharedWorkspace] notificationCenter];
+ [center addObserver: self
+ selector: @selector(observeWorkspace:)
+ name: @"NSWorkspaceDidMountNotification"
+ object: nil];
+
+ g_observing = YES;
+ }
+
+ // observer for finished rips
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(checkTaskStatus:)
+ name:NSTaskDidTerminateNotification
+ object:nil];
+
+ return self;
+}
+
+- (void)checkTaskStatus:(NSNotification *)aNotification
+{
+ int status = [[aNotification object] terminationStatus];
+ NSLog(@"Task completed with status %d", status);
+ id args = [[aNotification object] arguments];
+ NSLog(@"%@", args);
+
+ if (status == 0)
+ {
+ NSLog(@"Task succeeded.");
+ if ([args length] > 4)
+ {
+ NSString *path = [args objectAtIndex:4];
+ NSArray *eject = [NSArray arrayWithObjects:@"eject", @"-quiet", path, nil];
+ [NSTask launchedTaskWithLaunchPath:@"/usr/bin/hdiutil" arguments:eject];
+ }
+
+ }
+}
+
+- (void)dealloc
+{
+ if (g_observing)
+ {
+ NSNotificationCenter* center;
+
+ // remove notifications within our app
+ center = [NSNotificationCenter defaultCenter];
+ [center removeObserver: self];
+
+ // remove notifications from NSWorkspace
+ center = [[NSWorkspace sharedWorkspace] notificationCenter];
+ [center removeObserver: self];
+
+ // remove machine-wide notifications
+ center = [NSDistributedNotificationCenter defaultCenter];
+ [center removeObserver: self];
+
+ g_observing = NO;
+ }
+
+ [super dealloc];
+
+}
+
+- (void)observeWorkspace: (NSNotification*)notification
+{
+ id ui = [notification userInfo];
+ id path = [ui valueForKey:@"NSDevicePath"];
+ id volName = [ui valueForKey:@"NSWorkspaceVolumeLocalizedNameKey"];
+
+ NSLog(@"Imaging: %@", volName);
+
+ NSTask *dmg = [[NSTask alloc] init];
+ NSMutableArray *args = [NSMutableArray arrayWithObjects:@"create",
+ @"-format",
+ @"UDBZ",
+ @"-srcfolder",
+ path,
+ volName,
+ nil];
+
+ [dmg setCurrentDirectoryPath:@"/tmp/"];
+ [dmg setLaunchPath:@"/usr/bin/hdiutil"];
+ [dmg setArguments:args];
+ [dmg launch];
+
+}
+
+@end
+
+int main (int argc, const char * argv[])
+{
+ NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
+
+ //NSRunLoop *rl = [NSRunLoop currentRunLoop];
+ //[[Imager alloc] init];
+ //[rl run];
+ NSLog(@"%d", argv[1]);
+ [pool release];
+ return 0;
+}