Sunday, June 13, 2021

#1 2021-06-08 03:05:12 pm

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

NSAlert example

This script is a NSAlert that get a different look. In my learning to build UI and understand it better.
To get the return button name in app-model environment is not always straightforward. But I find a solution here I use reverse and theArray's reverseObjectEnumerator()'s allObjects() method.

This is a early test script to understand what is possible. Could be running directly in Script Editor.

UPDATED: 2021-06-09 added accessoryView, reference:
https://developer.apple.com/documentati … guage=objc

Applescript:

use framework "Foundation"
use framework "AppKit"
use scripting additions

property cancelButton : "Cancel"
property returnCode : missing value

on run
   set theAlert to showAlert given arguments:{title:"Title of Alert", message:"My message...", info:"My info...", accessory:"My accessory message", buttons:{"Cancel", "OK"}}
   -- return theAlert's button returned
end run

on showAlert given arguments:arguments
   try
       if my NSThread's isMainThread() as boolean then
           -- if we run in main thread set the arguments to dictionary.
           set arguments to current application's NSDictionary's dictionaryWithDictionary:arguments
           my performAlert:arguments
       else
           my performSelectorOnMainThread:"performAlert:" withObject:arguments waitUntilDone:true
       end if
       
       if button returned of returnCode is cancelButton then error number -128
       return returnCode
   on error error_message number error_number
       -- tell me to activate
       display alert "Error number: " & error_number & "" message error_message
       return returnCode
   end try
end showAlert

on performAlert:arguments
   -- log (my NSThread's isMainThread()) as boolean
   
   set theAlert to createAlert(arguments's title, arguments's message, arguments's info, arguments's accessory, arguments's buttons)
   
   set button to theAlert's runModal()
   set buttonsArray to (buttons of arguments)'s reverseObjectEnumerator()'s allObjects()
   set button to item (button mod 1000 + 1) of buttonsArray
   set my returnCode to {button returned:button as text}
end performAlert:

on createAlert(title, message, info, accessoryString, buttons)
   set theAlert to current application's NSAlert's alloc()'s init()
   theAlert's |window|'s setTitle:title
   theAlert's setIcon:(my createImageWithName(current application's NSImageNameActionTemplate))
   theAlert's setMessageText:message
   theAlert's setInformativeText:info
   theAlert's setAccessoryView:(my createTextViewWithRect(accessoryString, 12, 0, 0, 200, 15))
   
   theAlert's |window|'s setStyleMask:(current application's NSWindowStyleMaskBorderless)
   log (theAlert's |window|)'s |description|() as text -- NSPanel
   
   repeat with anItem in reverse of (buttons as list)
       (theAlert's addButtonWithTitle:anItem)
   end repeat
   return theAlert
end createAlert

on createTextViewWithRect(accessoryString, fontSize, x, y, width, height)
   set textViewSize to current application's NSMakeRect(x, y, width, height)
   set theAccessory to current application's NSTextView's alloc()'s initWithFrame:textViewSize
   set theFont to current application's NSFont's systemFontOfSize:fontSize
   set textAttributes to current application's NSDictionary's dictionaryWithObject:theFont forKey:(current application's NSFontAttributeName)
   theAccessory's insertText:(current application's NSAttributedString's alloc()'s initWithString:accessoryString attributes:textAttributes)
   theAccessory's setEditable:false
   theAccessory's setDrawsBackground:true
   return theAccessory
end createTextViewWithRect

on createImageWithName(theImageName)
   set theImage to current application's NSImage's imageNamed:theImageName
end createImageWithName

Last edited by Fredrik71 (2021-06-09 08:30:31 am)


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)