Professional Web Applications Themes

Command-line arguments in Mac App - Mac Programming

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...

  1. #1

    Default Command-line arguments in Mac App

    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
    Dave Guest

  2. #2

    Default Re: Command-line arguments in Mac App

    In article <DpM0c.124$net.nih.gov>,
    Dave Chen <nih.gov> 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

    --
    If this message helped you, consider buying an item
    from my wish list: <http://web.meeroh.org/wishlist>

    Miro Guest

  3. #3

    Default Re: Command-line arguments in Mac App

    Miro Jurisic wrote:
     
     


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

    dave
    Dave Guest

  4. #4

    Default Re: Command-line arguments in Mac App

    In article <4VM0c.125$net.nih.gov>,
    Dave Chen <nih.gov> wrote:
     
    >
    > Yeah, but I was hoping to keep the code as genericly Unix as possible.
    >
    > dave[/ref]

    I think what Meeroh was suggesting is that you wrap the unix code in a
    mac-specific function which reads parameters from an info.plist, then
    calls the unix code's main function--as if it were a shell program
    exec-ing a subprocess.

    This would keep the code portable, with just some extra code (maybe
    activated by compiler flag) to make a mac application.

    --
    |\/| /| |2 |<
    mehaase(at)sas(dot)upenn(dot)edu
    Mark Guest

  5. #5

    Default Re: Command-line arguments in Mac App

    Dave Chen <nih.gov> wrote in message news:<DpM0c.124$net.nih.gov>... 

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

    Michael
    Michael Guest

  6. #6

    Default Re: Command-line arguments in Mac App

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

    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.
    Simon Guest

  7. #7

    Default Re: Command-line arguments in Mac App

    In article <c25tit$qcc$1$demon.co.uk>,
    Simon Slavin <demon.co.uk>
    wrote:
     
    >
    > 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.[/ref]

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

    Cheers,
    -- Uli
    http://www.zathras.de
    Uli Guest

  8. #8

    Default Re: Command-line arguments in Mac App

    In article <t-online.com>,
    Uli Kusterer <de> wrote:
     
    > >
    > > 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.[/ref]
    >
    > Actually, there *will* be arguments. The Finder sends some info as
    > command-line arguments into applications it launches.[/ref]

    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?
    David Guest

  9. #9

    Default Re: Command-line arguments in Mac App

    On Fri, 5 Mar 2004, David Phillip Oster wrote:
     
    >
    > 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?[/ref]

    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.

    Michael Guest

  10. #10

    Default Re: Command-line arguments in Mac App

    David Phillip Oster wrote: 

    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.
    Jhnny Guest

  11. #11

    Default Re: Command-line arguments in Mac App

    In article <twistedsys.net>,
    Michael Ash <com> wrote:
     
    > >
    > > 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?[/ref]
    >
    > 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.
    >[/ref]

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

    ps xl
    David Guest

  12. #12

    Default Re: Command-line arguments in Mac App

    In article <sf.sbcglobal.net>,
    David Phillip Oster <org> wrote:
     
    >
    > 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){
    > }[/ref]

    That is how it was in Classic MacOS, yes.
     

    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?)
     

    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
    Uli Guest

  13. #13

    Default Re: Command-line arguments in Mac App

    In article <t-online.com>,
    Uli Kusterer <de> wrote:
     
    > >
    > > 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){
    > > }[/ref]
    >
    > That is how it was in Classic MacOS, yes.

    >
    > 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?)

    >
    > 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.[/ref]

    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 stanford.edu
    http://rescomp.stanford.edu/~ejalbert/
    Eric Guest

Similar Threads

  1. command line arguments
    By pop83in in forum Dreamweaver AppDev
    Replies: 0
    Last Post: March 28th, 07:54 AM
  2. Replies: 5
    Last Post: June 4th, 11:46 AM
  3. Any way to pass arguments to projectors from Windows command line?
    By matt_s webforumsuser@macromedia.com in forum Macromedia Director Lingo
    Replies: 5
    Last Post: November 30th, 10:57 PM
  4. command line arguments
    By Dan in forum PHP Development
    Replies: 3
    Last Post: October 22nd, 06:42 PM
  5. Regexp constructed from command line arguments.
    By Tad McClellan in forum PERL Miscellaneous
    Replies: 2
    Last Post: July 7th, 05:01 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139