Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to pass cmd line arguments to my carbon app?



Jay is presenting a legitimate engineering problem with some interesting constraints.  Let's put philosophy aside and solve his problem.

He wants:

- API compatibility with an application main() that expects argc and argv.
  That's easy; Mac OS X sends every launchable app these parameters.

- Some way for the end user to set these as needed.
  Windows has this built in; Mac doesn't.

- Not require the user to go to the command line.
  That's the usual Mac OS X way to provide Unixisms in user-land.  He doesn't want this.

I suggest that he use the one thing about an app that a user can modify, that is, its Finder comments.  Luckily Spotlight in Mac OS X 10.4 gives us a very convenient API to get the value of whatever the user has set by choosing the app in the Finder, doing a Get Info, and typing a string into Comments.

So let's just make this the extra args field for his purposes.

Change main to appMain (to move it aside) and write a new main like this:

int main(int argc, char* argv[])
{
#ifdef TARGET_OS_MAC
    int  myArgCount;
    char *myArgVector[20];
    char *emptyArg = "";
    int     i;
    char argBuffer[256];

    

    myArgCount = 0;

    

    CFBundleRef myAppBundle = CFBundleGetMainBundle();
    CFURLRef    myAppURL = CFBundleCopyBundleURL(myAppBundle);
    CFStringRef myAppPath = CFURLCopyPath(myAppURL);
    MDItemRef myAppItem = MDItemCreate(kCFAllocatorSystemDefault, myAppPath);
    CFDictionaryRef attributeDict = MDItemCopyAttributeList(myAppItem, kMDItemFinderComment);
    CFStringRef argString = CFDictionaryGetValue(attributeDict, kMDItemFinderComment);
    CFArrayRef argArray = CFStringCreateArrayBySeparatingStrings(kCFAllocatorSystemDefault, argString, CFSTR(" "));
    myArgCount = CFArrayGetCount(argArray);
    for(i = 0; i < myArgCount; i++) {
        if (CFStringGetCString((CFStringRef)CFArrayGetValueAtIndex(argArray, i),argBuffer,sizeof(argBuffer),kCFStringEncodingASCII)){
            myArgVector[i] = malloc(strlen(argBuffer)+1);
            strcpy(myArgVector[i], argBuffer);
            }
        else 
            myArgVector[i] = emptyArg;
        }    
    return appMain(myArgCount, myArgVector);
#else
    return appMain(argc, argv);
#endif
}

This extracts the Finder comments, parses them into an arg list, and calls your old main with the argc and argv it expects.  It's #ifdefed so that when compiled on a non-Mac platform it's basically a one-stack-frame pass-through.  

Code tested and works, but cleanup and error-checking are left as an exercise for the reader.  Note that this does not do all the quoting and globbing that a shell would; you can go to town with that if you want.  It considers every space a delimiter; you can do more sophisticated scanning on the string if you choose (CFStringCreateArrayBySeparatingStrings looked convenient).

Mac OS X 10.4 only.

Enjoy,

Chris
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Carbon-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/carbon-dev/email@hidden

This email sent to email@hidden

References: 
 >Re: How to pass cmd line arguments to my carbon app? (From: Jay Vaughan <email@hidden>)
 >Re: How to pass cmd line arguments to my carbon app? (From: Chris Espinosa <email@hidden>)
 >Re: How to pass cmd line arguments to my carbon app? (From: Jay Vaughan <email@hidden>)
 >Re: How to pass cmd line arguments to my carbon app? (From: Eric Schlegel <email@hidden>)
 >Re: How to pass cmd line arguments to my carbon app? (From: Jay Vaughan <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.