0

I have a NPAPI plugin for MAC that downloads and runs an Application from server.

When i use NSTask to open up the application. The application does not come to front.

NSBundle *bundle = [NSBundle bundleWithPath:AppPath];
NSString *path = [bundle executablePath];
NSTask *task = [[NSTask alloc] init];
NSArray *arguments;
arguments = [NSArray arrayWithObjects:AppParam,nil];

[task setLaunchPath:path];
[task setArguments:arguments];
[task launch];

I searched for the solution and came up with the code

int pid  = [task processIdentifier];
ProcessSerialNumber psn;
GetProcessForPID(pid, &psn);
SetFrontProcess(&psn);

but this code does not bring the application to front then i tried the following code.

NSRunningApplication *runapp = [NSRunningApplication runningApplicationWithProcessIdentifier:pid];
[runapp activateWithOptions:0];

This did not yield the desired result.

After some searches I came across posts that suggested the use of NSWorkspace to launch and activate the application. I came up with the code.

[workspace
    launchApplicationAtURL:[bundle bundleURL]
    options:NSWorkspaceLaunchNewInstance
    configuration:[NSDictionary
        dictionaryWithObject:arguments
        forKey:NSWorkspaceLaunchConfigurationArguments]
    error:error];

This works all OK BUT not under one situation. When the application is downloaded and executed FIRST TIME by the NSworkspace command MAC prompts with a dialog " is an application downloaded from the internet. Are you sure you want to open it".

If Pressed OK the application does launch but does not come to front. This dialog only appears if application is launch through NSWorkspace. Launching with NSTask, MAC does not prompt with the dialog before executing.

Could not find a solution that would go through the problem. I am OK with a confirmation dialog with NSWorkspace but can anyone suggest something that would still bring an application to front after clicking OK.

I also tried using.

[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
[[NSWorkspace sharedWorkspace] activeApplication];

Any help in this would be appreciated. Thanks in advance.

Regards,

LazyCoder7.

georgebrock
  • 28,393
  • 13
  • 77
  • 72
LazyCoder7
  • 45
  • 10

2 Answers2

2

You can manually strip the quarantine flag with xattr before you launch the app, if you want to bypass the dialog and thus avoid the focus problems it creates.

(This question really has nothing to do with NPAPI; you'd have exactly the same set of behaviors doing this from a stand-alone application.)

smorgan
  • 20,228
  • 3
  • 47
  • 55
0

You probably shouldn't bypass the quarantine flag generally. The need to do this generally indicates bad design.

NSWorkspace is going to run in user level permissions. NSTask can set its environment even though it defaults to that of the parent process.

uchuugaka
  • 12,679
  • 6
  • 37
  • 55
  • Depends on the design. If this is an NPAPI plugin that's downloading apps written by the same organization, from a server the plugin author controls (one hopes it's being done over https, so that it can't be MiTM'd), then having the user authorize the app is essentially pointless. It's all part of the same trust network in that case. I agree that if it's just downloading random apps, stripping the quarantine flag is a bad idea. But if it's random apps, downloading and running them without user intervention is a bad idea in the first place, so I was assuming it's not that. – smorgan Feb 20 '13 at 21:35
  • NO its not any random application. Its developed by the same author (ME) and for the same organization. So the application downloaded is a part of the trust network. – LazyCoder7 Feb 21 '13 at 07:08
  • But what ensures that your plugin doesn't get hijacked by malicious sites posing to provide your download but instead providing something else that installs itself? – uchuugaka Feb 21 '13 at 13:05