OK thanks Shane, if I understand you correctly, your saying that the code I’m putting into the main.scpt of the AS Editors template, should be put into the App delegate script instead, in the same way as I’m putting the identical code into the Xcode projects App Delegate, I’ll give that a try.
The idea started out as a script library for creating custom dialog boxes and windows, to be called from AS Editor scripts, with the idea of adding additional UI elements from the basic AS dialog boxes. rather than a specific application created with Xcode.
The code exmaple I tested is listed below, so to make it clearer what I was trying to achieve.
AS Editor Cocoa-Applescript Applet code.
use AppleScript version "2.3"
use scripting additions
use framework "Foundation"
use framework "AppKit"
property parent : class "NSObject"
property myWindow : missing value
property myButton : missing value
set myWindow to current application's NSWindow's alloc()'s initWithContentRect:(current application's NSMakeRect(0.0, 0.0, 360.0, 180.0)) ¬
styleMask:(15 as integer) ¬
backing:(current application's NSBackingStoreBuffered) ¬
defer:false
myWindow's setOneShot:true
myWindow's setTitle:("My New Window" as text)
myWindow's |center|()
myWindow's makeKeyAndOrderFront:me
delay (1.0)
set myButton to current application's NSButton's alloc()'s initWithFrame:(current application's NSMakeRect(250.0, 18.0, 90.0, 24.0))
myButton's setButtonType:(0 as integer)
myButton's setBezelStyle:(1 as integer)
myButton's setIgnoresMultiClick:true
myButton's setFont:(current application's NSFont's systemFontOfSize:(13.0 as real))
myButton's setKeyEquivalent:(current application's NSString's stringWithFormat:("\r" as text))
myButton's setTitle:("OK" as text)
myButton's setTarget:me
myButton's setAction:("myHandler:" as text)
tell myWindow's contentView() to addSubview:myButton
on myHandler:sender
tell myWindow to setTitle:("myHandler: called" as text)
end myHandler:
The myHandler causes the Applet to crash.
The Xcode project code, with identical code and no window xib or bindings.
script AppDelegate
property parent : class "NSObject"
property myWindow : missing value
property myButton : missing value
on applicationWillFinishLaunching:aNotification
set myWindow to current application's NSWindow's alloc()'s initWithContentRect:(current application's NSMakeRect(0.0, 0.0, 360.0, 180.0)) ¬
styleMask:(15 as integer) ¬
backing:(current application's NSBackingStoreBuffered) ¬
defer:false
myWindow's setOneShot:true
myWindow's setTitle:("My New Window" as text)
myWindow's |center|()
myWindow's makeKeyAndOrderFront:me
delay (1.0)
set myButton to current application's NSButton's alloc()'s initWithFrame:(current application's NSMakeRect(250.0, 18.0, 90.0, 24.0))
myButton's setButtonType:(0 as integer)
myButton's setBezelStyle:(1 as integer)
myButton's setIgnoresMultiClick:true
myButton's setFont:(current application's NSFont's systemFontOfSize:(13.0 as real))
myButton's setKeyEquivalent:(current application's NSString's stringWithFormat:("\r" as text))
myButton's setTitle:("OK" as text)
myButton's setTarget:me
myButton's setAction:("myHandler:" as text)
tell myWindow's contentView() to addSubview:myButton
end applicationWillFinishLaunching:
on myHandler:sender
tell myWindow to setTitle:("myHandler: called" as text)
end myHandler:
on applicationShouldTerminate:sender
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate:
end script
The myHandler works as expected.
Just could not work out why one worked, and one would not.
Regards Mark