Sunday, June 13, 2021

#1 2021-05-16 03:53:56 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 798

NSWindow, NSButton and NSWindowController

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.p … 84#p193384

You could run the script below directly in Script Editor.

It show you how to build button with actions.

Applescript:

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

Applescript:

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

Last edited by Fredrik71 (2021-05-20 02:03:47 pm)


if you are the expert, who will you call if its not your imagination.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)