15

I have a very strange issue with a sandboxed Mac app I'm developing. One requirement is that the user should have the possibility to launch the app when the system starts. For this, I'm using SMLoginItemSetEnabled() as described on http://blog.timschroeder.net/2012/07/03/the-launch-at-login-sandbox-project/.

When the user starts the app for the first time and enables this option, I can see an entry is being added to launchctl by using launchctl list. When I reboot the system, the app is not being started. More strange is the fact that the entry found using launchctl list has disappeared. However, a similar entry is still available in /private/var/db/launchd.db/com.apple.launchd.peruser.501/overrides.plist with key Disabled being false.

When I start the app manually and again set the option to start automatically, the entry is again available in launchctl list. When I reboot the system the app is being launched automatically. Concluding, for some reason SMLoginItemSetEnabled() only works the second time I run the app. Therefor it looks similar to this issue: https://stackoverflow.com/questions/16354295/sandbox-app-with-loginitems-only-work-after-second-app-launch. However, no solution is provided.

https://stackoverflow.com/questions/16354295/sandbox-app-with-loginitems-only-work-after-second-app-launch

Community
  • 1
  • 1
Niels Mouthaan
  • 1,010
  • 8
  • 19

1 Answers1

8

If you're like me, you probably had extra copies (generated by Xcode, etc) laying around that seem to confuse LaunchServices.

I wrote a post about it here: Login Items in macOS 10.11 and newer

But the short version is, use lsregister -dump to find all copies that LaunchServices knows about, remove them, then use lsregister -kill to reset the LaunchServices database when you're done.

Devin
  • 183
  • 2
  • 4
  • 2
    Should specify the full path for it to work: /System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister – strangetimes Jun 04 '18 at 23:06
  • related: https://stackoverflow.com/questions/27995970/smloginitemsetenabled-sometimes-silently-fails-to-launch-sandboxed-ui-helper – pkamb Nov 01 '18 at 22:31