How to add loginItem on macOS Mojave?
Solution I have found:
macOS Swift: How to properly add application as Login Item
but function called LSSharedFileListInsertItemURL is deprecated since macOS 10.11 (but still works on Mojave).
While using it, XCode gives note:
'kLSSharedFileListSessionLoginItems' is deprecated: first deprecated in macOS 10.11 - Use a LaunchAgent, XPCService or the ServiceManagement APIs instead.
Following that note, I have taken a look into ServiceManagement. And the file SMLoginItem.h defines SMLoginItemSetEnabled and documentation https://developer.apple.com/documentation/servicemanagement/1501557-smloginitemsetenabled says
Enable a helper application located in the main application bundle’s Contents/Library/LoginItems directory.
which does not apply to my case because I want to maintain LoginItems of applications which aren't located in my application bundle, for example I want to add /Applications/Books.app as LoginItem.
CFStringRef thePath = (__bridge CFStringRef)@"/Applications/Books.app";
return SMLoginItemSetEnabled(thePath, YES);
Above sniped of code returns
Could not locate login item /Applications/Books.app in the caller's bundle
Could not enable login item: /Applications/Books.app: 107: Malformed bundle
Secondly, how to remove LoginItem from system programmably?
I am not creating app for AppleStore so it isn't sandboxed (I have read kLSSharedFileListSessionLoginItems is deprecated for sandboxed apps)
I have apps both in the App Store and outside. I’m using each of the methods for adding login items depending on the distribution way. There is no other way to manage this at the moment.
SMLoginItemSetEnabled only applies to agents inside your own app bundle. For apps outside of App Store you can use LSSharedFileListInsertItemURL. It’s deprecated but there’s no adequate replacement and it’s still available in macOS 10.15 Beta SDK. Look out for further API changes next year.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With