ASOC NSButton

This ASOC use AppKit and show a simple way to execute command by pressing a button from a dialog.

Maybe it will be useful to anyone… or to something more useful. :slight_smile:

setButtonType and setBezierStyle could be NSString or integer but not both.
if you like to try other types.

Enjoy.

use AppleScript version "2.4"
use framework "AppKit"
use scripting additions

property NSThread : a reference to current application's NSThread

-- A window that an app displays on the screen
property NSWindow : a reference to current application's NSWindow

property NSButton : a reference to current application's NSButton

-- The window renders all drawing into a display buffer and then flushes it to the screen
property NSBackingStoreBuffered : a reference to 2
(*
	Ref: https://developer.apple.com/documentation/appkit/nsbackingstoretype/nsbackingstorebuffered?language=objc
	It supports hardware acceleration, Quartz drawing, 
	and takes advantage of the GPU when possible. 
	It also supports alpha channel drawing, opacity controls, 
	using the compositor
*)

property theWindow : missing value

-- So we could use Script Editor.
if NSThread's isMainThread() as boolean then
	mainWindowUI()
else
	my performSelectorOnMainThread:"mainWindowUI" withObject:(missing value) waitUntilDone:true
end if


-- styleMask value integer: 7 without resize, 15 with resize.
-- mainWindowUI
-- NSWindow
on mainWindowUI()
	set theWindow to NSWindow's alloc()'s initWithContentRect:{{500, 400}, {400, 300}} styleMask:7 backing:NSBackingStoreBuffered defer:true
	set theWindow's releasedWhenClosed to true
	theWindow's setTitle:"Main Window UI"
	theWindow's contentView's addSubview:theButton("Button 1", 10, 270)
	theWindow's contentView's addSubview:theButton("Button 2", 10, 245)
	theWindow's contentView's addSubview:theButton("Button 3", 10, 220)
	theWindow's contentView's addSubview:theButton("Button 4", 10, 195)
	theWindow's contentView's addSubview:theButton("Button 5", 10, 170)
	tell theWindow to makeKeyAndOrderFront:me
end mainWindowUI

