Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Used separate LoginItemManager class.
I wrote this in another project. Fixed bugs and stuff.
- Loading branch information
Showing
6 changed files
with
181 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// LoginItemManager.h | ||
// AppHider | ||
// | ||
// Created by Dustin Sallings on 3/23/11. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
|
||
@interface LoginItemManager : NSObject { | ||
@private | ||
|
||
} | ||
|
||
-(BOOL)inLoginItems; | ||
-(void)removeLoginItem:(id)sender; | ||
-(void)addToLoginItems:(id)sender; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// | ||
// LoginItemManager.m | ||
// AppHider | ||
// | ||
// Created by Dustin Sallings on 3/23/11. | ||
// | ||
|
||
#import "LoginItemManager.h" | ||
|
||
|
||
@implementation LoginItemManager | ||
|
||
- (id)init | ||
{ | ||
self = [super init]; | ||
if (self) { | ||
// Initialization code here. | ||
} | ||
|
||
return self; | ||
} | ||
|
||
- (void)dealloc | ||
{ | ||
[super dealloc]; | ||
} | ||
|
||
-(BOOL) inLoginItems { | ||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; | ||
BOOL rv = NO; | ||
|
||
[defaults addSuiteNamed:@"loginwindow"]; | ||
|
||
NSMutableArray *loginItems=[[[defaults | ||
persistentDomainForName:@"loginwindow"] | ||
objectForKey:@"AutoLaunchedApplicationDictionary"] mutableCopy]; | ||
|
||
// Remove anything that looks like the current login item. | ||
NSString *myName=[[[NSBundle mainBundle] bundlePath] lastPathComponent]; | ||
NSEnumerator *e=[loginItems objectEnumerator]; | ||
id current=nil; | ||
while( (current=[e nextObject]) != nil) { | ||
if([[current valueForKey:@"Path"] hasSuffix:myName]) { | ||
rv = YES; | ||
} | ||
} | ||
|
||
return rv; | ||
} | ||
|
||
-(void) removeLoginItem:(id)sender { | ||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; | ||
|
||
[defaults addSuiteNamed:@"loginwindow"]; | ||
|
||
NSMutableArray *loginItems=[[[defaults | ||
persistentDomainForName:@"loginwindow"] | ||
objectForKey:@"AutoLaunchedApplicationDictionary"] mutableCopy]; | ||
|
||
// Remove anything that looks like the current login item. | ||
NSString *myName=[[[NSBundle mainBundle] bundlePath] lastPathComponent]; | ||
NSEnumerator *e=[loginItems objectEnumerator]; | ||
id current=nil; | ||
while( (current=[e nextObject]) != nil) { | ||
if([[current valueForKey:@"Path"] hasSuffix:myName]) { | ||
NSLog(@"Removing login item: %@", [current valueForKey:@"Path"]); | ||
[loginItems removeObject:current]; | ||
break; | ||
} | ||
} | ||
|
||
[defaults removeObjectForKey:@"AutoLaunchedApplicationDictionary"]; | ||
[defaults setObject:loginItems forKey: | ||
@"AutoLaunchedApplicationDictionary"]; | ||
|
||
// Use the corefoundation API since I can't figure out the other one. | ||
CFPreferencesSetValue((CFStringRef)@"AutoLaunchedApplicationDictionary", | ||
loginItems, (CFStringRef)@"loginwindow", kCFPreferencesCurrentUser, kCFPreferencesAnyHost); | ||
CFPreferencesSynchronize((CFStringRef) @"loginwindow", kCFPreferencesCurrentUser, kCFPreferencesAnyHost); | ||
|
||
} | ||
|
||
-(void)addToLoginItems:(id)sender { | ||
|
||
[self removeLoginItem: self]; | ||
|
||
NSMutableDictionary *myDict=[[NSMutableDictionary alloc] init]; | ||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; | ||
|
||
[defaults addSuiteNamed:@"loginwindow"]; | ||
|
||
NSLog(@"Adding login item: %@", [[NSBundle mainBundle] bundlePath]); | ||
[myDict setObject:[NSNumber numberWithBool:NO] forKey:@"Hide"]; | ||
[myDict setObject:[[NSBundle mainBundle] bundlePath] | ||
forKey:@"Path"]; | ||
|
||
NSMutableArray *loginItems=[[[defaults | ||
persistentDomainForName:@"loginwindow"] | ||
objectForKey:@"AutoLaunchedApplicationDictionary"] mutableCopy]; | ||
|
||
[loginItems addObject:myDict]; | ||
[defaults removeObjectForKey:@"AutoLaunchedApplicationDictionary"]; | ||
[defaults setObject:loginItems forKey: | ||
@"AutoLaunchedApplicationDictionary"]; | ||
|
||
// Use the corefoundation API since I can't figure out the other one. | ||
CFPreferencesSetValue((CFStringRef)@"AutoLaunchedApplicationDictionary", | ||
loginItems, (CFStringRef)@"loginwindow", kCFPreferencesCurrentUser, kCFPreferencesAnyHost); | ||
CFPreferencesSynchronize((CFStringRef) @"loginwindow", kCFPreferencesCurrentUser, kCFPreferencesAnyHost); | ||
|
||
[myDict release]; | ||
[loginItems release]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.