text
stringlengths
0
15.7k
source
stringlengths
6
112
set theButton to (its addButtonWithTitle:aButton)
NSAlert.applescript
if icon is not missing value then -- set icon
NSAlert.applescript
set candidate to current application's NSImage's alloc's initByReferencingFile:icon
NSAlert.applescript
if (candidate's isValid as boolean) then -- file
NSAlert.applescript
its setIcon:candidate
NSAlert.applescript
else if icon is "critical" then -- critical style, otherwise informational
NSAlert.applescript
its setAlertStyle:(current application's NSCriticalAlertStyle)
NSAlert.applescript
if showing is true then -- show the alert?
NSAlert.applescript
return its runModal() as integer -- the button result (starts at 1000)
NSAlert.applescript
return it -- just the NSAlert object
NSAlert.applescript
end makeAlert
NSAlert.applescript
script AlertController
NSAlert.applescript
property cancelButton : "Cancel" -- the cancel button name
NSAlert.applescript
property alert : missing value -- this will be the alert object
NSAlert.applescript
property giveUpTime : missing value -- this will be the give up time (in seconds)
NSAlert.applescript
property buttonList : missing value -- this will be a list of the button names
NSAlert.applescript
property buttonPressed : missing value -- this will be the name of the button pressed
NSAlert.applescript
to performAlert for messageText given messageFont:messageFont : missing value, messageColor:messageColor : missing value, infoText:infoText : "", infoFont:infoFont : missing value, infoColor:infoColor : missing value, buttons:buttons : {"OK"}, icon:icon : missing value, accessory:accessory : missing value, givingUpAfter:givingUpAfter : missing value
NSAlert.applescript
set my alert to current application's NSAlert's alloc's init()
NSAlert.applescript
alert's |window|'s setAutorecalculatesKeyViewLoop:true -- hook any added views into the key-view loop
NSAlert.applescript
alert's setMessageText:adjustFonts(messageText, {messageFont, messageColor, infoFont, infoColor})
NSAlert.applescript
if infoText is not missing value then alert's setInformativeText:(infoText as text)
NSAlert.applescript
if givingUpAfter is not in {0, missing value} then set my giveUpTime to givingUpAfter
NSAlert.applescript
set my buttonList to setButtons(buttons) -- use updated button names (destructive action, etc)
NSAlert.applescript
if icon is not missing value then setIcon(icon)
NSAlert.applescript
if accessory is not missing value then alert's setAccessoryView:accessory
NSAlert.applescript
showAlert()
NSAlert.applescript
end performAlert
NSAlert.applescript
to showAlert()
NSAlert.applescript
set button to getButtonPress(setupTimer(giveUpTime)) -- do it
NSAlert.applescript
set button to item button of buttonList -- index using button number
NSAlert.applescript
set my buttonPressed to button
NSAlert.applescript
to getButtonPress(timer)
NSAlert.applescript
set button to (alert's runModal() as integer) - 999 -- first button returns 1000
NSAlert.applescript
set {timer, timerField, countdown} to {missing value, missing value, missing value}
NSAlert.applescript
end getButtonPress
NSAlert.applescript
to adjustFonts(messageText, fontInfo)
NSAlert.applescript
set {messageFont, messageColor, infoFont, infoColor} to fontInfo
NSAlert.applescript
tell alert's |window|'s contentView's subviews's item 5
NSAlert.applescript
if messageText is in {missing value, ""} then -- can't be nil, so just make it really small
NSAlert.applescript
set messageText to ""
NSAlert.applescript
its setFont:(current application's NSFont's systemFontOfSize:0.25)
NSAlert.applescript
if messageFont is not missing value then its setFont:messageFont
NSAlert.applescript
if messageColor is not missing value then its setTextColor:messageColor
NSAlert.applescript
tell alert's |window|'s contentView's subviews's item 6
NSAlert.applescript
if infoFont is not missing value then its setFont:infoFont
NSAlert.applescript
if infoColor is not missing value then its setTextColor:infoColor
NSAlert.applescript
return messageText as text
NSAlert.applescript
end adjustFonts
NSAlert.applescript
to setButtons(buttons)
NSAlert.applescript
set buttonNames to {}
NSAlert.applescript
set buttons to (current application's NSOrderedSet's orderedSetWithArray:(buttons as list))'s allObjects() as list -- remove duplicates
NSAlert.applescript
set aButton to aButton as text
NSAlert.applescript
set destructive to (aButton ends with return)
NSAlert.applescript
if destructive then set aButton to text 1 thru -2 of aButton
NSAlert.applescript
if aButton is not in {"", "missing value"} then -- skip missing titles
NSAlert.applescript
set end of buttonNames to aButton
NSAlert.applescript
set theButton to (alert's addButtonWithTitle:aButton)
NSAlert.applescript
if destructive and (theButton's respondsToSelector:"hasDestructiveAction") then (theButton's setHasDestructiveAction:true)
NSAlert.applescript
if buttonNames is {} then -- make sure there is at least one
NSAlert.applescript
set end of buttonNames to okButton
NSAlert.applescript
return buttonNames
NSAlert.applescript
end setButtons
NSAlert.applescript
to setIcon(icon)
NSAlert.applescript
if icon is missing value then return
NSAlert.applescript
if icon is "critical" then
NSAlert.applescript
else if icon is in {"informational", "warning"} then
NSAlert.applescript
else -- from a file
NSAlert.applescript
set iconImage to current application's NSImage's alloc's initByReferencingFile:(icon as text)
NSAlert.applescript
if (iconImage is not missing value) and (iconImage's isValid as boolean) then set alert's icon to iconImage
NSAlert.applescript
end setIcon
NSAlert.applescript
to setupTimer(giveUpTime)
NSAlert.applescript
set my timerField to current application's NSTextField's alloc's initWithFrame:{{0, 0}, {40, 20}}
NSAlert.applescript
timerField's setFont:(current application's NSFont's fontWithName:"Menlo Bold" |size|:14)
NSAlert.applescript
timerField's setAlignment:(current application's NSCenterTextAlignment)
NSAlert.applescript
positionTimerField(timerField)
NSAlert.applescript
end setupTimer
NSAlert.applescript
to positionTimerField(timerField)
NSAlert.applescript
alert's layout() -- get current layout
NSAlert.applescript
end positionTimerField
NSAlert.applescript
set {timer, my timerField, my countdown} to {missing value, missing value, missing value}
NSAlert.applescript
property response : missing value -- performSelectorOnMainThread doesn't return anything
NSAlert.applescript
to doStuff() -- UI stuff needs to be done on the main thread
NSAlert.applescript
set theButtons to {"One", "Two", "Cancel"}
NSAlert.applescript
set theAlert to (makeAlert for "Simple Alert" given infoText:"whatever", icon:"/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns", buttons:theButtons)
NSAlert.applescript
theAlert's setAccessoryView:(current application's NSView's alloc's ¬
NSAlert.applescript
initWithFrame:(current application's NSMakeRect(0, 0, 800, 0))) -- make wider
NSAlert.applescript
theAlert's |window|'s orderFront:me -- show it
NSAlert.applescript
theAlert's |window|'s setFrameOrigin:{0, 300} -- move it
NSAlert.applescript
set indx to (theAlert's runModal() as integer) - 999 -- run it (the first button is 1000)
NSAlert.applescript
if item indx of theButtons is AlertController's cancelButton then error number -128 -- manually cancel
NSAlert.applescript
set theButtons to {"OK", "Test" & return, missing value, "One", "One", "One"}
NSAlert.applescript
set accessory to makeButtonGroup at {0, 0} with radio given boxWidth:350, itemList:{"foo", "bar 0123456789012345678901234567890" & return, "baz"} -- example
NSAlert.applescript
AlertController's (performAlert for "Alert" given infoText:loremText, infoColor:(current application's NSColor's orangeColor), buttons:theButtons, accessory:accessory, givingUpAfter:10)
NSAlert.applescript
set response to {AlertController's buttonPressed, accessoryValues(accessory)} -- whatever
NSAlert.applescript
tell AlertController's alert -- alter the script's NSAlert object
NSAlert.applescript
its setMessageText:"AlertController's alert can be altered and run again" -- use API
NSAlert.applescript
its setInformativeText:""
NSAlert.applescript
AlertController's showAlert() -- use script to show it again and update button pressed
NSAlert.applescript
log response -- whatever
NSAlert.applescript