Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set MacOS X menubar app to launch at startup

I have a sandboxed menubar application (no dock icon) that in it's preferences window allows the user to check a checkbox to have the app launch at login. I used to use the LSSharedFileList api, but as this is not allowed anymore for sandboxed apps, I've migrated to using SMLoginItemSetEnabled. What I've found is that although the app will launch at login, as expected, if I go back into the Preferences and uncheck and re-check the launch at login checkbox, I get a second instance of my menubar app launched.

Here's my helper app code (in its app delegate):

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

    NSString * path = [[[[[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent]
                                    stringByDeletingLastPathComponent]
                                            stringByDeletingLastPathComponent]
                                                    stringByDeletingLastPathComponent];


    [[NSWorkspace sharedWorkspace] launchApplication:path];
    [NSApp terminate:nil];

}

Here is the code in my preferences window (main app):

- (IBAction)toggleLoginStatus:(NSButton*)sender{


    if(!SMLoginItemSetEnabled((__bridge CFStringRef)@"myAppBundleIdentifier", (BOOL)[sender state])){
            NSLog(@"Dagnabit!");
    }

}

After the second instance launches, unchecking/re-checking the checkbox does not launch anymore instances. Does anyone have any idea what's going on? Thanks

like image 745
Lazloman Avatar asked Oct 29 '14 16:10

Lazloman


People also ask

How do I make my Mac open apps at startup?

On your Mac, choose Apple menu > System Preferences, then click Users & Groups . Select your user account, then click Login Items at the top of the window. Do any of the following: Add a login item: Click the Add button below the list of items, select a document, folder, app, server, or other item, then click Add.

How do I pin an application to the Start menu on a Mac?

In the Start menuSelect the Start menu. Find the program you want to pin, and right-click it. Select Pin to Start.

How do I stop apps from opening when I log into my Mac?

Go to System Preferences > Users & Groups > Login Items for a list of apps set to launch on startup. To remove a specific app, highlight it and click the minus button under the list. If you prefer, certain apps can be set to launch at startup without necessarily popping up onto the screen.

Why does some apps automatically open when I start my computer Mac?

The app might have installed its own startup software. Often this is controlled by a setting within the app. Check the app's preferences or documentation, or contact the app's developer.


1 Answers

I've found the answer. None of the tutorials I looked at mentioned this, but in the docs for SMLoginItemEnabled says this:

The Boolean enabled state of the helper application. This value is effective only for the currently logged in user. If true, the helper application will be started immediately (and upon subsequent logins) and kept running. If false, the helper application will no longer be kept running.

So I'll have to check if the app is running already before allowing the helper to launch it.

like image 104
Lazloman Avatar answered Oct 25 '22 03:10

Lazloman