-- NSButton
on theButton(title, xPos, yPos)
	tell (NSButton's alloc()'s initWithFrame:{{xPos, yPos}, {50, 24}})
		-- its setButtonType:(current application's NSMomentaryChangeButton)
		-- ##############
		-- NSButtonType:
		-- ##############
		-- NSButtonTypeMomentaryLight
		-- its setButtonType:(current application's NSButtonTypeMomentaryLight)
		-- its setButtonType:0
		---------------------
		-- NSButtonTypePushOnPushOff
		-- its setButtonType:(current application's NSButtonTypePushOnPushOff)
		-- its setButtonType:1
		---------------------
		-- NSButtonTypeToggle
		-- its setButtonType:(current application's NSButtonTypeToggle)
		-- its setButtonType:2
		---------------------
		-- NSButtonTypeSwitch
		-- its setButtonType:(current application's NSButtonTypeSwitch)
		-- its setButtonType:3
		---------------------
		-- NSButtonTypeRadio
		-- its setButtonType:(current application's NSButtonTypeRadio)
		-- its setButtonType:4
		---------------------
		-- NSButtonTypeMomentaryChange
		-- its setButtonType:(current application's NSButtonTypeMomentaryChange)
		-- its setButtonType:5
		---------------------
		-- NSButtonTypeOnOff
		-- its setButtonType:(current application's NSButtonTypeOnOff)
		-- its setButtonType:6
		---------------------
		-- NSButtonTypeMomentaryPushIn
		-- its setButtonType:(current application's NSButtonTypeMomentaryPushIn)
		-- its setButtonType:7
		---------------------
		-- NSButtonTypeAccelerator
		-- its setButtonType:(current application's NSButtonTypeAccelerator)
		-- its setButtonType:8
		---------------------
		-- NSButtonTypeMultiLevelAccelerator
		-- its setButtonType:(current application's NSButtonTypeMultiLevelAccelerator)
		-- its setButtonType:9
		
		-- ##############
		-- NSBezelStyle
		-- ##############
		--NSBezelStyleRounded
		-- its setBezelStyle:(current application's NSBezelStyleRounded)
		-- its setBezelStyle:1
		---------------------
		--NSBezelStyleRegularSquare
		-- its setBezelStyle:(current application's NSBezelStyleRegularSquare)
		-- its setBezelStyle:2
		---------------------
		--NSBezelStyle
		-- its setBezelStyle:(current application's NSBezelStyle)
		-- its setBezelStyle:3
		---------------------
		--NSBezelStyle
		-- its setBezelStyle:(current application's NSBezelStyle)
		-- its setBezelStyle:4
		---------------------
		--NSBezelStyleDisclosure
		-- its setBezelStyle:(current application's NSBezelStyleDisclosure)
		-- its setBezelStyle:5
		---------------------
		--NSBezelStyleShadowlessSquare
		-- its setBezelStyle:(current application's NSBezelStyleShadowlessSquare)
		-- its setBezelStyle:6
		---------------------
		-- NSBezelStyleCircular
		-- its setBezelStyle:(current application's NSBezelStyleCircular)
		-- its setBezelStyle:7
		---------------------
		--NSBezelStyleTexturedSquare
		-- its setBezelStyle:(current application's NSBezelStyleTexturedSquare)
		-- its setBezelStyle:8
		---------------------
		--NSBezelStyleHelpButton
		-- its setBezelStyle:(current application's NSBezelStyleHelpButton)
		-- its setBezelStyle:9
		---------------------
		-- NSBezelStyleSmallSquare
		-- its setBezelStyle:(current application's NSBezelStyleSmallSquare)
		-- its setBezelStyle:10
		---------------------
		--NSBezelStyleTexturedRounded
		-- its setBezelStyle:(current application's NSBezelStyleTexturedRounded)
		-- its setBezelStyle:11
		---------------------
		--NSBezelStyleRoundRect
		-- its setBezelStyle:(current application's NSBezelStyleRoundRect)
		its setBezelStyle:12
		---------------------
		--NSBezelStyleRecessed
		-- its setBezelStyle:(current application's NSBezelStyleRecessed)
		-- its setBezelStyle:13
		---------------------
		--NSBezelStyleRoundedDisclosure
		-- its setBezelStyle:(current application's NSBezelStyleRoundedDisclosure)
		-- its setBezelStyle:14
		---------------------
		--NSBezelStyleInline
		-- its setBezelStyle:(current application's NSBezelStyleInline)
		-- its setBezelStyle:15
		---------------------
		-- https://developer.apple.com/documentation/appkit/nscell/1525857-refusesfirstresponder?language=objc
		its setRefusesFirstResponder:true
		
		-- https://developer.apple.com/documentation/appkit/nsbuttoncell/1535916-title?language=objc
		its setTitle:title
		-- https://developer.apple.com/documentation/appkit/nsfont/1525977-fontwithname?language=objc
		its setFont:(current application's NSFont's fontWithName:"Menlo" |size|:12)
		
		-- https://developer.apple.com/documentation/appkit/nstext/1533991-sizetofit
		its sizeToFit()
		
		--https://developer.apple.com/documentation/appkit/nsbuttoncell/1535382-alternatetitle?language=objc
		-- its setAlternateTitle:"Pressed"
		
		-- https://developer.apple.com/documentation/appkit/nsbuttoncell/1526922-attributedalternatetitle?language=objc
		-- its setAttributedTitle:(missing value)
		
		-- https://developer.apple.com/documentation/appkit/nscolorpanel/1524753-settarget?language=objc
		its setTarget:me
		
		-- https://developer.apple.com/documentation/appkit/nscolorpanel/1531244-setaction?language=objc
		its setAction:"buttonAction:"
		return it
	end tell
end theButton

-- Action
on buttonAction:sender
	set title to sender's title
	set buttonName to title as text
	set {{x, y}, {width, height}} to (sender's frame()) as list
	set x to x + (width div 2)
	set y to y + (height div 2)
	my messageDialog:buttonName
end buttonAction:

on messageDialog:theMessage
	if theMessage = "Button 1" then
		display dialog "This is button 1."
	else if theMessage = "Button 2" then
		display dialog "This is button 2."
	else if theMessage = "Button 3" then
		display dialog "This is button 3."
	else if theMessage = "Button 4" then
		display dialog "This is button 4."
	else
		display dialog "This is " & theMessage & "."
	end if
end messageDialog: