Add app at login items - MAS

How can I add/remove my app to the login items so my apps auto launch when the systems boot.
I know a way to this with Applescript but it needs to work when sandboxed and for the MAS.

MBSplugins or declares are welcome. :slight_smile:

Lets clarify… You want the app to launch on Login or when the machine boots? Which is it?

I think you can add login items using LSSharedFileListMBS class.

http://monkeybreadsoftware.net/class-lssharedfilelistmbs.shtml

e.g. like this:

[code]// Add iPhoto to launch items

// pick app
dim app as FolderItem = SpecialFolder.Applications.Child(“iPhoto.app”)

// get list object
dim l as new LSSharedFileListMBS(LSSharedFileListMBS.kSessionLoginItems)

// insert file
dim item as LSSharedFileListItemMBS = l.InsertFile(l.kLSSharedFileListItemBeforeFirst, “Launch iPhoto”, nil, app)

// check error
if l.Lasterror = 0 then
MsgBox “OK”
else
MsgBox "Failed: "+str(l.Lasterror)
end if[/code]

[quote=333641:@Christoph De Vocht]How can I add/remove my app to the login items so my apps auto launch when the systems boot.
I know a way to this with Applescript but it needs to work when sandboxed and for the MAS.

MBSplugins or declares are welcome. :)[/quote]

Remember that thread where Valdemar de Susa posted the how to of his helper for you ?
https://forum.xojo.com/14457-launchagents-howto/1

I have used his helper for several years for MAS apps.

LSSharedFileList seems to be deprecated.
I don’t want to use launchdaemon or launchagent because they are processed very late in the system boot sequence. The login items is what I need.

Do I understand it correctly you need a helper app for this? Why not just launch the main app? This is how I do it now. Is this really needed for the MAS?

Oh, than please ask Apple for what is replacement in 10.12.

Yes, I believe that you need a helper app for sandboxed MAS applications.
Your application isn’t allowed to change user preferences (login items) in a sandboxed environment.

Tried for two hours with the Valdemar method but I keep getting the same errors as Oliver O gets.
https://forum.xojo.com/14457-launchagents-howto/1

@Oliver O: did you resolve this issue?
Anyone else found a working version that works with 10.12 ?

I just tested my own Fonts Manager which has worked flawlessly until now. It appears the launch at startup feature based on VDSC no longer works. No crash, but the helper does not seem to start.

For the time being, I am considering instructing the user how to right click the dock icon and select the option “Open at login”.

I’ve been using the code from Valdemar for a while. For most users it worked for some it didn’t. Had to change to use a file in the LoginItems folder. This works reliably but the code is a bit more complicated. My app isn’t sandboxed.

I’ve just confirmed that Backup To Go works on Sierra; however I haven’t updated it for a few years and it’s no longer available (due to lack of interest).

Feel free to give it a try: download the helper from the link below.
http://www.ohanaware.com/xojo/LaunchHelper.zip

It needs to be placed in “.app/Contents/Library/loginItems/” (as pictured below).

The plist of the helper needs to be modified in two places:

  1. It needs to have a UNIQUE bundle identifier, this can be your main application identifier with “.launchHelper” on the end.
  2. “mainAppBundleID” needs to be modified to include the bundle ID of your application.

Xojo code:
Now this code won’t be able to be dropped directly into your application because of it’s dependancies, however the main declare still works “SMLoginItemSetEnabled”. It’s worth noting that I couldn’t find a way to tell if it was active or not, hence why this code stores it in the preferences. Also what seems important is that the application must be in the Applications folder.

When testing on my Sierra machine, all of the Apple things appear first, then the apps that were open before and finally a couple of a seconds later Backup To Go launches.

[code]#if TargetMacOS then
declare function SMLoginItemSetEnabled lib “ServiceManagement” ( identifier as CFStringRef, value as boolean ) as boolean

Dim newValue as boolean = not xPlatPreferences.lookup( kLaunchonloginIdentifier, false )

Dim valueWasSet as boolean
if debugBuild or left( app.executableFile.nativePath, 14 ) = “/Applications/” then
valueWasSet = SmLoginItemSetEnabled( “com.ohanaware.backupToGoHelper”, newValue )
end if

if valueWasSet = false then
// - It failed.
Dim errorMessage as string
if not debugBuild and left( app.executableFile.nativePath, 14 ) <> “/Applications/” then
errorMessage = appkit2015.localizedAppName + " needs to be in the Applications folder in order to launch on login."

elseif newValue = true then
  errorMessage = kWasUnableToRegisterTheLoginItem
  
else
  errorMessage = kCouldntUnregisterTheLoginItem
end if

Dim md as new messageDialog
md.message = kFailedToAlterTheLaunchOnLoginStatus
md.explanation = errorMessage

call md.showModalWithin( self )

newValue = not newValue

else
xPLatPreferences.value( kLaunchonloginIdentifier ) = newValue

end if

if xPlatPReferences.lookup( kLaunchonloginIdentifier, false ) then
me.setIcon 0, retinaKit.imageNamed( retinaKit.imageName.statusAvailable )
else
me.setIcon 0, retinaKit.imageNamed( retinaKit.imageName.statusNone )
end if

#endif[/code]

Hope that this helps.

Thank you Sam ! Will try this later today. Kudos

Please also check ServiceManagementModuleMBS here:

http://monkeybreadsoftware.net/module-servicemanagementmodulembs.shtml

RegisterHelperApp may also help as well as LoginItems functions.

One last thing is to disable “Inheritance” in the capabilities pane of App Wrapper, the Launch Helper won’t launch otherwise.

Another tip: Make sure your app is only present ones !! If the user has two (or more) versions of your app installed in different places, it will not work. It took me a while before finding out why it sometimes didn’t work. The created debug versions by Xojo did trigger this issue.

BTW the reason is that the login items are linked with the apps Bundle ID and it apparently confuses things.

So, I am resurrecting this thread for a problem I have encountered that I think is related.

I have given this a try, both with the helper app provided by Sam Rowlands as well as one I built on my own and it appears to work as I would expect.

However, I am unable to get App Wrapper to wrap the built applications correctly. When I try to wrap the application, App Wrapper gives me an error message indicating there are “Critical issues that need fixing”. Those issues are:

Gate Keeper 1 Invalid Resource Directory (directory Or Signature Have Been Modified)
Code sign diagnosis (followed by a whole list of “validated” and “prepared” statements that look like they have something to do with a bunch of MBS plugins (all of which are used in my application).

These errors did not occur until I added the helper app to my application’s bundle with a CopyFilesTo build step. The helper app is located in .app/Library/LoginItems as I think it is supposed to.

Any ideas?

[quote=419419:@Scott Crick]Gate Keeper 1 Invalid Resource Directory (directory Or Signature Have Been Modified)
Code sign diagnosis (followed by a whole list of “validated” and “prepared” statements that look like they have something to do with a bunch of MBS plugins (all of which are used in my application).[/quote]
First thing to to try is to restart your Mac. I’m not kidding either, some people have terrible trouble code signing, and it’s resolved when their computer is restarted (my guess is the Code Signing libraries cache some kind of information and it gets corrupted or whatever).

Second thing to try is to send me the log (via e-mail support) and I’ll take a look at it and hopefully the code signing API returned an error that’s useful (it doesn’t always).

This is occurring BEFORE the application gets sent to App Wrapper for code signing?

Yes, the computer has been restarted since my last post and the same errors are happening.

Done. I thank you for any assistance you could provide.

Correct. The helper application is being added via a CopyFilesTo build step that occurs before the App Wrapper build script executes.