ApplescriptObjC NSAlert and returnCode: At my wits end :(

Discussion in 'Mac Programming' started by mac.jedi, Oct 28, 2011.

  1. mac.jedi macrumors 6502

    Joined:
    Feb 1, 2008
    Location:
    The O.C.
    #1
    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!!!
     
  2. Partron22 macrumors 68000

    Partron22

    Joined:
    Apr 13, 2011
    Location:
    Yes
    #2
    This appears to be exactly the expected behavior as described on page 213 of Shane Stanley's excellent "AppleScript ObjC Explored 3.0.0"
     
  3. Red Menace macrumors 6502

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #3
    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
     
  4. mac.jedi thread starter macrumors 6502

    Joined:
    Feb 1, 2008
    Location:
    The O.C.
    #4
    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!!

    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 :)
     

Share This Page