Welcome to Soft32 Forums!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Command-line arguments in Mac App

 
   Soft32 Home -> Mac -> Programmer Help RSS
Next:  Embedding the SDL library in an application  
Author Message
Dave Chen

External


Since: Mar 01, 2004
Posts: 2



(Msg. 1) Posted: Mon Mar 01, 2004 8:45 am
Post subject: Command-line arguments in Mac App
Archived from groups: comp>sys>mac>programmer>help (more info?)

I'm porting a Unix program to Mac OS X. I've made a bundle for the app,
and I'd like to be able to have the executable run with some command
line arguments. I was hoping there was a way to specify the arguments
in the Info.plist or something like that. But I couldn't find any such
mechanism.

Does anyone have any ideas?

Thanks in advance.

dave chen
Back to top
Login to vote
Miro Jurisic

External


Since: May 10, 2004
Posts: 1194



(Msg. 2) Posted: Mon Mar 01, 2004 8:57 am
Post subject: Re: Command-line arguments in Mac App [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <DpM0c.124$Pp2.352@mencken.net.nih.gov>,
Dave Chen <dchen RemoveThis @mail.nih.gov> wrote:

> I'm porting a Unix program to Mac OS X. I've made a bundle for the app,
> and I'd like to be able to have the executable run with some command
> line arguments. I was hoping there was a way to specify the arguments
> in the Info.plist or something like that. But I couldn't find any such
> mechanism.
>
> Does anyone have any ideas?

There is nothing that prevents you from putting code inside the app that reads
arguments from Info.plist into an array and then calls the equivalent of main()
with those arguments. There is no standard way for it, though.

meeroh

--
If this message helped you, consider buying an item
from my wish list: <http://web.meeroh.org/wishlist>
Back to top
Login to vote
Dave Chen

External


Since: Mar 01, 2004
Posts: 2



(Msg. 3) Posted: Mon Mar 01, 2004 9:19 am
Post subject: Re: Command-line arguments in Mac App [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Miro Jurisic wrote:

> There is nothing that prevents you from putting code inside the app that reads
> arguments from Info.plist into an array and then calls the equivalent of main()
> with those arguments. There is no standard way for it, though.

> meeroh


Yeah, but I was hoping to keep the code as genericly Unix as possible.

dave
Back to top
Login to vote
Mark Haase

External


Since: Aug 16, 2003
Posts: 194



(Msg. 4) Posted: Mon Mar 01, 2004 2:47 pm
Post subject: Re: Command-line arguments in Mac App [Login to view extended thread Info.]
Imported from groups: per prev. post (more info?)

This message is not archived
Back to top
Login to vote
Michael Milvich

External


Since: Jul 14, 2003
Posts: 19



(Msg. 5) Posted: Wed Mar 03, 2004 3:37 am
Post subject: Re: Command-line arguments in Mac App [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Dave Chen <dchen RemoveThis @mail.nih.gov> wrote in message news:<DpM0c.124$Pp2.352@mencken.net.nih.gov>...
> I'm porting a Unix program to Mac OS X. I've made a bundle for the app,
> and I'd like to be able to have the executable run with some command
> line arguments. I was hoping there was a way to specify the arguments
> in the Info.plist or something like that. But I couldn't find any such
> mechanism.
>
> Does anyone have any ideas?

Why don't you make the main excutable a shell script that will in turn
execute your program with the correct arguments?

Michael
Back to top
Login to vote
Simon Slavin

External


Since: May 16, 2004
Posts: 781



(Msg. 6) Posted: Wed Mar 03, 2004 2:31 pm
Post subject: Re: Command-line arguments in Mac App [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 01/03/2004, Dave Chen wrote in message
<4VM0c.125$Pp2.413@mencken.net.nih.gov>:

> Yeah, but I was hoping to keep the code as genericly Unix as possible.

You can, by all means, use argv and argc. They work fine. But
since most OS X users will be launching your application using the
GUI, there will be no arguments.

You seem to be using the command line for something it was never
intended for. Generic /Windows/ program use PIFs to feed stuff in
via the command line. Generic Unix programs use the command line
only to notice stuff fed in my the user or by a shell script.

Simon.
--
Using pre-release version of newsreader.
Please tell me if it does weird things.
Back to top
Login to vote
Michael Ash

External


Since: Mar 01, 2004
Posts: 20



(Msg. 7) Posted: Thu Mar 04, 2004 11:58 pm
Post subject: Re: Command-line arguments in Mac App [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Fri, 5 Mar 2004, David Phillip Oster wrote:

> > Actually, there *will* be arguments. The Finder sends some info as
> > command-line arguments into applications it launches.
>
> News to me. I've been writing Mac software since 1984, and every app was
> the equivalent of (some were in Pascal, Forth, and Assembler):
>
> void main(void){
> }
>
> The Finder sends appleEvents, and has since System 7. For examples,
> just look at the results from this Google search:
>
> <http://www.google.com/search?as_q=+main+void&as_sitesearch=apple.com>
>
> Can you give references and examples to back up your assertion that the
> Finder sends info as command-line arguments?

I wrote a quick test program. I created a new Cocoa app, then changed its
main() to this:

int main(int argc, char *argv[])
{
int i;
for(i = 0; i < argc; i++)
NSLog(@"Arg %d = %s\n", i, argv[i]);
return NSApplicationMain(argc, argv);
}

Upon running from the Finder, I get the following in the Console:

2004-03-05 11:57:02.594 ArgsTest[582] Arg 0 =
/Users/mikeash/Development-Build-Products/ArgsTest.app/Contents/MacOS/ArgsTest
2004-03-05 11:57:02.595 ArgsTest[582] Arg 1 = -psn_0_2228225

The Finder doesn't pass files to open as command-line arguments, but in OS
X, the PSN information is passed as command-line arguments. OS X is a very
different beast from previous Mac OSes.
Back to top
Login to vote
Uli Kusterer

External


Since: Jun 22, 2005
Posts: 268



(Msg. 8) Posted: Fri Mar 05, 2004 2:41 am
Post subject: Re: Command-line arguments in Mac App [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <c25tit$qcc$1$830fa79d@news.demon.co.uk>,
Simon Slavin <slavins.delete.these.four.words.DeleteThis@hearsay.demon.co.uk>
wrote:

> On 01/03/2004, Dave Chen wrote in message
> <4VM0c.125$Pp2.413@mencken.net.nih.gov>:
>
> > Yeah, but I was hoping to keep the code as genericly Unix as possible.
>
> You can, by all means, use argv and argc. They work fine. But
> since most OS X users will be launching your application using the
> GUI, there will be no arguments.

Actually, there *will* be arguments. The Finder sends some info as
command-line arguments into applications it launches.

Cheers,
-- Uli
http://www.zathras.de
Back to top
Login to vote
David Phillip Oster

External


Since: Jul 21, 2005
Posts: 1426



(Msg. 9) Posted: Fri Mar 05, 2004 2:41 am
Post subject: Re: Command-line arguments in Mac App [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <witness-178C0F.08411505032004.DeleteThis@news.t-online.com>,
Uli Kusterer <witness.DeleteThis@t-online.de> wrote:

> In article <c25tit$qcc$1$830fa79d@news.demon.co.uk>,
> Simon Slavin <slavins.delete.these.four.words.DeleteThis@hearsay.demon.co.uk>
> wrote:
>
> > On 01/03/2004, Dave Chen wrote in message
> > <4VM0c.125$Pp2.413@mencken.net.nih.gov>:
> >
> > > Yeah, but I was hoping to keep the code as genericly Unix as possible.
> >
> > You can, by all means, use argv and argc. They work fine. But
> > since most OS X users will be launching your application using the
> > GUI, there will be no arguments.
>
> Actually, there *will* be arguments. The Finder sends some info as
> command-line arguments into applications it launches.

News to me. I've been writing Mac software since 1984, and every app was
the equivalent of (some were in Pascal, Forth, and Assembler):

void main(void){
}

The Finder sends appleEvents, and has since System 7. For examples,
just look at the results from this Google search:

<http://www.google.com/search?as_q=+main+void&as_sitesearch=apple.com>

Can you give references and examples to back up your assertion that the
Finder sends info as command-line arguments?
Back to top
Login to vote
it_means_"halo,_then_reso

External


Since: Mar 05, 2004
Posts: 162



(Msg. 10) Posted: Fri Mar 05, 2004 2:41 am
Post subject: Re: Command-line arguments in Mac App [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

David Phillip Oster wrote:
> Can you give references and examples to back up your assertion that
> the Finder sends info as command-line arguments?

it sends the process serial number as an argument when it starts a gui
cocoa app. i can say that for certain, because i was recently adding a
few debugging command-line options to one of my apps, and was very
surprised to see an argument on the command line that i definitely did
not put there myself.

it would take me awhile to derive a minimal gui app i could paste in
here to "prove" it. put a couple of printf()s in one of your gui app's
main() function and see for yourself.
Back to top
Login to vote
David Phillip Oster

External


Since: Jul 21, 2005
Posts: 1426



(Msg. 11) Posted: Fri Mar 05, 2004 11:08 am
Post subject: Re: Command-line arguments in Mac App [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <20040305055619.F75251.RemoveThis@agamemnon.twistedsys.net>,
Michael Ash <mikeash.RemoveThis@mikeash.com> wrote:

> On Fri, 5 Mar 2004, David Phillip Oster wrote:
>
> > > Actually, there *will* be arguments. The Finder sends some info as
> > > command-line arguments into applications it launches.
> >
> > News to me. I've been writing Mac software since 1984, and every app was
> > the equivalent of (some were in Pascal, Forth, and Assembler):
> >
> > void main(void){
> > }
> >
> > The Finder sends appleEvents, and has since System 7. For examples,
> > just look at the results from this Google search:
> >
> > <http://www.google.com/search?as_q=+main+void&as_sitesearch=apple.com>
> >
> > Can you give references and examples to back up your assertion that the
> > Finder sends info as command-line arguments?
>
> I wrote a quick test program. I created a new Cocoa app, then changed its
> main() to this:
>
> int main(int argc, char *argv[])
> {
> int i;
> for(i = 0; i < argc; i++)
> NSLog(@"Arg %d = %s\n", i, argv[i]);
> return NSApplicationMain(argc, argv);
> }
>
> Upon running from the Finder, I get the following in the Console:
>
> 2004-03-05 11:57:02.594 ArgsTest[582] Arg 0 =
> /Users/mikeash/Development-Build-Products/ArgsTest.app/Contents/MacOS/ArgsTest
> 2004-03-05 11:57:02.595 ArgsTest[582] Arg 1 = -psn_0_2228225
>
> The Finder doesn't pass files to open as command-line arguments, but in OS
> X, the PSN information is passed as command-line arguments. OS X is a very
> different beast from previous Mac OSes.
>

You are right. Now that you mention it, I remember seeing that when I
open a Terminal window and do a:

ps xl
Back to top
Login to vote
Uli Kusterer

External


Since: Jun 22, 2005
Posts: 268



(Msg. 12) Posted: Sat Mar 06, 2004 12:22 am
Post subject: Re: Command-line arguments in Mac App [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <oster-CE732F.23591004032004.RemoveThis@news.sf.sbcglobal.net>,
David Phillip Oster <oster.RemoveThis@ieee.org> wrote:

> > Actually, there *will* be arguments. The Finder sends some info as
> > command-line arguments into applications it launches.
>
> News to me. I've been writing Mac software since 1984, and every app was
> the equivalent of (some were in Pascal, Forth, and Assembler):
>
> void main(void){
> }

That is how it was in Classic MacOS, yes.

> The Finder sends appleEvents, and has since System 7. For examples,
> just look at the results from this Google search:

Yes, the Finder sends Apple Events for opening files etc. (and it
actually faked menu choices before that -- remember 'mstr' or whatever
the resource for the menu item names was?)

> Can you give references and examples to back up your assertion that the
> Finder sends info as command-line arguments?

It came up on Carbon-Dev a while ago. Can't find it right now, though.
Apparently when a bundled application is launched through the Finder, it
gets a command-line parameter, which is some sort of process ID or
window server connection ID or something of that sort. Caused a few
command-line applications that had simply been wrapped in a bundle to
abort because of the unexpected parameter.

Might be that it's restricted to a particular object file format,
though. Either it only happens with CFM apps, or (more likely) it only
happens with MachO apps.

Cheers,
-- Uli
http://www.zathras.de
Back to top
Login to vote
Eric Albert

External


Since: Jan 09, 2004
Posts: 683



(Msg. 13) Posted: Sat Mar 06, 2004 10:12 am
Post subject: Re: Command-line arguments in Mac App [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

In article <witness-1C192F.06224306032004 RemoveThis @news.t-online.com>,
Uli Kusterer <witness RemoveThis @t-online.de> wrote:

> In article <oster-CE732F.23591004032004 RemoveThis @news.sf.sbcglobal.net>,
> David Phillip Oster <oster RemoveThis @ieee.org> wrote:
>
> > > Actually, there *will* be arguments. The Finder sends some info as
> > > command-line arguments into applications it launches.
> >
> > News to me. I've been writing Mac software since 1984, and every app was
> > the equivalent of (some were in Pascal, Forth, and Assembler):
> >
> > void main(void){
> > }
>
> That is how it was in Classic MacOS, yes.
>
> > The Finder sends appleEvents, and has since System 7. For examples,
> > just look at the results from this Google search:
>
> Yes, the Finder sends Apple Events for opening files etc. (and it
> actually faked menu choices before that -- remember 'mstr' or whatever
> the resource for the menu item names was?)
>
> > Can you give references and examples to back up your assertion that the
> > Finder sends info as command-line arguments?
>
> It came up on Carbon-Dev a while ago. Can't find it right now, though.
> Apparently when a bundled application is launched through the Finder, it
> gets a command-line parameter, which is some sort of process ID or
> window server connection ID or something of that sort. Caused a few
> command-line applications that had simply been wrapped in a bundle to
> abort because of the unexpected parameter.
>
> Might be that it's restricted to a particular object file format,
> though. Either it only happens with CFM apps, or (more likely) it only
> happens with MachO apps.

If you run 'ps -auxwww' you'll see that both Mach-O and CFM applications
have a -psn_<something> argument. I'm not sure what it's for, but all
apps ignore it.

-Eric

--
Eric Albert ejalbert RemoveThis @cs.stanford.edu
http://rescomp.stanford.edu/~ejalbert/
Back to top
Login to vote
Display posts from previous:   
Related Topics:
GUI wrapper for command line application - Hi, I'm trying to write a GUI wrapper for a command line application and am following Apple's Moriarity example. Laun...

Shell command line to set Login startup items - What is the shell command line to set Login startup items (like in Account System Preferences) ?

Method or Shell command line to set Login startup item - What is the Ojective C method or shell command line to set Login startup item (like in Account System Preferences) ?

How to read file line by line??? - Hi All, I want to read the file line by line.Is there any API like getline..and also the function like strtok to be u...

Command-Option-D - Hello out there, In our application we have Command-Option-D assigned to a menu command. In Mac OS 10.1, our applicat...

Error -2110 in open command - Not sure which group this should go to, but I will post here because it is related to development. I am a newbie with r...
       Soft32 Home -> Mac -> Programmer Help All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum

Categories:
 Windows
 Linux
  Mac
 PDA


[ Contact us | Terms of Service/Privacy Policy ]