• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Default command line arguments
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Default command line arguments


  • Subject: Re: Default command line arguments
  • From: Chris Espinosa <email@hidden>
  • Date: Thu, 19 May 2005 10:55:21 -0700


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: 

http://developer.apple.com/documentation/DeveloperTools/Conceptual/XcodeUserGuide20/Contents/Resources/en.lproj/db_executable/chapter_38_section_5.html

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.

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Default command line arguments (From: Dieter Oberkofler <email@hidden>)

  • Prev by Date: Re: XCode 2.0 build style selection
  • Next by Date: Re: Executable Argument based on Target
  • Previous by thread: Default command line arguments
  • Next by thread: Various Key Binding Assignment Bugs and Restrictions
  • Index(es):
    • Date
    • Thread