有没有一种编程方法来检查 OSX 上的亮度是最大值还是最小值



所以,我正在制作一个虚拟助手,它可以响应语音命令并在命令执行后回复。我目前正在编写一个脚本来增加/减少亮度,并回复say "brightness increased"say "brightness decreased"

我想设置一个条件来检查亮度级别,如果值为最大值或最小值,请回复说亮度为最大值/最小值......有没有办法用AppleScript检查亮度级别?我已经在网上搜索了几个小时,但我还没有找到任何有用的信息......这是我到目前为止的代码,其中包含一些伪代码,用于我想实现检查最小/最大值的位置,因为此时我几乎被难住了......

on run argv
    if (brightnessLevel = max) then              -- start of pseudocode
        say "brightness is already at max level"
        error number -128
    else if (brightnessLevel = min) then
        say "brightness is already at min level"
        error number -128                        -- end of pseudocode
    if (count of argv = 1) then
        if (item 1 of argv as string = "up") then
            tell application "System Events"
                key code 144
                say "brightness increased" using "Trinoids"
            end tell
        else if (item 1 of argv as string = "down") then
            tell application "System Events"
                key code 145
                say "brightness decreased" using "Trinoids"
            end tell
        end if
    else
        return "Error: argument count invalid"
    end if
end run

编辑:我想我应该指定,我不想使用自制软件中的brightness包,因为如果有一种方法可以在没有依赖项的情况下做到这一点,那就是我想要的方式。

适用于我的MacBook Pro:

set backlightLevel to do shell script "nvram backlight-level | awk '{print $2}'"
if backlightLevel is "%ff%03" then
    say "brightness is already at max level"
    return
else if backlightLevel is "%00%00" then
    say "brightness is already at min level"
    return
end if

这是仅使用AppleScript的另一种解决方案。

这适用于我在MacBook Pro 15上使用最新版本的Sierra"

property theBrightness : ""
tell application "System Preferences"
    reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
    tell application "System Events" to tell process "System Preferences" to tell window 1
        set theBrightness to get value of value indicator 1 of slider 1 of group 2 of tab group 1
        set theBrightness to result * 10
        set theBrightness to round theBrightness
        set theBrightness to theBrightness / 10
        say "Brightness Is Currently At Level " & (theBrightness * 10 as integer) using "Trinoids"
        if theBrightness is equal to 1 then
            say "Brightness Is Already At Maximum Level" using "Trinoids"
        end if
        if theBrightness is equal to 0.1 then
            say "Brightness Is Already At Minimum Level" using "Trinoids"
        end if
    end tell
    --  call following the handler anywhere in this script as long as it's before the "quit" command
    my setBrightnessLevel(0.5) -- lowest to highest values from 0.1 to 1.0  example: .5 = halfway bright
    quit
end tell
on setBrightnessLevel(theNumber)
    tell application "System Events" to tell process "System Preferences" to tell window 1 to ¬
        set value of value indicator 1 of slider 1 of group 2 of tab group 1 to theNumber
end setBrightnessLevel

下面是使用键盘增量的 16 个值的版本。 目前仍在努力打磨它

global roundedUpLevelValues
global theDesiredBrightness
property theBrightness : ""
property levelValues : {0.062499992549, 0.125000014901, 0.187499955297, 0.250000029802, 0.312499970198, 0.374999910593, 0.437500119209, 0.500000059605, 0.562500238419, 0.624999880791, 0.687500119209, 0.749999761581, 0.8125, 0.875000238419, 0.937499880791, 1.0}
property dontRoundtheDesiredBrightness : 0.0625
set dialogBrightnessLevel to display dialog "Darkest To Brightest... Insert A Value From 1 to 16" default answer "16" buttons {" Cancel", "Set Brightness"} default button 2
set theDesiredBrightness to text returned of dialogBrightnessLevel -- darkest to brightest... Insert a value from 1 to 16
set roundedUpLevelValues to (item theDesiredBrightness of levelValues) * 100000
set roundedUpLevelValues to round roundedUpLevelValues
set roundedUpLevelValues to roundedUpLevelValues / 100000
tell application "System Preferences"
    reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
    tell application "System Events" to tell process "System Preferences" to tell window 1
        set theBrightness to get value of value indicator 1 of slider 1 of group 2 of tab group 1
        set theBrightness to result * 16
        set theBrightness to round theBrightness
        set theBrightness to theBrightness / 16
        say "Brightness Is Currently At Level " & (theBrightness * 16 as integer) using "Trinoids"
        if theBrightness is equal to item 16 of levelValues then
            say "Brightness Is Already At Maximum Level" using "Trinoids"
        end if
        if theBrightness is equal to dontRoundtheDesiredBrightness then
            say "Brightness Is Already At Minimum Level" using "Trinoids"
        end if
    end tell
    --  call the following  handler anywhere in this script as long as it's before the "quit" command
    try
        my setBrightnessLevel(roundedUpLevelValues)
    end try
    quit
end tell
on setBrightnessLevel(theDesiredBrightness)
    tell application "System Events" to tell process "System Preferences" to tell window 1 to ¬
        set value of value indicator 1 of slider 1 of group 2 of tab group 1 to theDesiredBrightness
end setBrightnessLevel

最新更新