Programmatically run at startup on Mac OS X?

前端 未结 4 1829
悲哀的现实
悲哀的现实 2020-12-07 21:09

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

Basically, the equivalent of the HKCU\\Software\\Microsoft\\Windo

相关标签:
4条回答
  • 2020-12-07 21:52

    A working example below.

    Create a file

    ~/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

    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

    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

    0 讨论(0)
  • 2020-12-07 21:55

    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.

    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");
    }
    
    0 讨论(0)
  • 2020-12-07 22:03

    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.

    0 讨论(0)
  • 2020-12-07 22:05

    You can add the application to the user's "Login Items" (under System Preferences=>Accounts=[user]) or you can add a launchd agent to the user's ~/Library/LaunchAgents folder (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).

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

    0 讨论(0)
提交回复
热议问题