开发者

transparent process creation for cocoa components

I have an application A which may or may not need to spawn an application B and will communicate with it using remote messaging (via NSConnections etc.).

While i know how to do th开发者_StackOverflow社区is if B is started first, i wonder:

What is a clean cocoa-based approach of transparently starting B on demand?

(For those familiar with COM, i am effectively looking for a CoCreateInstance() equivalent)


If this is a GUI app, you could do something like this for 10.6:

NSArray * runningBs = [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.example.B"];
if ([runningBs count] == 0) {
  NSURL * bURL = [[NSWorkspace sharedWorkspace] URLForApplicationWithBundleIdentifier:@"com.example.B"];
  NSRunningApplication * b = [[NSWorkspace sharedWorkspace] launchApplicationAtURL:bURL options:NSWorkspaceLaunchDefault configuration:nil error:nil];
}

For 10.5:
Use -[NSWorkspace launchedApplications] and iterate through the array to see if you find B.
If you don't, find the [NSWorkspace absolutePathForAppBundleWithIdentifier:] and then use one of the [NSWorkspace launchApplication:] varieties.


If the other application is a command-line app or can behave like one, NSTask is the best choice — you can launch another program and define its standard input, output and error streams. If this is a GUI app, you can use Scripting Bridge or NSWorkspace + use your own communication protocol with NSConnection, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