OSX Open Finder on a specific folder from a c/c++ program

It’s so stupid that I have to write it somewhere. I spent way too much time on this problem ;-)

I just wanted to open the finder app from my c/c++ Application . After a little research on Google I came across the solution (well that’s what I thought) a simple

system ('open mypath')

should do the trick.

I test it in Xcode … Bingo it works, I was so happy !

Unfortunately, when I ran my program outside of XCode (i.e. from the Finder)  => drama. Nothing worked, the “system” function returns me errors. Why ? I still don’t know, is it a privilege problem ? a sandbox problem ? If you got the answer please let me a comment, I’m really curious.
In fact the solution was more complex than expected, Thanks to qtcreator’s source code, It finally ended this way : You have to execute an applescript (via osascript) to tell the finder to open a POSIX file and another one to tell the finder to be in front of all others windows… Are you serious Apple ???


std::string tempPath;
std::string command;
tempPath = currentDir;
command = "osascript -e 'tell app \"Finder\" to open POSIX file \""+ tempPath+"\"'";
system(command.c_str());
command = "osascript -e 'tell application \"Finder\" to activate'";
system(command.c_str());