tags:

views:

62

answers:

2

Is there an easy way to get any more control over the py2app traceback dialogs, or just a nice way to display GUI messages?

If I raise an exception in my py2app script, I get a dialog that says something like this:

MyAppName Error

MyAppName Error

An unexpected error has occurred during execution of the main script

MyRaisedError: This is the text that I can control when I raise the error.

It has Open Console and Terminate buttons. My script needs to check if a certain DVD is in the drive, if it's not, I want to show an error dialog and quit. I would like to have more control over it than this, as I can only change some of the text and can't control the buttons.

I tried calling osascript to do 'display dialog' via applescript, but it gave me an error like this: 0:19: execution error: No user interaction allowed. (-1713) I don't particularly like this way of doing it, but if it's all I can do...

I would really prefer not to include a big project like Cocoa Dialogs or something like a PyObjC project... the script itself is very tiny and I can't see adding 10x the meat of my script just to get the dialog.

+1  A: 

Instead of using osascript, you can call display dialog via py-appscript which, if you don't already have it in your python site-library, can be installed via easy_install. This example works inside of a py2app-generated app:

#!/usr/bin/env python
from osax import *
import py2app

def doit():
    sa = OSAX()
    sa.display_dialog("Python says hello!",
            buttons=["Hi!", "Howdy!", "Duuuude!"],
            default_button=3)

if __name__ == '__main__':
    doit()
Ned Deily
What's the best way to include that in the py2app bundle? I don't want people using my app to have to install it.
zekel
By importing it in your script, py2app should automatically include in your app bundle. You don't have to do anything special.
Ned Deily
To clarify though, py2app won't necessarily do that if you are using the default Apple-supplied python. For a stand-alone app that you are going to distribute and that you might want to work on multiple levels of OS X, you probably want to be using a python.org python.
Ned Deily
This is only going to run on 10.6 (really!) The way it automatically bundles in imported stuff is really creepy, but awesome. I can't find osax files anywhere in the built package...
zekel
A: 

Just put a standard Python try/catch block around the section of code that throws the exception, then use NSAlert to tell your users they need to put the DVD in

RyanWilcox
Where does NSAlert come from?
zekel