Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mac OS X: open application at login, without showing the main window

Tags:

macos

cocoa

login

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.

like image 649
Marcos Crispino Avatar asked Apr 08 '11 15:04

Marcos Crispino


People also ask

How do I make an app default to open on my second display Mac?

With the app open control-click on the apps icon in the Dock, select Options and you should see a second sub menu where you can assign which Display to use for that app.

How do I stop apps from opening in full screen on Mac?

To stop using the app full screen, move the pointer to the green button again, then choose Exit Full Screen from the menu that appears or click the button .


1 Answers

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.

like image 186
Marcos Crispino Avatar answered Feb 13 '23 09:02

Marcos Crispino