如何让显示器休眠和唤醒

How to make the display sleep and wake

我的 mac 的背光有问题。有时当我打开它时,背光不亮。但是,如果我手动强制屏幕休眠并唤醒几次,它就会亮起。所以我想写一个 applescript 来自动化这个过程。 我看过... How do I wake from display sleep in OSX 10.7.4? 但这不是用 Applescript 写的(或者如果是我不知道如何 运行 它。

我试过以下方法

do shell script "pmset displaysleepnow"
tell application "Google Chrome"
activate
end tell
do shell script "pmset displaysleepnow"
tell application "Google Chrome"
quit
end tell

但是屏幕黑了,再也没有亮过。根据有关此的一些问题,我也尝试使用咖啡因。我已尝试使用代码移动鼠标 and/or 以编程方式单击鼠标。

如果我只是 运行 displaysleepnow 后跟 'activate chrome' 有效,但如果再次执行 displaysleepnow 似乎没有任何东西可以重新激活它。

我正在使用 macOS 10.14.4

剧情简介

解决此问题的一种方法是:

  1. 获取当前date/time加上x未来的秒数。
  2. 使用在第 1 点获得的 date/time 将 system/display 安排到 "wake"
  3. 然后将显示器设置为睡眠。

解决方案

考虑使用以下 AppleScript 来满足您的要求:

set wakeAfterSeconds to "15"
set myPassword to "xxxxxxxxxx"

set now to do shell script "date -r $(date +%s) '+%m/%d/%y %H:%M:%S'"

set wakeTime to do shell script "date -jv +" & wakeAfterSeconds & "S -f " & ¬
  quoted form of "%m/%d/%y %H:%M:%S" & " " & quoted form of now & " " & ¬
  quoted form of "+%m/%d/%y %H:%M:%S"

do shell script "pmset schedule wake " & quoted form of wakeTime & ¬
  " && pmset displaysleepnow" password myPassword with administrator privileges

代码说明

  1. 读到的那一行;

    set now to do shell script "date -r $(date +%s) '+%m/%d/%y %H:%M:%S'"
    

    本质上是执行下面的bashdate command, using the Applescript do shell script命令,获取当前date/time:

    $ date -r $(date +%s) '+%m/%d/%y %H:%M:%S'
    

    这会将格式为 MM/DD/YY HH:MM:SS 的当前 date/time 分配给名为 now 的变量。

  2. 阅读的行数;

    set wakeTime to do shell script "date -jv +" & wakeAfterSeconds & "S -f " & ¬
      quoted form of "%m/%d/%y %H:%M:%S" & " " & quoted form of now & " " & ¬
      quoted form of "+%m/%d/%y %H:%M:%S"
    

    通过添加 x 获得未来 date/time。当前 date/time 的秒数(即增加 15 秒)。这个未来 date/time(再次使用 shell date 实用程序计算)被分配给名为 wakeTime.

  3. 的变量
  4. 行:

    do shell script "pmset schedule wake " & quoted form of wakeTime & ¬
      " && pmset displaysleepnow" password myPassword with administrator privileges
    

    利用 pmset 实用程序执行以下两项任务:

    • 安排显示 "wake" 在未来 date/time,即 15 秒后。
    • 现在将系统设置为 "sleep"

补充说明:

  1. 您需要设置编号。将分配给 wakeAfterSeconds 变量的秒数设置为适合您的系统的值。您可能会发现需要增加或减少此值。

    本质上,您设置 wakeAfterSeconds 值的秒数必须长于显示器进入睡眠模式所需的秒数 - 它必须处于睡眠状态在它被唤醒之前!

  2. 使用 pmset 实用程序设置唤醒计算机的时间表必须是 运行 根用户,因此它需要您的密码。因此,您需要根据需要设置 myPassword 变量的值。

  3. 你提到了;

    ... if I manually force the screen to sleep and wake a couple of times it does light up.

    在这种情况下,您可能需要考虑将上述代码包装在 repeat 语句中。

    例如,下面重复这个过程两次:

    set wakeAfterSeconds to "15"
    set myPassword to "xxxxxxxxxx"
    
    repeat 2 times
      set now to do shell script "date -r $(date +%s) '+%m/%d/%y %H:%M:%S'"
    
      set wakeTime to do shell script "date -jv +" & wakeAfterSeconds & "S -f " & ¬
        quoted form of "%m/%d/%y %H:%M:%S" & " " & quoted form of now & " " & ¬
        quoted form of "+%m/%d/%y %H:%M:%S"
    
      do shell script "pmset schedule wake " & quoted form of wakeTime & ¬
        " && pmset displaysleepnow" password myPassword with administrator privileges
    
      delay wakeAfterSeconds
    end repeat
    

    利用"Login Items"自动运行应用程序:

    此外,您可能需要考虑将上述 AppleScript 应用程序添加到您的 登录项。当您 start-up 您的计算机时,这将自动 运行 应用程序。为此:

    1. 首先确保脚本 "File Format" 已通过您的 AppleScript 编辑器保存为 "Application"。
    2. 启动 "System Preferences" 和 select "Users & Groups".
    3. Select 在左侧面板中您的用户帐户,然后单击 "Login Items"
    4. 单击加号图标 + 和 select AppleScript 应用程序。
    5. 重新启动计算机。

年长 Mac OSX:

在旧版本的 macOS(即 OSX)上,pmset 实用程序不包含 displaysleepnow 命令。它只有 pmset sleepnow 命令。因此,对于那些想要 运行 在较旧的 OSX 上执行此操作的人,您需要将最后一个 do shell script 命令更改为:

do shell script "pmset schedule wake " & quoted form of wakeTime & ¬
  " && pmset sleepnow" password myPassword with administrator privileges
       ^^^^^^^^^^^^^^