Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a helper application for Mac App to start it on user login?

In fact, I read the following document:

Applications can contain a helper application as a full application bundle, stored inside the main application bundle in the Contents/Library/LoginItems directory. Set either the LSUIElement or LSBackgroundOnly key in the Info.plist file of the helper application’s bundle.

I don't quite understand it, anyone knows how to do?

and also, what does this mean:

Note: Before calling the SMLoginItemSetEnabled function, first register with Launch Services by calling the LSRegisterURL function with the URL for the helper application bundle.

is there any example about how to use LSRegisterURL and SMLoginItemSetEnabled?

like image 556
disorderdev Avatar asked Sep 15 '11 19:09

disorderdev


People also ask

How do I get programs to start automatically on my Mac?

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.

What is a helper application Mac?

The apps on the Mac App Store are sandboxed, which means they cannot read certain system sensors. The Helper app can fetch more system information and exchange it with the Usage app.

How do I install an application on a single user Mac?

Hold down the Command key and drag the app from the main Applications folder into your personal one. (You must hold down Command or macOS will create an alias instead of moving the app.) You may be prompted to enter an administrative password, or if you have a Mac with Touch ID, to validate the move with a fingertip.


2 Answers

+ (void)startHelper {
    NSURL *helperURL = [[[NSBundle mainBundle] bundleURL] URLByAppendingPathComponent:@"Contents/Library/LoginItems/YourHelper.app" isDirectory:YES];
    OSStatus status = LSRegisterURL((CFURLRef)helperURL, YES);
    if (status != noErr) {
        NSLog(@"Failed to LSRegisterURL '%@': %jd", helperURL, (intmax_t)status);
    }


    Boolean success = SMLoginItemSetEnabled(CFSTR("com.yourcompany.helper-CFBundleIdentifier-here"), YES);
    if (!success) {
        NSLog(@"Failed to start Helper");
    }
}

Note that the Helper must be packaged with the main app in the "Contents/Library/LoginItems" directory. You will need to create it during the build and copy the helper there.

like image 179
roustem Avatar answered Oct 05 '22 11:10

roustem


I found a handy link:

http://www.delitestudio.com/2011/10/25/start-dockless-apps-at-login-with-app-sandbox-enabled/

EDIT: sadly this link no longer works. Perhaps someone could suggest a better alternative...

It's a good tutorial for doing registering a Login Item in a Sandboxed environment if that's of use (and we'll all need to eventually!). The important thing is that, annoyingly, you have to copy your built, main app to the Applications folder and don't do what I do which is forget to sandbox the helper-app and add a Application is agent (UIElement) row to the helper's plist with TRUE for the value. NSLog and Console is your old-fashioned debugging friend now.

...just have to figure out how to get the helper app to launch the main app when they're both sandboxed..... Edit: Found this question: Cocoa: Sandbox entitlement to launch another application

like image 43
Todd Avatar answered Oct 05 '22 11:10

Todd