<html><head></head><body bgcolor="#FFFFFF"><div>You may need to initialize the create command bits later in the app to avoid the open doc event getting swallowed up during startup...<br><br>Sent from my iPhone</div><div><br>On May 31, 2012, at 7:47 AM, "Michael O'Donnell" <<a href="mailto:micko@wagsoft.com">micko@wagsoft.com</a>> wrote:<br><br></div><div></div><blockquote type="cite"><div>Hi Kevin, Ronald,<div><br></div><div> I must be missing something. I have inserted the discussed</div><div>code into a test file (test.py) as follows:</div><div><br></div><div>----------------------</div><div><div>import sys</div>
<div>from Tkinter import *</div><div>import tkMessageBox</div><div><br></div><div>def doOpenFile(*args):</div><div> for arg in args:</div><div> tkMessageBox._show(message=str(arg), type="ok", icon=tkMessageBox.WARNING)</div>
<div><br></div><div>tk = Tk()</div><div>tk.createcommand("::tk::mac::OpenDocument", doOpenFile)</div><div>fr=Frame(tk, height=400, width=400, bg="red")</div><div>fr.pack()</div><div>tk.mainloop()</div>
<div>-----------------------</div><div><br></div><div>Then I package this using py2app, specifying that it should launch</div><div>whenever a .ctpx file is double clicked:</div><div><br></div><div>-------</div><div><div>from setuptools import setup</div>
<div>import sys</div><div>OPTIONS={'py2app': {'argv_emulation': True, 'resources': [],</div><div> 'plist': dict(CFBundleDocumentTypes = [dict(</div><div> CFBundleTypeExtensions = ["ctpx"],</div>
<div> CFBundleTypeName = "Test File",</div><div> CFBundleTypeRole = "Editor")])}}</div><div>sys.argv=[sys.argv[0]]</div><div>sys.argv.append("py2app")</div>
<div>setup(</div><div> app=['test.py'],</div><div> data_files=[],</div><div> options=OPTIONS,</div><div> setup_requires=['py2app'])</div></div><div>------</div><div><br></div><div>Running this creates an app: Test.app.</div>
<div><br></div><div>If I then click on a file with a .ctpx extension, the application</div><div>opens, but doOpenFile is not called (not message pops up).</div><div>If I click on the .ctpx file again (while the app is open) the</div>
<div>message DOES appear.</div><div><br></div><div>Now, this is all as I would expect, because my call of</div><div>tk.createcommand happens only AFTER my application</div><div>launches, and any openDocument event was probably</div>
<div>handled BEFORE the first line of my python code is</div><div>executed.</div><div><br></div><div>So none of this is any help to me. My users expect to</div><div>double click on a .ctpx document to launch the application</div>
<div>AND open that document for editing.</div><div><br></div><div>Have I missed something?</div><div><br></div><div>Mick</div><div><br></div><div><br></div><br><div class="gmail_quote">On Wed, May 30, 2012 at 8:29 PM, Kevin Walzer <span dir="ltr"><<a href="mailto:kw@codebykevin.com" target="_blank">kw@codebykevin.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im"><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
IDLE can be opened by double clicking on python files, the code I linked<br>
to should be responsible for implementing this. Also, the argv_emulation<br>
code uses the openDocument event to do its work (argv_emulation basicly<br>
runs a Carbon event loop until it has received some openDocument events<br>
or until a timeout occurs).<br>
</blockquote>
<br></div>
Also:<br>
<br>
*smacks head*<br>
<br>
Mick, I didn't realize that you were trying to have your app respond to a file being dropped on the app icon. Ronald is right, argv_emulation is not necessary for this.<br>
<br>
If the code sample Ronald pointed to isn't quite clear, let me explain it a bit more:<br>
<br>
Tk on the Mac supports all of the basic Apple events (app launch, file open, etc.) out of the box -- stub commands are included in the code that you can fill out to trigger an action in response to one of the supported events.<br>
<br>
In the case of opening a file by dropping it on the app icon, or double-clicking it, the relevant Tk command is "tk::mac::OpenDocument" . When this command is defined in Tk, the code contained in the command will be executed when an "odoc" ("open document") event is received.<br>
<br>
To access this functionality from Tkinter, you should use Tkinter's createcommand() function. The "createcommand" functionality allows a Python function/method to be mapped to a Tk command. Hence, in the IDLE example, you have:<br>
<br>
root.createcommand("::tk::mac:<u></u>:OpenDocument", doOpenFile)<br>
<br>
which maps the following code to the "tk::mac::OpenDocument" command:<br>
<br>
def doOpenFile(*args):<br>
for fn in args:<br>
flist.open(fn)<br>
<br>
<br>
Tk's support for this stuff on the Mac, out of the box, is actually quite rich, but it's been poorly documented until quite recently. I contributed documentation on all these commands to the Tk man pages:<br>
<br>
<a href="http://www.tcl.tk/man/tcl8.6/TkCmd/tk_mac.htm" target="_blank">http://www.tcl.tk/man/tcl8.6/<u></u>TkCmd/tk_mac.htm</a><br>
<br>
The commands are documented for Tk 8.6, but they are all present in the Cocoa-based version of Tk 8.5 from ActiveState as well, so you may find some useful things to look at here.<br>
<br>
Hope this helps,<br>
Kevin<div class="im HOEnZb"><br>
-- <br>
Kevin Walzer<br>
Code by Kevin<br>
<a href="http://www.codebykevin.com" target="_blank">http://www.codebykevin.com</a><br></div><div class="im HOEnZb">
______________________________<u></u>_________________<br>
Pythonmac-SIG maillist - <a href="mailto:Pythonmac-SIG@python.org" target="_blank">Pythonmac-SIG@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/pythonmac-sig" target="_blank">http://mail.python.org/<u></u>mailman/listinfo/pythonmac-sig</a><br>
unsubscribe: <a href="http://mail.python.org/mailman/options/Pythonmac-SIG" target="_blank">http://mail.python.org/<u></u>mailman/options/Pythonmac-SIG</a><br>
<br>
<br></div><div class="HOEnZb"><div class="h5">
______________________________<u></u>_________________________<br>
Unlimited Disk, Data Transfer, PHP/MySQL Domain Hosting<br>
<a href="http://www.doteasy.com" target="_blank">http://www.doteasy.com</a> </div></div></blockquote></div><br></div>
</div></blockquote></body></html>