Source Code for the open command

We have develloped a small multiplateform library in c++. Among other things there is a cmd launcher, roughly imagine these:


pid_t pid = Launch( wchar_t* cmd, [ wchar_t* stderr, wchar_t* stdout, wchar_t* stdin ] )
kill( pid_t pid ) // Kills the pgid 
wait( pid_t pid , timeout t, bool kill ) // Kill after timeout or pass

...The problem is:


Our library users have noticed that, if they launch a graphical app without using open:


> wchar_t* cmd = myApp myArgs
instead of 
> wchar_t* cmd = open myApp myArgs


Then many gui events would not wok properly. But there are a lot of problem with that:


  • The pid returned is the one of open not the app launched
  • Even using open -W killing the process group id of open does not kill the launched app


I have searched here

http://opensource.apple.com/release/os-x-10112/
http://opensource.apple.com/source/system_cmds/system_cmds-671.10.3/

http://opensource.apple.com/source/shell_cmds/shell_cmds-187/

As well as bash/zsh/tcsh...


I haven't been able to find the source code of the open command. I need to be able to simulate its "gui foreground" behaviour when the lib is ran on mac os x. Any idea is very welcome.


Cheers, and thanks to have read up to here already 🙂

open
is not open source.

Last I checked

open
does most of its work using public Launch Services / NSWorkspace APIs.

The biggest gotcha here is the context in which you call these APIs; these only work reliably in a GUI login context, so if your code calls them from, say, a daemon context, things are not going to work reliable. I’m not sure if that’s a practical concern in your library.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks, "Launch Services / NSWorkspace APIs." I have no Idea what this is, I'm more of a posix guy but if I can call those from c++ I'll check it out.


The context ?
A gui application (double click launched) launching another instance of self with fork/execve. The clients tell me the child does not receive some clicks properly if the execve command line is not using "open"...


Thanks a lot anyway 🙂

"Launch Services / NSWorkspace APIs." I have no Idea what this is, I'm more of a posix guy but if I can call those from c++ I'll check it out.

Lots of docs for these in the Mac Developer Library.

A gui application (double click launched) launching another instance of self with fork/execve.

By this I’m assuming that your code is running inside a GUI app. If you’re prepared to require that, this problem is pretty simple. You can use

-[NSWorkspace launchApplicationAtURL:options:configuration:error:]
to start the app. This returns an instance of NSRunningApplication, which has a
processIdentifier
property that yields a
pid_t
.

Having said that, waiting on a pid is a bit silly when you’re dealing with a GUI app because there’s no guarantee that the app’s process will terminate when the user stops working with it.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

"Having said that, waiting on a pid is a bit silly when you’re dealing with a GUI app because there’s no guarantee that the app’s process will terminate when the user stops working with it."


They are using the gui app to launch itself for unit testing. Each unit test declares its max running time. If not returned in due time, it is killed. 🙂


thanks a lot this really helps!

Source Code for the open command
 
 
Q