[wxPython-users] Accepting files for Drag and drop on OS-X

Kevin Ollivier kevino at theolliviers.com
Thu Mar 8 11:35:04 PST 2007


Hi Chris,

On Mar 8, 2007, at 10:58 AM, Christopher Barker wrote:

> HI all,
>
> I'm working on making an app accept files for drag and drop on OS- 
> X. I've started with what's on this page:
>
> http://wiki.wxpython.org/index.cgi/Optimizing_for_Mac_OS_X
>
> Which got me most of the way there. However, while I can now drag a  
> file onto the icon in the doc when the app is running, and have it  
> call MacOpenFile(), if I start the app by dropping a file on it, it  
> doesn't seem to register it at all, it just starts without ever  
> calling MacOpenFile().
>
> How can I get it to start up with the file dropped on it?

I'm surprised there isn't any info about this on the wiki page.  
Basically, your app won't start receiving AppleEvents until it's  
fully started, so you won't get a MacOpenFile notification. However,  
if you drag a file onto an app to start it, you will receive the  
filename of the dragged file in the app's command line arguments, so  
you need to parse sys.argv, pull out the filename, and load it that way.

Regards,

Kevin

> Enclosed is a small sample, that should accept all files for  
> dropping, once it's been bundled with py2app.
>
> Tested with:
> Python2.5
> wxPython 2.8.1
> py2app from SVN
>
> I'll try to update the wiki some once I've got this all working.
>
> -Chris
>
>
> -- 
> Christopher Barker, Ph.D.
> Oceanographer
>
> Emergency Response Division
> NOAA/NOS/OR&R            (206) 526-6959   voice
> 7600 Sand Point Way NE   (206) 526-6329   fax
> Seattle, WA  98115       (206) 526-6317   main reception
>
> Chris.Barker at noaa.gov
> <ConverterIcon.icns>
> """
> This is a setup.py script generated by py2applet
>
> Usage:
>     python setup.py py2app
> """
>
> from setuptools import setup
>
> # A custom plist for letting it associate with all files.
> Plist = dict(CFBundleDocumentTypes= [dict(CFBundleTypeExtensions= 
> ["*"],
>                                            
> #CFBundleTypeName="kUTTypeText",
>                                           CFBundleTypeRole="Editor"),
>                                     ]
>              )
>
>
> APP = ['MacApp.py']
> DATA_FILES = []
> OPTIONS = {'argv_emulation': True,
>            'iconfile': '/Users/cbarker/PythonStuff/Py2AppTest/ 
> wxPython/ConverterIcon.icns',
>            'plist': Plist,
>            }
>
>
> setup(
>     app=APP,
>     data_files=DATA_FILES,
>     options={'py2app': OPTIONS},
>     setup_requires=['py2app'],
> )
>
> # plist entry for making app accept text (all?) files
> # <key>CFBundleDocumentTypes</key>
> #       <array>
> #             <dict>
> #                   <key>CFBundleTypeExtensions</key>
> #                       <array>
> #                         <string>*</string>
> #                      </array>
> #                    <key>CFBundleTypeName</key>
> #                        <string>kUTTypeText</string>
> #                    <key>CFBundleTypeRole</key>
> #                        <string>Editor</string>
> #             </dict>
> #       </array>
> #
> #!/usr/bin/env python2.5
>
> """
> This is a small, simple app that tried to do all the right things on
> OS-X -- putting standard menus is the right place, etc. It should work
> just fine on other platforms as well : that's the beauty of wx!
> """
>
> import wx
>
> class DemoFrame(wx.Frame):
>     """ This window displays a button """
>     def __init__(self, title = "Micro App"):
>         wx.Frame.__init__(self, None , -1, title)
>
>         MenuBar = wx.MenuBar()
>
>         FileMenu = wx.Menu()
>
>         item = FileMenu.Append(wx.ID_EXIT, text = "&Exit")
>         self.Bind(wx.EVT_MENU, self.OnQuit, item)
>
>         item = FileMenu.Append(wx.ID_ANY, text = "&Open")
>         self.Bind(wx.EVT_MENU, self.OnOpen, item)
>
>         item = FileMenu.Append(wx.ID_PREFERENCES, text =  
> "&Preferences")
>         self.Bind(wx.EVT_MENU, self.OnPrefs, item)
>
>         MenuBar.Append(FileMenu, "&File")
>
>         HelpMenu = wx.Menu()
>
>         item = HelpMenu.Append(wx.ID_HELP, "Test &Help",
>                                 "Help for this simple test")
>         self.Bind(wx.EVT_MENU, self.OnHelp, item)
>
>         ## this gets put in the App menu on OS-X
>         item = HelpMenu.Append(wx.ID_ABOUT, "&About",
>                                 "More information About this program")
>         self.Bind(wx.EVT_MENU, self.OnAbout, item)
>         MenuBar.Append(HelpMenu, "&Help")
>
>         self.SetMenuBar(MenuBar)
>
>         btn = wx.Button(self, label = "Quit")
>
>         btn.Bind(wx.EVT_BUTTON, self.OnQuit )
>
>         self.Bind(wx.EVT_CLOSE, self.OnQuit)
>
>
>     def OnQuit(self,Event):
>         self.Destroy()
>
>     def OnAbout(self, event):
>         dlg = wx.MessageDialog(self, "This is a small program to  
> test\n"
>                                      "the use of menus on Mac, etc. 
> \n",
>                                 "About Me", wx.OK |  
> wx.ICON_INFORMATION)
>         dlg.ShowModal()
>         dlg.Destroy()
>
>     def OnHelp(self, event):
>         dlg = wx.MessageDialog(self, "This would be help\n"
>                                      "If there was any\n",
>                                 "Test Help", wx.OK |  
> wx.ICON_INFORMATION)
>         dlg.ShowModal()
>         dlg.Destroy()
>
>     def OnOpen(self, event):
>         dlg = wx.MessageDialog(self, "This would be an open Dialog\n"
>                                      "If there was anything to open 
> \n",
>                                 "Open File", wx.OK |  
> wx.ICON_INFORMATION)
>         dlg.ShowModal()
>         dlg.Destroy()
>
>     def OnPrefs(self, event):
>         dlg = wx.MessageDialog(self, "This would be an preferences  
> Dialog\n"
>                                      "If there were any preferences  
> to set.\n",
>                                 "Preferences", wx.OK |  
> wx.ICON_INFORMATION)
>         dlg.ShowModal()
>         dlg.Destroy()
> class MyApp(wx.App):
>     def OnInit(self):
>         frame = DemoFrame()
>         frame.Show()
>
>         return True
>
>     def MacOpenFile(self, filename):
>         print filename
>         print "%s dropped on app"%(filename) #code to load filename  
> goes here.
>         dlg = wx.MessageDialog(None,
>                                "A file was just dropped on this app: 
> \n%s\n"%filename,
>                                "File Dropped",
>                                wx.OK|wx.ICON_INFORMATION)
>         dlg.ShowModal()
>         dlg.Destroy()
>
>
> app = MyApp(False)
> app.MainLoop()
>
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: wxPython-users-unsubscribe at lists.wxwidgets.org
> For additional commands, e-mail: wxPython-users- 
> help at lists.wxwidgets.org





More information about the wxpython-users mailing list