Re: Launch app with params as front process and wait?
Re: Launch app with params as front process and wait?
- Subject: Re: Launch app with params as front process and wait?
- From: Ken Thomases <email@hidden>
- Date: Sat, 17 Apr 2010 14:38:32 -0500
On Apr 17, 2010, at 12:43 PM, Gaurav Srivastava wrote:
> I am using following:
>
> NSTask* task = [NSTask launchedTaskWithLaunchPath:@"path" arguments:args];
> int pid = [task processIdentifier];
> ProcessSerialNumber psn;
> GetProcessForPID(pid, &psn);
> SetFrontProcess(&psn);
> [task waitUntilExit];
> SetFrontProcess(&psn);
>
> Even on switching to NSWorkSpace doesn't seem to solve the purpose. The launched application does not get highlighted. Also, one more issue I am facing is how to send custom events from launched application to parent application. Thanks in advance.
First, you have to wait for the process to have gotten to a certain point in its startup before trying to make it the front process. It has to acquire its connection to the window server and check in. If you try to SetFrontProcess immediately, as shown above, it probably won't work. In fact, you may be failing to acquire the PSN. I hope in your real code you're checking all return values, etc. This is why I suggested that you wait for the NSWorkspace notification that the process did launch.
Second, are you by any chance trying to launch a program which is not packaged into an application bundle but is trying to be a GUI application? The symptoms you describe sound like it. Another way of asking this is, does the program work normally (become frontmost, process mouse and keyboard, etc.) if launched from the Finder?
An unbundled GUI application does not work properly without some extra work. You should build a proper application bundle for any GUI app. If there's some real reason that you can't bundle the application, it can call TransformProcessType() to become a proper GUI application.
As far as sending custom events, there are a couple of approaches. Since you seem to be sticking with NSTask, you can communicate between the processes using pipes for standard input and output. You can use Distributed Objects. You can use a Unix-domain socket. You can make the parent application scriptable and have the child script it with AppleEvents. Etc.
Regards,
Ken
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden