如何在 AppleScript 中控制亮度?

How to control brightness in AppleScript?

我想用 AppleScript 更改亮度级别,但我不知道怎么做。

很难在官方文档中找到它。

感谢任何指导。

虽然 Peter 的回答中的内容对我有用;尽管如此,它并不像对该答案的评论中所指出的那样。因此,这里有一些其他的亮度调节方法。

有一个名为 brightness 第三方实用程序 可以在 TerminalAppleScript 使用 do shell script 命令,例如:

  • 注意:这些是暂时将亮度设置为0,等待2秒,然后将亮度设置为0.75(或75%)的演示示例。刻度为 0 到 1 之间的任何 十进制值

终端:

/path/to/brightness 0; sleep 2; /path/to/brightness 0.75

AppleScript中:

do shell script "/path/to/brightness 0; sleep 2; /path/to/brightness 0.75
  • 注意:如果 brightness 位于 目录中, 位于 PATH 中,您可以省略 [=] 的 /Path/to/ 部分34=]命令。

这是另一个 AppleScript 示例,未使用任何 第三方实用程序:

以下示例 AppleScript 代码,在macOS High下测试Sierra,会将亮度设置为 0,等待 2 秒,然后设置为 0.75(或 75%)。刻度为 0 到 1 以及介于两者之间的任何小数值。请注意,在其他版本的 macOS 上,可能需要针对 path 调整 code 34=]滑块 1 的值指示器 1。

--  # Start with System Preferences closed.

if running of application "System Preferences" then
    try
        tell application "System Preferences" to quit
    on error
        do shell script "killall 'System Preferences'"
    end try
end if
repeat while running of application "System Preferences" is true
    delay 0.1
end repeat

--  # Open System Preferences to the target pane.

tell application "System Preferences" to ¬
    reveal anchor "displaysDisplayTab" of ¬
        pane id "com.apple.preference.displays"

--  # Change the Brightness: slider.

tell application "System Events" to ¬
    tell value indicator 1 of ¬
        slider 1 of ¬
        group 1 of ¬
        tab group 1 of ¬
        window 1 of ¬
        application process "System Preferences" to ¬
        set its value to 0

delay 2

tell application "System Events" to ¬
    tell value indicator 1 of ¬
        slider 1 of ¬
        group 1 of ¬
        tab group 1 of ¬
        window 1 of ¬
        application process "System Preferences" to ¬
        set its value to 0.75

--  # Close System Preferences.

quit application "System Preferences"

注意:示例 AppleScript code 就是这样,不包含任何可能适当的错误处理。用户有责任根据需要添加任何 错误处理 。查看 try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay 命令 在适当的事件之间可能是必要的,例如delay 0.5,适当设置延迟