I’m writing a script using Shane Stanley’s Myriad Tables Lib, and I’d like to add a slider to the script. Having never created a slider, and being pretty new to ASOC in general, I tried to buy Shane’s Everyday AppleScriptObjC, but the link to buy the book isn’t working. So I scoured the web on for an example on how to implement a slider using ASOC, and found this:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
property response : missing value -- result from the alert
on run -- UI items need to be run on the main thread
if current application's NSThread's isMainThread() as boolean then
doStuff()
else
my performSelectorOnMainThread:"doStuff" withObject:(missing value) waitUntilDone:true
end if
end run
to doStuff()
try
showAlert given info:"This is an example using a slider.", buttons:{"OK", "Whatever"}
-- do stuff with the response
log response
on error errmess
display alert "Error with doing stuff" message errmess
end try
end doStuff
to showAlert given message:message : "Alert", info:info : "", buttons:buttons : {"OK"} -- reverse order
set accessory to makeSlider of {275, 26} given initial:0, minimum:0, maximum:10
tell current application's NSAlert's alloc's init()
set its |window|'s autorecalculatesKeyViewLoop to true -- hook added views into the key-view loop
# cheats to 'hide' the alert's bold message text field, and increase the size
# of the informative text field (comment or remove for normal operation):
(its |window|'s contentView's subviews's item 5)'s setFont:(current application's NSFont's boldSystemFontOfSize:0.25) -- something small
(its |window|'s contentView's subviews's item 6)'s setFont:(current application's NSFont's systemFontOfSize:13) -- something bigger
its setMessageText:message
its setInformativeText:info
repeat with aButton in buttons
(its addButtonWithTitle:aButton)
end repeat
set its accessoryView to accessory
set theButton to item ((its runModal() as integer) - 999) of buttons -- rightmost button returns 1000
set response to {button:theButton, slider:(accessory's intValue) as integer}
end tell
end showAlert
to makeSlider of |size| given origin:origin : {0, 0}, initial:initial : 0.0, minimum:minimum, maximum:maximum, action:action : missing value
tell (current application's NSSlider's sliderWithValue:initial minValue:minimum maxValue:maximum target:me action:action)
its setFrame:{origin, |size|}
its setNumberOfTickMarks:(maximum + 1)
set its allowsTickMarkValuesOnly to true
# set other properties as desired
return it
end tell
end makeSlider
from here.
The script works well, but is a bit of a work-around. I have 2 questions:
- Is there a less kludgy way of implementing a slider?
- How do I then link the NSSlider instance to an NSTextField instance to display the value of the slider? An example would be greatly appreciated - again, I’m pretty new to this and I’m not familiar with most of the terminology and processes. Thanks so much.