2

I'm using the instructions located here to create an embedded helper application that will open the main application, and register the helper app as a login item.

The helper app is currently started at login, but is failing to open the main application.

The system's Console repeats the following error over and over for about 5 minutes then starts: 500px Uploader Helper: LSOpenFromURLSpec() returned -10827 for application 500px Uploader path (null).

Screenshot from Console.app

The helper app is using it's Application Delegate to launch the main app with the following code:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    dispatch_async(dispatch_get_main_queue(), ^{
        BOOL success = [[NSWorkspace sharedWorkspace] launchApplication:@"500px Uploader"];

        if (success)
        {
            NSLog(@"YEAHHHH");
            exit(EXIT_SUCCESS);
        }
        else
        {
            NSLog(@"NOOOOO");
            exit(EXIT_FAILURE);
        }
    });
}

According to other questions on SO, this is the recommended way to open applications in the sandboxed environment.

I have tried to delay the launch until after the first run loop with no success. A coworker has verified that the issue isn't related to my development environment. I've also tried using the absolute path as the argument to launchApplication:. launchApplication: is also failing to open other applications in /Applications.

Does anyone know of any reason why this might not be working or why it would start to work after a few minutes?

Community
  • 1
  • 1
Ash Furrow
  • 12,391
  • 3
  • 57
  • 92

2 Answers2

2

Not sure why your code isn't working (these sandbox issues are still quite experimental), but it looks like a path issue to me, i.e. that LSOpenFromURLSpec doesn't find your main app. I use the following code to launch my main application from the helper application (and it works):

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Check if main app is already running; if yes, do nothing and terminate helper app
    BOOL alreadyRunning = NO;
    NSArray *running = [[NSWorkspace sharedWorkspace] runningApplications];
    for (NSRunningApplication *app in running) {
        if ([[app bundleIdentifier] isEqualToString:@"com.timschroeder.LaunchAtLoginApp"]) {
            alreadyRunning = YES;
        }
    }

    if (!alreadyRunning) {
        NSString *path = [[NSBundle mainBundle] bundlePath];
        NSArray *p = [path pathComponents];
        NSMutableArray *pathComponents = [NSMutableArray arrayWithArray:p];
        [pathComponents removeLastObject];
        [pathComponents removeLastObject];
        [pathComponents removeLastObject];
        [pathComponents addObject:@"MacOS"];
        [pathComponents addObject:@"LaunchAtLoginApp"];
        NSString *newPath = [NSString pathWithComponents:pathComponents];
        [[NSWorkspace sharedWorkspace] launchApplication:newPath];
    }
    [NSApp terminate:nil];
}

Update: I've seen a similar issue in a sample project uploaded by the author of another question and there the code worked fine if only the main app was placed in the /Applications or ~/Applications folder. Perhaps this is your issue here, too.

Community
  • 1
  • 1
Tim
  • 1,659
  • 1
  • 21
  • 33
  • I'm getting the following error when I do this: `12-07-05 7:23:33.226 PM 500px Uploader Helper: LSOpenFromURLSpec() returned -10827 for application /Applications/500px Uploader.app path (null).` – Ash Furrow Jul 05 '12 at 23:24
  • Then the problem is probably not in your helper app's code but in your main app's build settings. This is quite tricky, but you could have a look at my sequel tutorial to the tutorial you cited: http://blog.timschroeder.net/2012/07/03/the-launch-at-login-sandbox-project/ – Tim Jul 05 '12 at 23:27
  • Another point: Really not sure about this, but the space character in your main app's name could be an issue. Have you tried to replace it with %32? – Tim Jul 05 '12 at 23:29
  • I've tried replacing it with `\\` (to an escaped slash character), but will try the percent escape next. I've also updated my main application build settings according to your link (set strip debug symbols to `NO` and set the helper application to "background only"). – Ash Furrow Jul 05 '12 at 23:37
  • I'm using a separate target in the same Xcode project, where the tutorial is using a sub-project. Could that affect anything? – Ash Furrow Jul 05 '12 at 23:38
  • Could be, I've seen at least one other post where it wasn't working with two targets. However, I can't really tell why my approach works and the two-target-approach doesn't. There's some Xcode magic in this.. – Tim Jul 05 '12 at 23:41
0

I had this problem. It was caused by launching the helper application myself from the bundle. Apparently, that's not OK with sandboxing? So:

  1. Opening the App bundle and clicking on the helper app in LoginItems always results in LSOpenFromURLSpec() returned -10827
  2. Logging out and logging back in, my helper app launches the main app fine without any problems.

This means the only way to test your helper app is by logging out and logging back in. Just running the app will always fail.

Levi Nunnink
  • 592
  • 5
  • 10