 |
 |
Add login items programmatically
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jan 2003
Location: Kaysville, UT
Status:
Offline
|
|
Is it possible to add login items programmatically in OS X? I want to add a shared folder to the login items via some sort of API call if there is one.
Thanks,
Jim
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
You'd have to use CFPreferences and modify it directly (lots of things do that).
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jan 2003
Location: Kaysville, UT
Status:
Offline
|
|
Originally posted by Angus_D:
You'd have to use CFPreferences and modify it directly (lots of things do that).
Ok, thanks! Now that I've got your attention, ever done Java with JNI on Mac OS? I would like to do this from a Java app and I think that the path I'll need to take is JNI. I've found an example on the ADC, but it would be nice to have some more basic info about it.
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
Actually you could probably do it from native java using NSUserDefaults (in com.apple.cocoa.Foundation or similar).
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jan 2003
Location: Kaysville, UT
Status:
Offline
|
|
Originally posted by Angus_D:
Actually you could probably do it from native java using NSUserDefaults (in com.apple.cocoa.Foundation or similar).
Thanks for saving me from an dangerous path! I'll take a look around those apple classes. I'm just barely getting my feet wet with development on the Mac. Downloading the Developer Tools and Java tools from ADC as I write.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Oct 2001
Status:
Offline
|
|
Here you go.
void addToLoginItems( NSString* path, BOOL hide) {
NSString *loginwindow = @"loginwindow";
NSUserDefaults *u;
NSMutableDictionary *d;
NSDictionary *e;
NSMutableArray *a;
/* get data from user defaults
(~/Library/Preferences/loginwindow.plist) */
u = [[NSUserDefaults alloc] init];
if( !(d = [[u persistentDomainForName:loginwindow] mutableCopyWithZone:
NULL]) )
d = [[NSMutableDictionary alloc] initWithCapacity:1];
if( !(a = [[d objectForKey:@"AutoLaunchedApplicationDictionary"]
mutableCopyWithZone:NULL]) )
a = [[NSMutableArray alloc] initWithCapacity:1];
/* build entry */
e = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithBool:hide], @"Hide",
path, @"Path",
nil];
/* add entry */
if( e )
{
[a insertObject:e atIndex:0];
[d setObject:a forKey:@"AutoLaunchedApplicationDictionary"];
}
/* update user defaults */
[u removePersistentDomainForName:loginwindow];
[u setPersistentDomain:d forName:loginwindow];
[u synchronize];
/* clean up */
[e release];
[a release];
[d release];
[u release];
}
void removeLoginItem( NSString* path) {
NSString *loginwindow = @"loginwindow";
NSUserDefaults *u;
NSMutableDictionary *d;
NSMutableArray *a;
NSEnumerator *enumerator;
id anObject;
id theObject = nil;
/* get data from user defaults
(~/Library/Preferences/loginwindow.plist) */
u = [[NSUserDefaults alloc] init];
if( d = [[u persistentDomainForName:loginwindow] mutableCopyWithZone:
NULL] ) {
if( a = [[d objectForKey:@"AutoLaunchedApplicationDictionary"]
mutableCopyWithZone:NULL] ) {
/* remove entry */
enumerator = [a objectEnumerator];
while (anObject = [enumerator nextObject]) {
if ([[anObject objectForKey:@"Path"] isEqualToString:path]) {
theObject = anObject;
}
}
[a removeObject:theObject];
[d setObject:a forKey:@"AutoLaunchedApplicationDictionary"];
[u removePersistentDomainForName:loginwindow];
[u setPersistentDomain:d forName:loginwindow];
[u synchronize];
[a release];
}
/* clean up */
[d release];
[u release];
}
}
(Last edited by 3R1C; Nov 10, 2003 at 10:03 PM.
)
|
3R1C
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
Originally posted by 3R1C:
Here you go.
He's using Java. 
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jan 2003
Location: Kaysville, UT
Status:
Offline
|
|
Yep, Java, but thanks for the reply anyway!
Jim
|
|
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
There shouldn't be that much difference between the Cocoa/Java and Cocoa/Obj-C code. Glancing at it, the only thing I notice is the constructors ([[Something alloc] init] and [something mutableCopyWithZone:]). Those would have to be replaced with native calls, but otherwise it's a simple syntactic conversion.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Jan 2003
Location: Kaysville, UT
Status:
Offline
|
|
Originally posted by Chuckit:
There shouldn't be that much difference between the Cocoa/Java and Cocoa/Obj-C code. Glancing at it, the only thing I notice is the constructors ([[Something alloc] init] and [something mutableCopyWithZone:]). Those would have to be replaced with native calls, but otherwise it's a simple syntactic conversion.
It is true that it could be ported. I've had another project bubble up to the top in importance, so no work is happening on the one that needs the login item. But I have saved off the C code to review later.
Thanks,
Jim
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Jan 2001
Location: Between Sydney and Melbourne
Status:
Offline
|
|
Originally posted by Angus_D:
You'd have to use CFPreferences and modify it directly (lots of things do that).
Does anyone know a shell command to do this?
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Jan 2000
Status:
Offline
|
|
Originally posted by moonmonkey:
Does anyone know a shell command to do this?
man defaults
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|