I want to use a command-line with a argument to call my cocoa app , but in my cocoa app ,how to receive the argument , this argument is a file path, thank you very much!
相关问题
- Xcode debugger displays incorrect values for varia
- Image loads in simulator but not device?
- importing files from other directories in xcode
- XCode Server: Opening import file for module '
- softlinks atime and mtime modification
相关文章
- xcode 4 garbage collection removed?
- Xcode: Is there a way to change line spacing (UI L
- Unable to process app at this time due to a genera
- Popover segue to static cell UITableView causes co
- “Storyboard.storyboard” could not be opened
- didBeginContact:(SKPhysicsContact *)contact not in
- Compile and build with single command line Java (L
- Converting (u)int64_t to NSNumbers
The normal main function in Cocoa passes the command line arguments to NSApplicationMain. The arguments are ignored by NSApplicationMain, but you are free to parse them as needed. There are a few standard ways to parse command line arguments, like getopt, or you can just access the values directly.
Note that launch services may pass command line arguments when an application is opened normally, for example when double clicked in the Finder. Be sure to handle unrecognized arguments.
In the special case of a file path to an existing file you can use
open
like this:And then implement this in your application delegate:
Neat thing: use
NSUserDefaults
.If you do:
Then in your code you can do:
The
key
is the-argument
switch, and the value is the thing that comes after it. Note that this isn't very flexible (you can't do combine options:-a -l
≠-al
), but for rudimentary arguments, this is dead simple.edit with multiple arguments:
And then extract via: