Add app to OSX “Login Items” during a Package Maker installer postflight script

后端 未结 2 829
-上瘾入骨i
-上瘾入骨i 2020-12-03 06:10

I need a way to add an application to the Login Items from a postflight script which is run as part of my installer. It needs to work on 10.5+. Preferably, it would work i

相关标签:
2条回答
  • 2020-12-03 06:24

    Here's the options I investigated and experimented with:

    Option 1: Use Login Items

    This is the method I used. It's very easy to do from a bash file by adding the following line to your postflight.

    defaults write /Library/Preferences/loginwindow AutoLaunchedApplicationDictionary -array-add '{Path="/Applications/Your Application.app";}'
    

    Note: You don't even have to worry about adding duplicates if you reinstall the application. The loginwindow process removes duplicates when it reads them.

    I tested this on 10.5, 10.6, and 10.7
    @noa says this doesn't work on mountain lion (10.8), Haven't personally confirmed.

    Option 2: LaunchAgent

    The unique ramifications of using a Launch Agent are:

    1. Your application doesn't appear in the Login Items list, so the user really has to know what they're doing to get rid of it
    2. The user cannot end your applications process without running: launchctl unload /Library/LaunchAgents/com.your.package.plist

    Here's some code you could use to create the launch agent in your bash file:

    cat > /Library/LaunchAgents/com.your.application.agent.plist << EOT
    <?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>com.your.application.agent</string>
        <key>ProgramArguments</key>
        <array>
            <string>/Applications/Your Application.app/Contents/MacOS/Your Application</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
    </dict>
    </plist>
    EOT
    

    Option 3: Compile Obj-c code to a binary

    I never actually finished this approach. Apparently, this is the approach that Novell takes. Essentially you'd make a foundation application that calls the libraries referenced from this solution: How do you make your App open at login?

    Other

    Didn't try this but according to this post if you want it to work on tiger you need to use AppleScript..? I can't confirm or deny that but thought this link might be relevant. Editing Mac OS X login items in Objective-C through AppleScript

    0 讨论(0)
  • 2020-12-03 06:46

    There are two ways you can start your program at login time:

    1. Use login items
    2. Set up a LaunchAgent

    The LaunchAgent is simplest, all you need is a .plist file that tells launchd to load your program, and then place that file in /Library/LaunchAgents (as part of the install package).

    Login items are a bit of a pain and it's per-user.

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