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

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

What I want now, and I can’t find how to do it, launches the application, but does not show the main window. This only happens when the application starts at login, if the application is closed and the user opens it by clicking on the Dock (or something else), I want it to show a window.

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

In the settings of the account system, where you install applications that run when you log in, there is a β€œhidden” check that does what I want, but I want to do it programmatically.

+6
source share
1 answer

Well, I found how to do it ... This Open Radar bug report helped , I used the wrong property.

Here is 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 true and pass it to the LSSharedFileListInsertItemURL function.

+10
source

Source: https://habr.com/ru/post/885520/


All Articles