Re: how to make cocoa application run as a command line tool?
Re: how to make cocoa application run as a command line tool?
- Subject: Re: how to make cocoa application run as a command line tool?
- From: Jeremy Pereira <email@hidden>
- Date: Tue, 20 Oct 2009 12:20:43 +0100
On 20 Oct 2009, at 11:42, Paul M wrote:
On 20/10/2009, at 10:58 PM, XiaoGang Li wrote:
Greetings,
I have created an document-based cocoa application, now I
need to
provide a command line interface for my users.
for example, users input this into the terminal:
./myApplication.app/Contents/MacOS/myApplication -c --srcFolder "A/
B/C"
--dstFolder "A/B/D";
I can get the argument information through [[NSProcessInfo
processInfo]
arguments] in the init method of the application
delegate, and parse the arguments, then step by step.
My question is that, I don't want the window and other document be
displayed
on the screen, even the menu.
I want all the action be processed without user's interventio.
Maybe, this feature seems odd. anyway, however, user can open my
application
in the Finder, and open a document to edit it. but they
also can run it like a shell command utility to do some other
faceless work,
like convert the type the document to another type.
I don't know whether I have a detailed description for my issue,
but I will
be very appreciated for your feedback.
Your app has to detect whether it's been started from the finder or
whether
it's being called from the command line. I'm not sure off the top of
my head
how best to do this, but it shouldn't be too hard to figure out a way.
Then it'll either show or not show your UI, as appropriate. Simple.
When I need to do this, I create a switch which puts the app into
"command line" mode.
static int commandLine(int argc, const char* argv[]);
int main(int argc, char *argv[])
{
if (argc > 1)
{
if (strcmp(argv[1], "-commandLine") == 0)
{
return commandLine(argc, argv);
}
}
else
{
return NSApplicationMain(argc, (const char **) argv);
}
}
This way I can run the binary directly from the command line supplying
the argument:
/Applications/MyApp.app/Contents/MacOS/MyApp -commandLine other args
Or
open /Applications/MyApp.app --args -commandLine other args
NB I haven't actually tried the open method to see if it works.
There's multiple ways to structure this though. You could have 2
separate
apps, but built with as much common code as possible; you could have
a single
app which shows or doesnt show the UI - as described above; you
could even have
a 3rd app which does the heavy lifting, but is called by either a
command line
tool or by a regular cocoa based app.
Without knowing what exactly you're trying to do, it's hard to
comment on the
best aproach.
By the way, an alternative aproach to calling into your application
bundle
from the command line is to simply type 'open -a myApplication',
however I
dont think this will allow you to pass args as you've described above.
paulm
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden