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

Macos 是否有一种编程方法来检查OSX上的亮度是最大值还是最小值?,macos,applescript,Macos,Applescript,因此,我正在制作一个虚拟助手,它响应语音命令,并在命令执行后回复。我目前正在编写一个脚本来增加/减少亮度,并用说“亮度增加”或说“亮度降低” 我想设置一个条件来检查亮度水平,如果该值是最大值或最小值,回答说亮度是最大值或最小值。。。有没有办法用AppleScript检查亮度级别?我已经在网上搜索了几个小时了,我还没有找到任何有用的信息。。。这是我到目前为止的代码,其中包括一些伪代码,我想在其中实现最小/最大值检查,因为在这一点上我几乎被难住了 on run argv if (bright

因此,我正在制作一个虚拟助手,它响应语音命令,并在命令执行后回复。我目前正在编写一个脚本来增加/减少亮度,并用
说“亮度增加”
说“亮度降低”

我想设置一个条件来检查亮度水平,如果该值是最大值或最小值,回答说亮度是最大值或最小值。。。有没有办法用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

编辑:我想我应该指定,我不想使用自制的
亮度
软件包,因为如果有一种无依赖性的方法,那就是我想要的方法。

这在我的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

FWIW不使用
窗口的实际
名称
,即
窗口“内置视网膜显示”
,只需使用例如
窗口1
,因为标记对象总是更好的,除非用例绝对要求引用对象的实际
名称
属性。“这里还有一个仅使用AppleScript的解决方案。”
DoShell脚本
是AppleScript!:)也就是说,我实际上已经用slider 1的
值指示器1的值编写了一个“AppleScript only”,并且没有发布它,因为我当时没有时间了,还没有回来,但是我必须说,我更喜欢你的方法,尽管实际上有16个不同的级别,使用亮度功能键时不是10。有没有办法使用亮度功能键可以执行的实际步数来执行此操作?此外,在我的MacBook Pro上,滑块1的
值指示器1的有效值为
0.0
1.0
,增量为
0.0625
。值得称赞的是“窗口1”方法。至于“还有什么问题”,这里有另一个仅使用AppleScript的解决方案。“DoShell脚本是AppleScript!”。。。是的,虽然它都是AppleScript,但它仍然调用shell脚本命令。。。仅就我自己而言,我是一个shell脚本的白痴lol,我很容易偏离正轨。也就是说,因为你,我花了近2个小时研究终端,仔细查看和阅读手册页。我还将研究你提到的16步增量,看看我能想出什么。我欠你一个歉意,当我说“实际上有16个不同的级别,而不是使用亮度功能键时的10个级别。”在之前的评论中。。。我说错了,应该说。。。实际上有17种不同的亮度级别,在使用亮度功能键时,从
0.0
1.0
0.0625
为增量,而不是以10为增量,分16步实现。因此,由于我的错误,我导致您编写的代码不能真正反映实际情况,但我在同一评论中说“
滑块1的值指示器1
0.0
1.0
0.0625
的增量”.对不起,我说错了,您没有看到可能导致练习无效的差异。对不起,我花了这么长时间回复,我有一个严格的截止日期。我只是想澄清一下,您的解决方案确实有效,但是打印出来的值与您在帖子中提供的值不同——尽管如此,这只是设置最小和最大亮度并将打印出来的值复制到终端的问题。谢谢你的解决方案!但是,如果我想做一个能在所有mac电脑上运行的解决方案,这个解决方案就行不通了,我想我知道一个解决方案,可以让它在所有mac电脑上运行,但要做到这一点,我需要知道是否有办法让脚本知道它是macbook还是imac,因为macbook和imac的亮度上升和亮度下降键代码不同。你知道有没有办法知道用户是在使用macbook还是imac?有没有办法把显示器的类型改成灰色?这对我来说已经足够了。@Elliot Tregoning,这将得到模型名:
设置模型名以执行shell脚本“system_profiler SPHardwareDataType | awk-F':'/model name:/{print$2}'
@Elliot Tregoning,这将得到显示名:
告诉应用程序“系统事件”要将displayName设置为当前桌面的显示名称
@Elliot Tregoning,这也将为您提供显示名称:
设置displayName以执行shell脚本“system\u profiler SPDisplaysDataType | awk'/^[]+显示:/{getline;sub(/^[]+/,\“\”);打印$0;}'
,尽管这假定只有一个显示。