English 中文(简体)
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 this 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.





相关问题
2 mysql instances in MAC

i recently switched to mac. first and foremost i installed xampp. then for django-python-mysql connectivity, i "somehow" ended up installing a seperate MySQL. now the seperate mysql installation is ...

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Controlling OSX windows

I m trying to control windows of a foreign OSX applications from my application. I d like to 1. move the windows on the screen 2. resize the windows on the screen 3. change the currently active window ...

Switching J2SE versions on Mac OS (SnowLeopard)

My current JDK on Mac OS (10.6) is set to 1.6 and I d like to switch to 1.5. A listing of /System/Library/Frameworks/JavaVM.framework/Versions/ shows: lrwxr-xr-x 1 root wheel 10 Nov 3 18:34 ...

Scrolling inside Vim in Mac s Terminal

I ve been googling around trying to figure out if it s possible to use my mouse wheel to scroll while inside Vim in Mac s Terminal, with no luck. It seems as if only X11 or iTerm support this. Before ...

export to MP3 from quicktime API

A question for Apple,QT programmers. Would like to know if it s possible to export a Movie object to MP3 using the QuickTime API. Preferably the ConvertMovieToFile function. I ve looked at ...

热门标签