Add "login item" to existing app

Greetings,


I'm aware, from doing "Show Package Contents" in the Finder, that some Mac OS apps have "login items", as evidenced by the existence of the Contents/Library/LoginItems folder within the app bundle. I have obsereved in some instances that items within the "LoginItems" folder of an app later appear as "agents" (or "sub-processes") within the process tree as shown by "ps" in the Terminal, after the main app is launched.


Can I assume in this context, generally, that "LoginItems" means "items that are (or can be) started when the app launches"?


My real question is: Is it possible for an end user (albeit one with Administrator capability), to add a new "login item" to an existing application simply by adding a program (or bundle or script) to the Contents/Library/LoginItems folder, and perhaps by editing a plist file somewhere in the bundle, to "register" the new LoginItem with the app? So that, the new program runs automatically whenever the app is launched?


Thanks,

Barry

Replies

In short, no. And that is not how login items are launched.


Login items inside the bundle of an app are Service Management login items. When the owning app enables them, these login items are run at login for the current user, every time that user logs in. No administrator is required.


You will not be able to hack a login item into an existing app. Although you can add login items into a user's session through other means. Additionally, if you want to run an agent in response to some other task being run, you can do that with a launchd agent.

john daniel has covered the login item side of this, but I wanted to address the bigger picture. You wrote:

So that, the new program runs automatically whenever the app is launched?

That’s a fairly unusual requirement. Why do you want to do that?

Regardless, it should be possible to achieve this goal. There’s three main steps:

  1. Create a login item (or launchd agent) that runs for the duration of the user’s login session. Configure it to show no UI (see

    LSBackgroundOnly
    ), so that it’s not visible to the user.
  2. Use

    NSWorkspace
    to monitor the target app being launched and terminated.
  3. When the target app is launched, use

    NSWorkspace
    to launch the companion app. When the target app terminates, use
    NSWorkspace
    to terminate the companion app.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"