Add the OSX "Login Elements" application during the installation of the Package Maker postflight script installer

I need to add the application to the input elements from a postflight script that runs as part of my installer. It should work at 10.5+. Preferably, it will work in a bash script. My application already requires administrative rights.

The approach I found here: Mac OS Login Items with arguments? seemed to be on the right track (see below) ... but didn't work when I tried this on the command line and I'm not sure how to install it for all users, or if I need to add logic to check, added whether it is in startup items before calling this code.

#!/bin/bash /usr/bin/osascript -e "tell application \"System Events\" to make new login item with properties { path: \"$1\", hidden:false } at end" 

I suspect that I can do something with the launch. But I'm not sure which approach is best practice for inter-version compatibility.

NOTE. I DO NOT want to add it using objective-c code inside my application. I need the installer to add it. I am currently starting the application after installation, which then adds it to the code entry elements using LSSharedFileListRef ... An example of this approach can be found here: How to make my application open at login? . The reason is that this is not normal, I need to install the application from Apple Remote Desktop via the command line when on the login screen. Therefore, the application should not start automatically after installation.

+8
bash macos packagemaker
source share
2 answers

Here are the options that I explored and experimented with:

Option 1: Use Login Elements

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

 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 they are read.

I tested this on 10.5, 10.6 and 10.7
@noa says this does not work for a mountain lion (10.8), has not been personally confirmed.

Option 2: LaunchAgent

The only consequences of using the launch agent are:

  • Your application does not appear in the Login Elements list, so the user really needs to know what they are doing to get rid of it.
  • The user cannot complete the application process without starting: launchctllload / Library / LaunchAgents / com.your.package.plist

Here is the 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. Compiling Obj-c Code to Binary

I have never finished this approach. This seems to be the approach Novell is taking. Essentially, you will create a basic application that calls the libraries referenced by this solution: How do I open the application when I log in?

Other

Did not try this, but according to this post, if you want it to work with a tiger, you need to use AppleScript ..? I cannot confirm or refute this, but thought that this link might be relevant. Editing Mac OS X login items in Objective-C via AppleScript

+14
source share

There are two ways to launch your program during login:

  • Use Login Elements
  • Configure LaunchAgent

LaunchAgent is simple, you need a .plist file that tells launchd to load your program , and then put that file in / Library / LaunchAgents (as part of the installation package).

Login elements are a bit of a pain and this is for every user.

+2
source share

All Articles