macos 在 Mac OS X 上启动时以编程方式运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3358410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 07:05:12  来源:igfitidea点击:

Programmatically run at startup on Mac OS X?

macosautorun

提问by Jake Petroules

How do I programmatically set an application bundle on Mac OS X to run when the user logs in?

如何以编程方式将 Mac OS X 上的应用程序包设置为在用户登录时运行?

Basically, the equivalent of the HKCU\Software\Microsoft\Windows\CurrentVersion\Runregistry key in Windows.

基本上,相当于HKCU\Software\Microsoft\Windows\CurrentVersion\RunWindows中的注册表项。

采纳答案by Barry Wark

You can add the application to the user's "Login Items" (under System Preferences=>Accounts=[user]) or you can add a launchdagent to the user's ~/Library/LaunchAgentsfolder (see man launchd.plist). Use ~/Library/LaunchDaemons/if your app has no user-facing UI. As others point out, launchd gives you a lot of control over when the app starts, what happens if the app quits or crashes, etc. and is most appropriate for "daemon" style apps (with our without UI).

您可以将应用程序添加到用户的“登录项”(在 System Preferences=>Accounts=[user] 下),或者您可以将launchd代理添加到用户的~/Library/LaunchAgents文件夹(请参阅参考资料man launchd.plist)。使用~/Library/LaunchDaemons/,如果你的应用程序有没有面向用户的UI。正如其他人指出的那样,launchd 可以让您对应用程序何时启动、应用程序退出或崩溃时发生的情况等进行大量控制,并且最适合“守护进程”风格的应用程序(我们没有 UI)。

The first option (Login Items) can be manipulated programmatically(link from Gordon).

第一个选项(登录项)可以通过编程方式进行操作(来自Gordon 的链接)。

回答by Dmitriy

A working example below.

下面是一个工作示例。

Create a file

创建文件

~/Library/LaunchAgents/my.everydaytasks.plist

~/Library/LaunchAgents/my.everydaytasks.plist

With contents:

含内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>my.everydaytasks</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Applications/EverydayTasks.app/Contents/MacOS/EverydayTasks</string>
    </array>
    <key>ProcessType</key>
    <string>Interactive</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <false/>
</dict>
</plist>

See the original post that helped me to make this example:

请参阅帮助我制作此示例的原始帖子:

https://superuser.com/a/229792/43997

https://superuser.com/a/229792/43997

To test you need to run this in terminal

要测试,您需要在终端中运行它

launchctl load -w ~/Library/LaunchAgents/my.everydaytasks.plist

To unload

卸载

launchctl unload -w ~/Library/LaunchAgents/my.everydaytasks.plist

See also

也可以看看

https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html

https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html

The is the other way of adding your application to starup using "Login Items". See this example project on how to implement it:

这是使用“登录项”将应用程序添加到启动的另一种方式。有关如何实现它,请参阅此示例项目:

https://github.com/justin/Shared-File-List-Example

https://github.com/justin/Shared-File-List-Example

回答by Jeremy

The "correct" method is to create a LaunchAgent for processes you want to start at login that may have a UI and a LaunchDaemon for those that should be pure background processes. In your installer drop your plist into the correct folder, either for the user, or all users, or the system. The reason this method is superior is because you can use launchd to control how your process is run including the built-in ability to make sure it keeps running even if it crashes or is killed by the user.

“正确”的方法是为您想在登录时启动的进程创建一个 LaunchAgent,这些进程可能有一个 UI,并为那些应该是纯后台进程的进程创建一个 LaunchDaemon。在您的安装程序中,将您的 plist 放入正确的文件夹中,对于用户、所有用户或系统。这种方法之所以优越,是因为您可以使用 launchd 来控制您的进程的运行方式,包括确保它在崩溃或被用户杀死时继续运行的内置功能。

回答by jocull

Wanted to throw this out here for anyone using Qt / C++. Qt makes it super easy to use plists through the QSettings class. Check out this code snippet from a sample dummy application.

想把这个扔给任何使用 Qt/C++ 的人。 Qt 使通过 QSettings 类使用 plist 变得非常容易。从示例虚拟应用程序中查看此代码片段。

void MainWindow::readPlist()
{
    QSettings settings(appPlistPath, QSettings::NativeFormat);
    QVariant value = settings.value("mykey");
    QMessageBox::information(this, "Your Value", value.toString());
}

void MainWindow::addPlistEntry()
{
    QSettings settings(appPlistPath, QSettings::NativeFormat);
    settings.setValue("mykey", "myvalue");
}

void MainWindow::removePlistEntry()
{
    QSettings settings(appPlistPath, QSettings::NativeFormat);
    settings.remove("mykey");
}