Login Items in macOS 10.11 and newer

If you've ever written or tried to write a macOS application that needs to launch itself at login, you've probably spent some time fighting with SMLoginItemSetEnabled.

There are a couple of good tutorials online that explain how to setup a help application and embed it in your main application to launch it.

However, after doing everything right, you may be banging your head against the wall because your application won't launch when you login.

That's what happened to me, anyway.

In my case (and maybe yours), the problem turned out to be copies of my app scattered around my hard drive that were confusing LaunchServices.  Once I removed all but one copy, it worked as expected.

The tricky part is, Xcode puts copies of your app in all sorts of temporary folders, and maybe you've dragged them to the trash.  LaunchServices can get confused by any one of these.

After some mucking around, I found a way to locate all the copies that LaunchServices knows about, and to reset its database once you've cleaned them out.

You use a utility called lsregister, it's probably not in your path, so you'll need the full path to run it.

Find all copies of your app that LaunchServices knows about:

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -dump|grep .*path.*<YourAppName.

After deleting the extra copies, reset the LaunchServices database:

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user

Also, now that Apple has deprecated several LaunchServices APIs, it's not easy to determine if your app is currently set to start on launch.  It seems most apps just don't check anymore, which is probably fine, but I suspect you could call lsregister -dump and parse the output to find out if your app is set to launch or not.