On May 19, 2005, at 9:32 AM, Dieter Oberkofler wrote: When starting an application from the OS X finder, it seems as if it would automatically get some kind of a default command line parameter starting with -NP. Where does this argument come from and what does it mean?
If you're speaking of the '-psn_0_xxxxxxx' argument that you're finding in argv[1], it's supplied by the Finder.
In general, end-user GUI applications on Mac OS X should not rely on UNIX environment variables or launch arguments, as these vary greatly according to how the application is launched.
I posted thisto the carbon-dev group last week, it's a decent summary of the situation:
1) If it's a CFM application, you are out of luck; CFM apps are loaded by a separate process (LaunchCFMApp) and won't have access to any arguments.
2) If it's a Mach-O application, then you must write your main() function with the standard prototype:
int main (int argc, const char * argv[])
Then within your main() you can retrieve the command-line arguments in the normal argv[0], argv[1], etc. manner.
3) When launching from the Finder, the only arguments you'll see are the following:
argv[0]: path to your executable (inside your .app bundle, e.g. /volume/path/foo.app/Contents/MacOS/foo) argv[1]: the Carbon ProcessSerialNumber in the form -psn_0_XXXXXXXX
You can't pass other arguments when launching from the Finder.
4) You can launch a Mach-O application from the command line by executing the executable inside the bundle directly. You can then add whatever command-line arguments you want:
CDE-Nimitz:~ espich$ /Volumes/Local/Objects/Debug/argvapp.app/Contents/MacOS/argvapp -foo -bar argv[0] : /Volumes/Local/Objects/Debug/argvapp.app/Contents/MacOS/argvapp argv[1] : -foo argv[2] : -bar
NOTE that in these two cases argv[1] is different than it is when launching from the Finder. You probably need to special-case this in your code.
5) When developing in Xcode, you can select the executable and choose Get Info, and set the command-line arguments to use when debugging or executing from Xcode by adding them to the arguments list. These are passed in the same manner they would be in 4). See:
This is especially convenient because you can check and uncheck arguments to be used (so you don't have to retype them every run), and drag them in the list to order them.
|