NSAlert and returnCode: At my wits end :(

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


	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!!!

Do you have Shane Stanley’s book on ASOC? He also includes code that can run an alert sheet, and I know it works, I’ve used it. You just plug in 2 Cocoa class files and then use the ASOC code.
It’s a great e-book (PDF) and very helpful code. It’s about $35 I think.

Hi SuperMacGuy!

Thanks for replying to my post! I’ve been eyeing Shane Stanley’s book on macosxautomation.com. You’ve sold me, I’m buying :slight_smile:

I’m sure I’ll learn a ton from the book, but I’d still like to know what I’m doing wrong with the code I posted. I like to learn from my mistakes :wink: Am I not initializing it correctly? Any insight would be great!

It’s not so much what you’re doing as a bug. The problem is with the contextInfo: argument, which is a void * type, and AppleScriptObjC doesn’t cope with such things. I believe the problem was fixed in Lion, but I haven’t tried it yet myself.

You should probably also makes the alert differently. From the documentation on alertWithMessageText:defaultButton:alternateButton:otherButton:informativeTextWithFormat:

This is a compatibility method.[ …] Unless you must maintain compatibility with existing alert-processing code that uses the function-based API, you should allocate (alloc) and initialize (init) the object, and then set its attributes using the appropriate methods of the NSAlert class.

I have tried it in Lion, and it still doesn’t work properly. I can get the beginSheetModalForWindow_modalDelegate_didEndSelector_contextInfo_ method to work but only if I use a selector that has only one argument, the alert itself, so no way to get the return code.

I did get it to sort of work if you create the alert with an alloc init (and do nothing else), and then I can pass a number or an NSString as the context info, but the return code is null. If I add buttons with addButtonWithTile, then I get the dreaded “EXC_BAD_ACCESS” error message.

Ric

Bummer.

Thank you all so much!

Shane Stanley! AppleScriptObjC Explored and the examples you provided have been super-helpful!! You’ve done an AMAZING job :slight_smile:

Thanks again!!!

I’ve got another question regarding NSOutlineView. I’ll post my question in another thread, but perhaps one of you will be kind enough to check it out and help me out again?