Mac OS X:在登录时打开应用程序,而不显示主窗口

问题描述:

我正在开发一个应用程序,我想在用户登录时自动启动。有几个答案如何做,特别是我使用的代码从这个GitHub存储库,它工作正常。

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.

,并且找不到如何做,是启动应用程序,但没有显示主窗口。这只是当应用程序在登录时启动,如果应用程序关闭,并且用户通过点击Dock(或其他任何)打开它,我想它显示窗口。

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.

好吧,我找到了怎么办...这个打开雷达错误报告帮助了,我使用了错误的属性。

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

这里代码:

- (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);
}
}

解决方案是创建一个关键字为kLSSharedFileListLoginItemHidden的字典和值 true ,并将其传递给LSSharedFileListInsertItemURL函数。

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