Pull requests

#3 Merged
Repository
tito
Branch
default
Repository
pygame
Branch
default

Add support for MacOSX OpenFile. This was done in SDL, but not in the pygame implementation. Use a similar approach that SDL 1.3 does, using SDL_USEREVENT + USEREVENT_DROPFILE usercode + data1 for filename.

Author
  1. Mathieu Virbel
Reviewers
Description

On MacOSX, when you try to open a file with an application, the file is not appended to the argv as others platform. The NSApplication delegate have a method named OpenFile, that is called by macosx to open a file in your application.

SDL 1.2 have a trick to append the filename to argv only if the app is not started yet, otherwise, the openfile is refused. With python, sys.argv are already set, and pygame don't even use sdlmain. So using the same approach would just not work.

SDL 1.3 have implemented a new SDLDropEvent, with appropriate structure changes in SDL_Event.

So i'm proposing a similar approach for pygame, using SDL_UserEvent. The usercode is set to pygame.USEREVENT_DROPFILE, and the event have a "filename" attribute. You can now do in your main loop:

event = pygame.event.get_event() if event.type == pygame.USEREVENT and event.code == pygame.USEREVENT_DROPFILE: print 'MacOSX want to open', event.filename

  • Learn about pull requests

Comments (2)

  1. illume

    hey,

    this looks useful.

    I think it will be fine the way you've done it. Do you see any issues with the way you've done it? It's a pitty we can't make it modify sys.argv.

    There's a tiny fixup for the documentation """ On MacOSX, USEREVENT can have `code = pygame.USEREVENT_DROPFILE`. That means the user is trying to open a file with your application. The filename can be found at `event.filename` """