PDA

View Full Version : Command Line Tool Executable from Finder?




Duke Leto
Apr 21, 2011, 03:21 PM
I made a project in C that was a "command line tool" in XCode. It uses GLUT/OpenGL, so it does have a user interface. It works fine, and I can run it from Terminal, but I was wondering if there was any way to allow it to run from Finder.

I did create a new target (a Cocoa application), and I included the linked frameworks and removed the prefix header and it compiles and runs. However, it won't work from Finder. I think that the problem is that my program relies on a file named "Input" that it assumes is in its directory. I tried using
FILE* data = fopen("../../Input", "r");

instead of
FILE* data = fopen("Input", "r");

but that didn't work. (I also tried more levels of up and down, that doesn't seem to work either.)

I hope that that ramble was half-intelligible!

Thanks in advance for any help.



chown33
Apr 21, 2011, 04:16 PM
In both cases of fopen(), you are assuming a particular value for current working directory, and from which your relative pathnames are assumed to be sensible.

I suggest checking your assumption of working directory, perhaps by NSLog()'ing the actual value, such as returned by the C function getcwd(). You should read the man page for getcwd() to see what its parameters are.

Duke Leto
Apr 21, 2011, 05:48 PM
I looked into getcwd, and I was able to get the directory of the executable. I wrote some code to modify the path to go up a couple of folders, and I verified that it did the correct modifications. Furthemore, I found that the program worked just fine when I opened the package contents of the .app file and ran the executable inside the MacOS folder from Terminal.
However, the .app will not run from Finder.

chown33
Apr 21, 2011, 07:13 PM
I looked into getcwd, and I was able to get the directory of the executable. I wrote some code to modify the path to go up a couple of folders, and I verified that it did the correct modifications. Furthemore, I found that the program worked just fine when I opened the package contents of the .app file and ran the executable inside the MacOS folder from Terminal.
However, the .app will not run from Finder.

Running the executable using Terminal, after cd'ing into the .app Contents/Resources is not the same thing as running it from Finder.

You should look at the working directory when run from Finder, using the debugger if you have to.

You're making a faulty generalization based on what happens in Terminal. In particular, I can almost guarantee that launching from Finder DOES NOT set the working directory to the location of the executable.