[Pythonmac-SIG] py2app bug with argv_emulation=True

Kevin Walzer kw at codebykevin.com
Sun Jun 3 04:30:45 CEST 2012


On 6/2/12 9:24 PM, Michael O'Donnell wrote:
> Kevin,
>
>    Seems we are not understanding each other. When a user
> double clicks on a document associated to the app, the app
> is launched. With argv emulation, I could read sys.argv to find
> which document was clicked on.
>
> With openDocument events, the double clicking on a document
> opens the application. The openDocument event is consumed
> (but at this point, there is only dummy code for this). Then my
> code is executed, overwriting the openDocument event handler.
>
> So, with this approach, there is no way to retrieve WHICH document
> was clicked on to open the application.
>
> Mick
>

Mick,

(ccing back to the list as well)

This brute-force method works for me--I double-click a "ctpx" file and 
the test app launches and then displays a messagebox with the name of 
the launched file:

----
import sys
import time
from Tkinter import *
import tkMessageBox

def doOpenFile(*args):
     for arg in args:
         tkMessageBox._show(message=str(arg), type="ok", 
icon=tkMessageBox.WARNING)

tk = Tk()
fr=Frame(tk, height=400, width=400, bg="red")
fr.pack()
time.sleep(2)
tk.createcommand("::tk::mac::OpenDocument", doOpenFile)
for idx, value in enumerate(sys.argv):
     print idx, value
doOpenFile(sys.argv[1])
tk.mainloop()
----

I'm not sure if this works because you set argv_emulation in py2app's 
options, since I don't structure my apps this way, and obviously the 
hard command of "doOpenFile(sys.argv[1])" needs to be wrapped in some 
sort of try statement to guard against the app being launched without a 
file argument, but clearly what you want to do can be done. The opendoc 
event is still useful for dragging the file to the app icon in the Dock, 
regardless.

--Kevin

-- 
Kevin Walzer
Code by Kevin
http://www.codebykevin.com


More information about the Pythonmac-SIG mailing list