Title: Re: Fwd: 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];
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/craigvaughan%40mac.com
This email sent to email@hidden
Chris:
This is fine for him *if* all his users are using 10.4, but I
think my suggestion to use AppleScript to encapsulate his app with his
default args like this:
do shell script "exec <path to his app>/hisapp -arg1
-arg2"
is a lot simpler. His users can modify this using the AppleScript
Editor that ships standard with the system and it works with any
version of OS X as far back as he cares to support.
- Craig
_______________________________________________
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