Applescript 如何使显示器处于休眠和唤醒状态
我的mac电脑的背光有问题。有时当我打开它的背光不亮。然而,如果我手动强迫屏幕睡眠和醒来几次,屏幕就会亮起来。所以我想写一个applescript来自动化这个过程。 我看过。。。 但这不是用Applescript编写的(或者如果是,我就不知道如何运行它) 我试过以下方法Applescript 如何使显示器处于休眠和唤醒状态,applescript,Applescript,我的mac电脑的背光有问题。有时当我打开它的背光不亮。然而,如果我手动强迫屏幕睡眠和醒来几次,屏幕就会亮起来。所以我想写一个applescript来自动化这个过程。 我看过。。。 但这不是用Applescript编写的(或者如果是,我就不知道如何运行它) 我试过以下方法 do shell script "pmset displaysleepnow" tell application "Google Chrome" activate end tell do shell script "pmset
do shell script "pmset displaysleepnow"
tell application "Google Chrome"
activate
end tell
do shell script "pmset displaysleepnow"
tell application "Google Chrome"
quit
end tell
但是屏幕是空白的,永远不会重新亮起。我也试着用咖啡因来回答一些问题。我试着用代码来移动鼠标和/或用编程的方式单击鼠标
如果我只是运行displaysleepnow,然后运行“激活chrome”,但如果再次执行displaysleepnow,则不会显示任何内容来重新激活它
我正在使用macOS 10.14.4
一种方法是:
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
代码解释
set now to do shell script "date -r $(date +%s) '+%m/%d/%y %H:%M:%S'"
基本上使用Applescript命令执行以下bash命令以获取当前日期/时间:
$date-r$(日期+%s)+%m/%d/%y%H:%m:%s'
这将当前日期/时间(格式为MM/DD/YY HH:MM:SS
)分配给名为now
的变量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秒数(即,添加15秒)来获取未来日期/时间。此未来日期/时间(再次使用shellsdate
实用程序计算)分配给名为wakeTime
的变量do shell script "pmset schedule wake " & quoted form of wakeTime & ¬
" && pmset displaysleepnow" password myPassword with administrator privileges
利用该实用程序执行以下两项任务:
- 将显示器安排为在未来的日期/时间“唤醒”,即15秒后
- 现在将系统设置为“睡眠”
wakeAfterSeconds
变量的秒数设置为适合您的系统的值。您可能会发现需要增加或减少该值
本质上,您将wakeAfterSeconds
值设置为的秒数必须大于显示器进入睡眠模式所需的秒数-必须先进入睡眠状态,然后才能将其唤醒pmset
实用程序设置唤醒计算机的计划必须以root用户身份运行,因此需要密码。因此,需要根据需要设置myPassword
变量的值在这种情况下,您可能需要考虑在语句中包覆上述代码。
例如,以下步骤将重复该过程两次: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
利用“登录项目”自动运行应用程序:
此外,您可能需要考虑将上述Apple脚本应用程序添加到登录项中。这将在启动计算机时自动运行应用程序。
+
,然后选择AppleScript应用程序pmset
实用程序没有包含displayseleepnow
命令。它只有pmset sleepnow
命令。因此,对于希望在旧版OSX上运行此功能的用户,您需要将上一个do shell script
命令更改为:
do shell script "pmset schedule wake " & quoted form of wakeTime & ¬
" && pmset sleepnow" password myPassword with administrator privileges
^^^^^^^^^^^^^^
非常好的回答非常有效。非常感谢。顺便问一下,你知道为什么我的方法不起作用吗?