1

I'm new to OS X development, I have a status bar app that suppose to stay on the status bar until user explicitly close it.

problem now is that if the user log out from the system and then login, the app closes.

so my question is how to make the app stays open, or how to automatically re open it when the user login to the system?

  • this app is currently for my own use (not for the app store) if it make it easier..
Mario
  • 2,431
  • 6
  • 27
  • 34

3 Answers3

1

I had this same question today, and found a different (in my opinion, simpler) way to achieve this. Basically, you can create a launchd job which runs your app when the user logs in.

You'll need to define a job in the form of a .plist file (it's XML), which looks like:

<?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.capablemonkey.sleepApp</string>
    <key>ProgramArguments</key>
      <array>
          <string>/usr/bin/open</string>
          <string>-W</string>
          <string>/Applications/sleep.app</string>
      </array>
    <key>RunAtLoad</key>
    <true/>
  </dict>
</plist>

From there, assuming you can make your Cocoa app run some command line commands, you'll want to copy your file to ~/Library/LaunchAgents/:

cp com.capablemonkey.sleepApp.plist ~/Library/LaunchAgents/

and then tell launchd to run your new job:

launchctl load ~/Library/LaunchAgents/com.capablemonkey.sleepApp.plist

See my blog post for a deeper dive and examples in node.js. It describes ways to also undo this should the user decide to not have your app run on login, and to check to see if the launchd job is currently active.

Community
  • 1
  • 1
Gordon Zheng
  • 499
  • 3
  • 10
0

One option you can consider is using a script to launch your application on login (for example, Running script upon login mac).

One your develop a functional script, you can modify for application to install the script, perhaps at the first time the application is launched, or as part of an installer.

Community
  • 1
  • 1
kc9ddi
  • 141
  • 1
  • 7
0

Their shouldn't be a need to run a script to auto launch you app. OSX has that built in.

A user can have "Login Items" You can view these in System Preferences>Users and Groups>Login Items

With that said, you just need to find out how your app can add itself on this list so that when a user installs your app, it places itself there.

You would have to have your app run a script upon installing to do this.

I am a new user so I cannot post pictures but I found this...

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

Of course the other thing is, if this app is just for your own use and not meant for anyone else, just add it to your login items yourself and voila! Problem solved.

hope this helps!

Community
  • 1
  • 1
orah
  • 26
  • 3