+
+ //Here we set whether we will launch at login by modifying loginwindow.plist
+ if ([launchAtLoginCheckbox state] == NSOnState) {
+ NSMutableDictionary *loginwindow;
+ NSMutableArray *loginarray;
+ ComponentInstance temp = OpenDefaultComponent(kOSAComponentType, kAppleScriptSubtype);;
+ int i;
+ BOOL skip = NO;
+
+ [defaults synchronize];
+ loginwindow = [[defaults persistentDomainForName:@"loginwindow"] mutableCopy];
+ loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
+
+ for (i = 0; i < [loginarray count]; i++) {
+ NSDictionary *tempDict = [loginarray objectAtIndex:i];
+ if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
+ skip = YES;
+ }
+ }
+
+ if (!skip) {
+ AEDesc scriptDesc, resultDesc;
+ NSString *script = [NSString stringWithFormat:@"tell application \"System Events\"\nmake new login item at end of login items with properties {path:\"%@\", kind:\"APPLICATION\"}\nend tell", [[NSBundle mainBundle] bundlePath]];
+
+ AECreateDesc(typeChar, [script cString], [script cStringLength],
+ &scriptDesc);
+
+ OSADoScript(temp, &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
+
+ AEDisposeDesc(&scriptDesc);
+ AEDisposeDesc(&resultDesc);
+ CloseComponent(temp);
+ }
+ } else {
+ NSMutableDictionary *loginwindow;
+ NSMutableArray *loginarray;
+ int i;
+
+ [[NSUserDefaults standardUserDefaults] synchronize];
+ loginwindow = [[[NSUserDefaults standardUserDefaults] persistentDomainForName:@"loginwindow"] mutableCopy];
+ loginarray = [loginwindow objectForKey:@"AutoLaunchedApplicationDictionary"];
+
+ for (i = 0; i < [loginarray count]; i++) {
+ NSDictionary *tempDict = [loginarray objectAtIndex:i];
+ if ([[[tempDict objectForKey:@"Path"] lastPathComponent] isEqualToString:[[[NSBundle mainBundle] bundlePath] lastPathComponent]]) {
+ [loginarray removeObjectAtIndex:i];
+ [defaults setPersistentDomain:loginwindow forName:@"loginwindow"];
+ [defaults synchronize];
+ break;
+ }
+ }
+ }