6

I am developing an application that I want to start automatically when the user logs in. There are several answers on how to do this, in particular I'm using the code from this GitHub repository, and it works fine.

What I want now, and couldn't find how to do it, is start the application but without showing the main window. This is only when the application starts on login, if the application is closed and the user opens it with a click in the Dock (or whatever), I want it to show the window.

Is it possible? Any ideas on how to do this?

In the Accounts system preference, where you set the applications that launch on login, there is a "hide" check that does what I want, but I want to do it programmatically.

informatik01
  • 16,038
  • 10
  • 74
  • 104
Marcos Crispino
  • 8,018
  • 5
  • 41
  • 59

1 Answers1

10

Well, I've found how to do it... This Open Radar bug report helped, I was using the wrong property.

Here's the code:

- (void)enableLoginItemWithLoginItemsReference:(LSSharedFileListRef )theLoginItemsRefs ForPath:(NSString *)appPath {
// We call LSSharedFileListInsertItemURL to insert the item at the bottom of Login Items list.
CFURLRef url = (CFURLRef)[NSURL fileURLWithPath:appPath];

CFMutableDictionaryRef inPropertiesToSet = CFDictionaryCreateMutable(NULL, 1, NULL, NULL);
CFDictionaryAddValue(inPropertiesToSet, kLSSharedFileListLoginItemHidden, kCFBooleanTrue);

LSSharedFileListItemRef item = LSSharedFileListInsertItemURL(theLoginItemsRefs, kLSSharedFileListItemLast, NULL, NULL, url, inPropertiesToSet, NULL);       
if (item) {
    CFRelease(item);
}
}

The solution was to create a dictionary with the key kLSSharedFileListLoginItemHidden and value true, and pass it to the LSSharedFileListInsertItemURL function.

Marcos Crispino
  • 8,018
  • 5
  • 41
  • 59
  • Hi Marcos, I am having the same issue on my Mac OS X application. I tried your code, it sets as hidden(Tick enabled) in User&Groups->LoginItems for my application after i installed my app on Mac OS X Lion. But, when i reboot my machine, it opens up my application window, which i wanted to hide UIWindow instead of showing automatically. Could you please advise me how can i fix it exactly? – Getsy Aug 05 '12 at 19:31
  • Thanks for this answer but how do I know if the app has been launched automatically or by the user independently of the fact to display or not the main window ? Thanks !! – toto_tata Jan 04 '13 at 17:36
  • @marcos-crispino Can you please post the code where you *check* to see if the app was opened at login vs. opened manually? – ck_ Apr 22 '13 at 20:39