MacOSX: Getting docs when launched from the Finder

I have an SDL app on MacOS X that needs to accept files that are
double-clicked in the Finder, or dragged onto the app’s icon. Since this
app isn’t originally a Mac app, it doesn’t have a concept of Apple
events (or events at all, outside of SDL_PollEvents).

The simplest way I could find to do this is to catch the Finder’s
requests in the Cocoa stub that starts a MacOSX SDL app, and rewrite the
command line that is passed to the application’s main(). This makes the
changes minimal (20 lines of Cocoa, basically), avoids me having to muck
with Carbon or Cocoa in the app itself, and lets me use the same app
code (parsing argc/argv) that I’d use on Linux.

However, I have no idea if this is a “good idea”, so I’ve attached the
patch here. Can someone who actually knows anything about MacOS tell me
if this is a fatally stupid idea before I commit it to CVS?

Thanks,
–ryan.

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed…
Name: SDL-Mac-Finder-Launch-Argv-RYAN-1.diff
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20050616/00fe7ca0/attachment.asc

Ryan C. Gordon wrote:

I have an SDL app on MacOS X that needs to accept files that are
double-clicked in the Finder, or dragged onto the app’s icon. Since this
app isn’t originally a Mac app, it doesn’t have a concept of Apple
events (or events at all, outside of SDL_PollEvents).

The simplest way I could find to do this is to catch the Finder’s
requests in the Cocoa stub that starts a MacOSX SDL app, and rewrite the
command line that is passed to the application’s main(). This makes the
changes minimal (20 lines of Cocoa, basically), avoids me having to muck
with Carbon or Cocoa in the app itself, and lets me use the same app
code (parsing argc/argv) that I’d use on Linux.

However, I have no idea if this is a “good idea”, so I’ve attached the
patch here. Can someone who actually knows anything about MacOS tell me
if this is a fatally stupid idea before I commit it to CVS?

Looks sane to me. The way I did it when I needed that functionality in
one of my apps was implementing -application:openFile: in a category of
SDLMain and have it copy the received file name to a global variable
that is later checked in main() (in addition to argv). But your way
actually looks more elegant - and has the added benefit of also working
if multiple files are opened simultaneously. Having it in SDL proper
would allow me to remove some code from my app, which is always welcome.

Be sure to test it both with and without a NIB file. I currently see no
reason for it to behave differently in these two cases, but just in case…

Minor nitpick: in Cocoa, usually YES and NO are used as boolean
constants. But since TRUE and FALSE are defined in CoreFoundation (at
least here on 10.3.9), they should work as well.

I wonder, are there any other OSes (supported by SDL) than Mac OS and
Mac OS X that have the concept of “(the user) opening a file”, but don’t
pass the names of opened files in argv?

-Christian

The simplest way I could find to do this is to catch the Finder’s
requests in the Cocoa stub that starts a MacOSX SDL app, and rewrite the
command line that is passed to the application’s main(). This makes the
changes minimal (20 lines of Cocoa, basically), avoids me having to muck
with Carbon or Cocoa in the app itself, and lets me use the same app
code (parsing argc/argv) that I’d use on Linux.

(This is in CVS now.)

–ryan.