display alert compilation error

Shane,

Success! I finally figured how to do this by the non-protocol method, and it turns out that it’s far simpler than either of us thought (in its final structure that is, not in how much fiddling it took to get there). You don’t need to put anything in the Obj-C files other than the 2 methods, and there’s no need to do anything in the nib. You just need to make the AS a subclass of the Obj-C class and make the alert ended method in the AS the same as the one in the Obj-C class. Just like the protocol method, I don’t understand why this works. Here is what worked:

//  ShowAlert.h
//  NewAlert
//
#import <Cocoa/Cocoa.h>

@interface ShowAlert : NSObject {}

-(void)runSheet:(NSAlert *)anAlert onWindow:(NSWindow*)aWindow;
-(void)alertEnded:(NSAlert *)alert withButton:(NSInteger)returnCode andContext:(void *)info;

@end
//  ShowAlert.m
//  NewAlert
//
#import "ShowAlert.h"

@implementation ShowAlert

-(void)runSheet:(NSAlert *)anAlert onWindow:(NSWindow*)aWindow

{ [anAlert beginSheetModalForWindow:aWindow modalDelegate:self didEndSelector:@selector(alertEnded:withButton:andContext:) contextInfo:nil];}

-(void)alertEnded:(NSAlert *)alert withButton:(NSInteger)returnCode andContext:(void *)info {}

@end

script NewAlertAppDelegate
	property parent : class "ShowAlert"
	property NSAlert : class "NSAlert"
	property button1 : missing value
	property myWindow : missing value
	
	on killProgram_(sender) --connected to button1
		current application's NSApp's terminate_(me)
	end killProgram_
	
	on alertEnded_withButton_andContext_(alert, returnCode, info)
		log {alert, returnCode, info}
	end alertEnded_withButton_andContext_
	
	on applicationWillFinishLaunching_(aNotification)
		set messageText to "This is a message"
		set infoText to "Click any button to close"
		set myalert to NSAlert's alertWithMessageText_defaultButton_alternateButton_otherButton_informativeTextWithFormat_(messageText, "OK", "Cancel", "done", infoText)
		runSheet_onWindow_(myalert, myWindow)
	end applicationWillFinishLaunching_
end script

The info argument of the alertEnded:withButton:andContext can be typed as (void *) as long as you pass in nil, but if you want to pass it something you can change that to (id). You can put statements inside the alertEnded:withButton:andContext: method, and they get executed as well as the ones inside the AS version of this method. I’m not sure why you need this method here even though its implementation is empty.

Neat!

You mean both get called without a continue statement? That’s a bit odd – the AS one should just override the superclass method.

I guess it’s just setting the method signature, like the protocol. Still baffling why it works with (void *) either way…

Shane,

I was wrong about that. Last night (at 1 AM) when I was doing this, I had a more complicated program including some of the stuff you said to do and some things I tried – after I got it to work, I started deleting things until I got down to the minimum required. I thought I saw both functions fire, but I could have been hallucinating by then.

I tried it again this morning, and you’re right – If I put statements in the Obj-C one, only the AS one executes. If I comment out the AS one, then the Obj-C one executes.

Well that’s a relief :slight_smile: