Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Add login items programmatically

Add login items programmatically
Thread Tools
Fresh-Faced Recruit
Join Date: Jan 2003
Location: Kaysville, UT
Status: Offline
Reply With Quote
May 15, 2003, 04:01 PM
 
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
Reply With Quote
May 16, 2003, 10:24 AM
 
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
Reply With Quote
May 16, 2003, 11:35 AM
 
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
Reply With Quote
May 16, 2003, 01:51 PM
 
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
Reply With Quote
May 16, 2003, 02:02 PM
 
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
Reply With Quote
May 24, 2003, 02:05 AM
 
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
Reply With Quote
May 24, 2003, 09:42 AM
 
Originally posted by 3R1C:
Here you go.
He's using Java.
     
Fresh-Faced Recruit
Join Date: Jan 2003
Location: Kaysville, UT
Status: Offline
Reply With Quote
May 27, 2003, 08:32 AM
 
Yep, Java, but thanks for the reply anyway!

Jim
     
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
May 27, 2003, 01:07 PM
 
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
Reply With Quote
May 27, 2003, 01:21 PM
 
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
Reply With Quote
Jun 17, 2003, 11:53 PM
 
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
Reply With Quote
Jun 18, 2003, 09:12 AM
 
Originally posted by moonmonkey:
Does anyone know a shell command to do this?
man defaults
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 03:33 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2