Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mac.jedi

macrumors 6502
Original poster
Feb 1, 2008
355
3
The O.C.
Hi All!

I've been trying to wrap my head around how to convert my existing "display alert"'s to a sheet style NSAlert attached to the main window. I've found some helpful hints on the macscripter forum, but I'm stuck.

I have two issues:
1. Clicking the Default Button in the alert panel returns a EXC_BAD_ACCESS
2. None of the buttons seem to send a returnCode as an integer. I'd like to be able to do something different depending on which button was pressed. This all seems pretty straight forward, but I'm at a loss.

Here's the code I've been playing with:

Code:
    property parent : class "NSObject"
    property mainWindow : missing value
    property alertWindow : class "NSAlert"
    
    on showAlert_(sender)
        set theAlert to alertWindow's alertWithMessageText_defaultButton_alternateButton_otherButton_informativeTextWithFormat_("An alert", "OK", "Cancel", missing value, "Further explanation")
        theAlert's beginSheetModalForWindow_modalDelegate_didEndSelector_contextInfo_(mainWindow, me, "alertDidEnd:returnCode:contextInfo:", missing value)
    end showAlert_
    
    on alertDidEnd_returnCode_contextInfo_(theAlert, returnCode, contextInfo)
        log returnCode
    end alertDidEnd_returnCode_contextInfo_

Any help would be GREATLY appreciated. Please let me know if there's anymore info I can provide.

Thanks so much!!!
 

Partron22

macrumors 68030
Apr 13, 2011
2,655
808
Yes
1. Clicking the Default Button in the alert panel returns a EXC_BAD_ACCESS
This appears to be exactly the expected behavior as described on page 213 of Shane Stanley's excellent "AppleScript ObjC Explored 3.0.0"
You can set the value of contextInfo to whatever you wish when you display the sheet, and it is passed back in the callback when the sheet closes.
Unfortunately, the contextInfo parameter is defined as a void * class (essentially meaning a pointer to anything, object or not), and the bridge between AppleScript and Objective-C does not appreciate this — the result is that if you simply convert the Objective-C code to its AppleScriptObjC equivalent, you will get an exception when you dismiss a sheet: Program received signal: “EXC_BAD_ACCESS”.
There are a couple of ways around this, all of which involve using some Objective-C....
 

Red Menace

macrumors 6502a
May 29, 2011
578
226
Colorado, USA
You should probably be using the sheetDidEnd_returnCode_contextInfo_ method there...

There was a problem with ASObjC in Snow Leopard with void class parameters (the contextInfo part) that requires a little bit of Objective-C code - supposedly Lion handles them now, although I haven't had time to check.

Shane Stanley has written a collection of helper classes that includes alert sheets, so you might give that a look - http://www.macosxautomation.com/applescript/apps/helpers.html
 

mac.jedi

macrumors 6502
Original poster
Feb 1, 2008
355
3
The O.C.
This appears to be exactly the expected behavior as described on page 213 of Shane Stanley's excellent "AppleScript ObjC Explored 3.0.0"

Thanks Partron22! I bought the book after your recommendation and read the pages you described. I added the helpers to my test and it almost made me cry when it worked and started returning the button pressed. I've been eyeing that book for a long time and I'm glad you pushed me to pull the trigger! Thanks Again!!

You should probably be using the sheetDidEnd_returnCode_contextInfo_ method there...

There was a problem with ASObjC in Snow Leopard with void class parameters (the contextInfo part) that requires a little bit of Objective-C code - supposedly Lion handles them now, although I haven't had time to check.

Shane Stanley has written a collection of helper classes that includes alert sheets, so you might give that a look - http://www.macosxautomation.com/applescript/apps/helpers.html

Hi Red Menace!

Thanks for your help! The helper classes look great and the alert class worked perfectly! I just hope they don't make me too lazy :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.