28

In my Mac app, I've been using SMCopyAllJobDictionaries() to check whether the app will be launched automatically at login. Basically, I get an array of all the login items and check whether my Bundle ID is in the array.

This function call works until Mavericks, but is deprecated in Yosemite. According to Apple,

This routine is deprecated and will be removed in a future release. There will be no provided replacement.

However, how can I do the same job in Yosemite? I do have to check whether my app is in the list of login items, in order to show a check box properly. I couldn't find relevant documentation from Apple.

zavié
  • 4,301
  • 2
  • 34
  • 46
  • Notice that `LSSharedFileList` no longer works with Sandbox enabled. See stackoverflow.com/a/12629184/284811 too. I need to sandbox my app because it's distributed via the Mac App Store. – zavié Oct 12 '14 at 14:11
  • You don't need a replacement for `SMCopyAllJobDictionaries`, see my answer here http://stackoverflow.com/questions/32546893/smcopyalljobdictionaries-and-smjobcopydictionary-is-deprecated-so-what-are-thei –  Jun 07 '16 at 08:49

3 Answers3

11

After some research it appears that there isn't an easy answer to this, period. After testing multiple apps including F.lux and BetterSnapTool, I've been able to easily desynch their user interfaces from the system preferences. If I enable "launch on start up" in any of these apps, then remove them from the system preferences log in items section, then relaunch them, their interfaces still think they are set to launch on start up. Interacting with their checkboxes does nothing, as the apps try to remove themselves from the list they no longer belong to and a second click is required to do anything.

To me this signifies that they keep their own internal state as a BOOL and save it between launches and that there simply isn't a way to get the list to synch with as of Yosemite. If someone knows otherwise I'll give them the bounty.

Metabble
  • 11,773
  • 1
  • 16
  • 29
  • 1
    Thx for the investigation! This is really weird, seems to me like Apple is removing this feature altogether.. – zavié Jul 26 '15 at 20:28
  • No problem. It does appear that way. Guess we'll just have to see. – Metabble Jul 27 '15 at 01:06
  • Thanks for sharing the findings, had a feeling this is the case. The reasonable solution is to run a background check when app launches and simply sync whatever the set preference is. – Ian Bytchek Mar 29 '16 at 08:12
2

As of WWDC 2017, Apple engineers have stated that this is still the preferred API to use.

However, using this API will cause your build to fail. Don't turn off all deprecated function warnings. Instead, to enable your app to compile, wrap SMCopyAllJobDictionaries with the following:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
  CFArrayRef  cfJobDicts = SMCopyAllJobDictionaries( kSMDomainUserLaunchd );
#pragma clang diagnostic pop

If this issue is important to your app and you'd like Apple to provide a clean solution, please file a radar; this helps Apple engineers determine priorities of work items.

Jeff Szuhay
  • 162
  • 7
1

I'm using LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL) for a similar purpose. See Nick Moore's answer to How do you make your App open at login? for an example.

Note that this works for login items, but might not work for launchd jobs.

Community
  • 1
  • 1
MrMage
  • 7,282
  • 2
  • 41
  • 71
  • 2
    Unfortunately `LSSharedFileList` no longer works with Sandbox enabled. See http://stackoverflow.com/a/12629184/284811 too. I need to sandbox my app because it's distributed via the Mac App Store. – zavié Oct 12 '14 at 10:13