Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 31, 2012, 11:09 AM   #1
silvercircle
macrumors member
 
Join Date: Nov 2010
NSDocument based app takes each command line argument as a filename

For each argument I supply on the command line I get a message that the application can't open files of that type.
It is an NSDocument based application for OSX.
I want to use the arguments later on via [[NSProcessInfo processInfo] arguments], but how do I avoid that each argument is taken as a file to open?
silvercircle is offline   0 Reply With Quote
Old Jan 1, 2013, 12:38 PM   #2
Senor Cuete
macrumors regular
 
Join Date: Nov 2011
An NSDocument application is not designed to be used as a command line application. Mac OS X is a UNIX based OS. The main() function in a Mac OS app looks like this:

Code:
int main(int argc, char *argv[])
{
    return NSApplicationMain(argc, (const char **) argv);
}
The NSDocument application thinks you are opening it by dragging a file and dropping it on the app's icon with the finder. Open and run the app by using the run button in the upper left corner of the XCode window.
Senor Cuete is offline   0 Reply With Quote
Old Jan 1, 2013, 01:25 PM   #3
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by silvercircle View Post
... but how do I avoid that each argument is taken as a file to open?
You could write some code in main() that parses the argc/argv it's been given, and saves it for later using your own code. Then call NSApplicationMain() with a fake argc/argv that prevents NSApplicationMain() from treating the real argc/argv as filenames to open.

Using Senor Cuete's code as an illustration:
Code:
int main(int argc, char *argv[])
{
   // Your new code goes here.

   // Make fake argc/argv here.

    return NSApplicationMain( fake_argc, fake_argv);
}
There are some real and important args passed to NSDocument-based applications, so this isn't as simple as it may seem. It's possibly you won't be able to come up with a satisfactory solution. In that case, don't use the NSDocument application as a command-line tool, and instead write a separate command-line tool. It can reside in the same sub-dir as the application executable, or it can reside under Resources. I think TextWrangler and BBEdit have bundled command-line tools, so look at them.
chown33 is offline   0 Reply With Quote
Old Jan 1, 2013, 01:43 PM   #4
silvercircle
Thread Starter
macrumors member
 
Join Date: Nov 2010
Quote:
Originally Posted by chown33 View Post
You could write some code in main() that parses the argc/argv it's been given, and saves it for later using your own code. Then call NSApplicationMain() with a fake argc/argv that prevents NSApplicationMain() from treating the real argc/argv as filenames to open.

Using Senor Cuete's code as an illustration:
Code:
int main(int argc, char *argv[])
{
   // Your new code goes here.

   // Make fake argc/argv here.

    return NSApplicationMain( fake_argc, fake_argv);
}
There are some real and important args passed to NSDocument-based applications, so this isn't as simple as it may seem. It's possibly you won't be able to come up with a satisfactory solution. In that case, don't use the NSDocument application as a command-line tool, and instead write a separate command-line tool. It can reside in the same sub-dir as the application executable, or it can reside under Resources. I think TextWrangler and BBEdit have bundled command-line tools, so look at them.
I guess you are right, adding a command-line tool might be the best way to go.
Calling NSApplicationMain() with fake argc/argv is indeed not that simple, the args passed are not documented (as fas as I know). So not easy to filter them out.
Thanks for putting me on the right track!
silvercircle is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Dictonary from command line Crusader007 OS X 1 Feb 25, 2014 08:21 PM
General: iOS7 command line... Equivalent of "say" command? RobertoG Jailbreaks and iOS Hacks 0 Jan 19, 2014 06:05 PM
Command Line newb jeanniebeannie Mac Basics and Help 8 Oct 9, 2013 12:25 PM
Need Command-line help EricBrian Community Discussion 1 Feb 20, 2013 09:40 PM
Getting Argument Exception (nil URL argument)! sridvijay Mac Programming 0 Jun 8, 2012 11:35 PM

Forum Jump

All times are GMT -5. The time now is 08:29 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC