有没有办法在 Applescript 中添加滑块?
Is there a way to have a slider in Applescript?
所以我想知道是否有办法在 AppleScript 中添加滑块。我发现 this Apple Community post 讨论如何从某些应用程序读取滑块,但我想显示一个具有特定范围的滑块。所以我希望它看起来像这样:
set theSlider to slider from 1 to 10 with prompt "Set your number"
我认为它不存在,但我想确定一下,因为它会不时派上用场。
如评论中所述,常规 AppleScript 没有类似的东西,但是 Cocoa 框架可以与一些 AppleScriptObjC.
NSAlert class 是用于 display alert
的,但是因为你无法进入 StandardAdditions 对于自定义,您需要自己构建警报。在此示例中,我正在创建一个 NSAlert(稍微隐藏粗体文本字段),并使用 NSSlider 作为辅助视图。警报的响应是一条记录,其中包含按下的按钮的名称和滑块的值。
windows 等用户界面项需要在主线程上 运行; run
处理程序发现了这一点,因为脚本编辑器通常 运行 在他们自己的线程中编写脚本。
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
所以我想知道是否有办法在 AppleScript 中添加滑块。我发现 this Apple Community post 讨论如何从某些应用程序读取滑块,但我想显示一个具有特定范围的滑块。所以我希望它看起来像这样:
set theSlider to slider from 1 to 10 with prompt "Set your number"
我认为它不存在,但我想确定一下,因为它会不时派上用场。
如评论中所述,常规 AppleScript 没有类似的东西,但是 Cocoa 框架可以与一些 AppleScriptObjC.
NSAlert class 是用于 display alert
的,但是因为你无法进入 StandardAdditions 对于自定义,您需要自己构建警报。在此示例中,我正在创建一个 NSAlert(稍微隐藏粗体文本字段),并使用 NSSlider 作为辅助视图。警报的响应是一条记录,其中包含按下的按钮的名称和滑块的值。
windows 等用户界面项需要在主线程上 运行; run
处理程序发现了这一点,因为脚本编辑器通常 运行 在他们自己的线程中编写脚本。
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