I have an app, called Voice in a Can, which lets you use Alexa on your Apple Watch and iPhone. I’m working on bringing it to the Mac, and one of the things I want is that it be started at login, if the user wants this.

To do this in a sandboxed app, you need to create a helper app, and bundle it inside your main app, in a specific location (/Contents/Library/LoginItems). This helper app is automatically launched at startup, and has no UI - all it does is launch the main app, which in my case sits as an icon in the system toolbar.

There is a great blog post on how to do this by Artur Shamsutdinov, which this post is based on. This blog post adds some detail, information on how to use MSBuild, and trouble-shooting information. You really should check out Artur’s post too.

I created a main application, in my case it is called VoiceInACan.AppleMac:

I made sure this was signed, and configured to use the SandBox.

In my AppDelegate I called SMLoginItemSetEnabled to tell MacOS to launch my helper app at startup (the com.atadore.VoiceInACanForMacLoginHelper is the bundle ID of my helper app, defined below) :

    [DllImport("/System/Library/Frameworks/ServiceManagement.framework/ServiceManagement")]
    static extern bool SMLoginItemSetEnabled(IntPtr aId, bool aEnabled);

    public static bool StartAtLogin(bool value) {
      CoreFoundation.CFString id = new CoreFoundation.CFString("com.atadore.VoiceInACanForMacLoginHelper");
      return SMLoginItemSetEnabled(id.Handle, value);
    }

    public override void DidFinishLaunching(NSNotification notification) {
      ...
      var worked = StartAtLogin(true);
      ...

In a real app you’ll not want to auto-launch a Sandboxed app without permission from the user since your app will be rejected by App Review when you submit it.

I created a helper Mac app, as another project, in my case called VoiceInACan.AppleMacLoginHelper

I made sure this was signed, and configured to use the SandBox

I edited the storyboard to uncheck Is Initial Controller (in the properties on the right) to ensure the helper app has no UI:

I updated Info.plist to indicate the app was background only (because it will have no UI and serve purely to launch my main app on startup):

I added a dependency from my main app to the helper app by right-clicking on References in my main app, selecting Edit References, going to the Projects tab and checking the checkbox next to my helper app: This ensures that the helper app is built before my main app.

In my AppDelegate.cs in my helper app, I launch my main app:

using System.Linq;
using AppKit;
using Foundation;

namespace AppleMacLoginHelper {
  [Register("AppDelegate")]
  public class AppDelegate : NSApplicationDelegate {
    public AppDelegate() {
    }

    public override void DidFinishLaunching(NSNotification notification) {
      System.Console.WriteLine("ViacHelper: starting");
      if (!NSWorkspace.SharedWorkspace.RunningApplications.Any(a => a.BundleIdentifier == "com.atadore.VoiceInACanForMac")) {
        System.Console.WriteLine("ViacHelper: Got bundle");
        var path = new NSString(NSBundle.MainBundle.BundlePath)
            .DeleteLastPathComponent()
            .DeleteLastPathComponent()
            .DeleteLastPathComponent()
            .DeleteLastPathComponent();
        var pathToExecutable = path + @"Contents/MacOS/VoiceInACan";
        System.Console.WriteLine("ViacHelper: Got path: " + pathToExecutable);

        if (NSWorkspace.SharedWorkspace.LaunchApplication(pathToExecutable)) {
          System.Console.WriteLine("ViacHelper: Launched: " + pathToExecutable);
        } else {
          NSWorkspace.SharedWorkspace.LaunchApplication(path);
          System.Console.WriteLine("ViacHelper: Launched: " + path);
        }
      }

      System.Console.WriteLine("ViacHelper: dying");
      NSApplication.SharedApplication.Terminate(this);
    }

    public override void WillTerminate(NSNotification notification) {
      // Insert code here to tear down your application
    }
  }
}

I updated my main app to embed the helper app within it

So far I’ve created two apps: the main app, which provides my main functionality (in my case Alexa), and a helper app which has no functionality other than to launch the main app. In order for the SMLoginItemSetEnabled to work the helper app needs to be embeded within the main app.

To do this, I edited the csproj of my main app, and added markup to embed the main app. Here are the bits, the complete thing is below:

First, define an ItemGroup that references all the files in the helper app’s bundle (the Configuration refers to Debug or Release):

  <ItemGroup>
    <HelperApp Include="$(ProjectDir)/../VoiceInACan.AppleMacLoginHelper/bin/$(Configuration)/AppleMacLoginHelper.app/**" />
  </ItemGroup>

Next, copy those files into the right place in the main app (note that this is done after _CopyContentToBundle so that it is copied before the build signs the final bundle):

  <Target Name="CopyHelper" AfterTargets="_CopyContentToBundle">
    <Message Text="Copying helper app" />
    <MakeDir Directories="$(AppBundleDir)/Contents/Library" />
    <MakeDir Directories="$(AppBundleDir)/Contents/Library/LoginItems" />
    <Copy SourceFiles="@(HelperApp)" DestinationFiles="@(HelperApp->'$(AppBundleDir)/Contents/Library/LoginItems/AppleMacLoginHelper.app/%(RecursiveDir)%(Filename)%(Extension)')" />
  </Target>

Finally, the embeded bundle’s files can be signed (this may not be necessary … first try without this):

  <Target Name="CodeSignHelper" AfterTargets="CopyHelper">
    <Message Text="Signing helper app" />
    <Codesign SessionId="$(BuildSessionId)" ToolExe="$(CodesignExe)" ToolPath="$(CodesignPath)" CodesignAllocate="$(_CodesignAllocate)" Keychain="$(CodesignKeychain)" Resources="$(AppBundleDir)/Contents/Library/LoginItems/AppleMacLoginHelper.app" SigningKey="$(_CodeSigningKey)" ExtraArgs="$(CodesignExtraArgs)">
    </Codesign>
  </Target>

This is my complete modification to my csproj (after the import of the Xamarin.Forms.targets):

  <Import Project="..\packages\Xamarin.Forms.3.3.0.912540\build\Xamarin.Forms.targets" Condition="Exists('..\packages\Xamarin.Forms.3.3.0.912540\build\Xamarin.Forms.targets')" />
  <ItemGroup>
    <HelperApp Include="$(ProjectDir)/../VoiceInACan.AppleMacLoginHelper/bin/$(Configuration)/AppleMacLoginHelper.app/**" />
  </ItemGroup>
  <Target Name="CopyHelper" AfterTargets="_CopyContentToBundle">
    <Message Text="Copying helper app" />
    <MakeDir Directories="$(AppBundleDir)/Contents/Library" />
    <MakeDir Directories="$(AppBundleDir)/Contents/Library/LoginItems" />
    <Copy SourceFiles="@(HelperApp)" DestinationFiles="@(HelperApp->'$(AppBundleDir)/Contents/Library/LoginItems/AppleMacLoginHelper.app/%(RecursiveDir)%(Filename)%(Extension)')" />
  </Target>
   <Target Name="CodeSignHelper" AfterTargets="CopyHelper">
    <Message Text="Signing helper app" />
    <Codesign SessionId="$(BuildSessionId)" ToolExe="$(CodesignExe)" ToolPath="$(CodesignPath)" CodesignAllocate="$(_CodesignAllocate)" Keychain="$(CodesignKeychain)" Resources="$(AppBundleDir)/Contents/Library/LoginItems/AppleMacLoginHelper.app" SigningKey="$(_CodeSigningKey)" ExtraArgs="$(CodesignExtraArgs)">
    </Codesign>
  </Target>

</Project>

Finally copy your main app’s bundle to the Application folder, and run it so that it registers the embedded helper to start on login.

Troubleshooting SMLoginItemSetEnabled

The first challenge is getting log information. If you run the Console app, it only shows you information from after it was launched, which is after you login. You can get historical information, from the terminal

sudo log collect --last 1d
open system_logs.logarchive

This will show you the last day’s worth of logs. You’ll want to look for messages from otherbsd

The second challenge I faced was that although I registered the startup item properly, it wasn’t being launched properly. I was getting this cryptic error Could not submit LoginItem job com.atadore.VoiceInACanForMacLoginHelper: 119: Service is disabled:

After Googling, I discovered the lsregister command, and was able to see many many “registrations” of my helper app, from developing and backups etc

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -dump | grep AppleMacLoginHelper.app | more

What fixed it for me, and your millage may vary, and you should really check what these commands do before executing them, was:

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -gc
/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill

I then re-ran my main app, which re-registered my helper app as a single entry in lsregister and joy, my app launches at startup. I started working on this yesterday at 7:30 am and got it working around 1:30 pm. I’m hoping if you need to do something similar this post will shave a little time off your experience!

Acknowledgements

There is no way I’d have got this working without Artur Shamsutdinov’s blog post from 2016.