Here is updated version that also didn’t work in Mojave 10.14.6 in Script Editor.
The orginal code is here
https://www.macscripter.net/viewtopic.php?pid=193384#p193384
You could run the script below directly in Script Editor.
It show you how to build button with actions.
use AppleScript version "2.4"
use framework "AppKit"
use framework "Foundation"
use scripting additions
property windowController : missing value
on run
if current application's NSThread's isMainThread() as boolean then
my performDialog:me
else
my performSelectorOnMainThread:"performDialog:" withObject:me waitUntilDone:true
end if
end run
on performDialog:sender
set theWindow to createWindowWithRect(0, 0, 500, 500)
set windowController to createWindowController(theWindow)
set theButton to createButton("Click me", 20, 20, 100, 24, "buttonAction:")
theWindow's contentView()'s addSubview:theButton
windowController's |window|'s |center|()
windowController's |window|'s makeKeyAndOrderFront:me
end performDialog:
on buttonAction:sender
display dialog "I'm clicked"
end buttonAction:
on createButton(title, xMin, yMin, xLen, yLen, selector)
set buttonSize to current application's NSMakeRect(xMin, yMin, xLen, yLen)
set theButton to current application's NSButton's alloc()'s initWithFrame:buttonSize
theButton's setTitle:title
theButton's setBezelStyle:(current application's NSRoundedBezelStyle)
theButton's setAction:selector
theButton's setTarget:me
return theButton
end createButton
on createWindowWithRect(xMin, yMin, xLen, yLen)
set windowSize to current application's NSMakeRect(xMin, yMin, xLen, yLen)
set winStyle to (current application's NSWindowStyleMaskTitled) + (get current application's NSWindowStyleMaskClosable)
set theWindow to current application's NSWindow's alloc()'s initWithContentRect:windowSize styleMask:winStyle backing:2 defer:yes
return theWindow
end createWindowWithRect
on createWindowController(theWindow)
set theController to current application's NSWindowController's alloc()'s initWithWindow:theWindow
end createWindowController
In this example I have add NSMutableAttributedString to NSButton
use framework "Foundation"
use framework "AppKit"
use scripting additions
property arguments : missing value
on run
if current application's NSThread's isMainThread() as boolean then
my performDialog:arguments
else
my performSelectorOnMainThread:"performDialog:" withObject:arguments waitUntilDone:true
end if
end run
on performDialog:arguments
set {theWidth, theHeight} to {600, 300}
set theWindow to createWindowWithRect(0, 0, theWidth, theHeight)
set theButton to createButton("Click me", 20, 20, 100, 24, "buttonAction:")
theWindow's contentView()'s addSubview:theButton
theWindow's |center|()
theWindow's makeKeyAndOrderFront:me
end performDialog:
(**
* [Class] NSWindow
* A Window that an app displays on the screen.
**
* A single NSWindow object corresponds to at most one onscreen window.
* The two principal functions of a window are to provide an area in which
* views can be placed and to accept and distribute, to the appropriate views,
* events the user instigates through actions with the mouse and keyboard.
*)
on createWindowWithRect(xMin, yMin, xLen, yLen)
set windowSize to current application's NSMakeRect(xMin, yMin, xLen, yLen)
set winStyle to (current application's NSWindowStyleMaskTitled as integer) + (current application's NSWindowStyleMaskClosable as integer)
set theWindow to current application's NSWindow's alloc()'s initWithContentRect:windowSize styleMask:winStyle backing:2 defer:yes
return theWindow
end createWindowWithRect
on buttonAction:sender
display dialog "I'm clicked"
end buttonAction:
on createButton(title, xMin, yMin, xLen, yLen, selector)
set buttonSize to current application's NSMakeRect(xMin, yMin, xLen, yLen)
set theButton to current application's NSButton's alloc()'s initWithFrame:buttonSize
-- Set attributeString to the title of a button.
set buttonTitle to theButton's title
set attributeString to current application's NSMutableAttributedString's alloc()'s initWithString:buttonTitle
-- The color of the text.
attributeString's addAttribute:(current application's NSForegroundColorAttributeName) value:(current application's NSColor's yellowColor) range:{0, buttonTitle's |length|}
set theButton's attributedTitle to attributeString
--
theButton's setBezelStyle:(current application's NSRoundedBezelStyle)
theButton's setAction:selector
theButton's setTarget:me
return theButton
end createButton