【问题标题】:How can I unlock my computer and display the screen through applescript?如何通过applescript解锁我的电脑并显示屏幕?
【发布时间】:2014-03-26 13:12:10
【问题描述】:

我每天早上都会运行一个“唤醒”脚本,它会打开一个 youtube 播放列表。但是它没有显示在屏幕上,因为我的计算机在空闲几分钟后自动锁定。

我正在使用从另一个问题得到的以下脚本

tell application "System Events"
  tell security preferences
    set require password to wake to false
  end tell
end tell

tell application "ScreenSaverEngine" to quit

但它实际上并没有让我的电脑屏幕打开。我必须按一个键/移动鼠标。它实际上使我的计算机无法使用,只有最前面的应用程序的菜单有效,必须重新启动。也许它没有正确解锁?

【问题讨论】:

  • 正在运行哪个版本的 OS X?在 10.9+ 中,这充其量是困难的,并且可能需要某种类型的解决方法或 hack。
  • 你可以试试set require password to wake of security preferences to false 和/或do shell script "killall ScreenSaverEngine"(顺便说一句——我还没有测试过这些)。
  • 设置需要false 的密码似乎确实有效。但是即使我可以看到应用程序,我的计算机仍然感觉被锁定。
  • 是的,我认为这是问题之一——因为您似乎无需密码即可解锁屏幕保护程序,但系统仍处于锁定状态。

标签: macos applescript


【解决方案1】:

我在 10.10.2 上对此进行了测试,它解锁了屏幕。

首先,您需要下载我编写的名为 MouseTools 的免费程序。它使用您的鼠标进行操作,在这种情况下,我们可以使用 MouseTools 移动您的鼠标,从而解锁屏幕。 Get MouseTools here。然后系统事件可以输入您的密码并完成解锁屏幕。然后,您需要在您的计算机上输入 MouseTools 的路径,并在脚本顶部输入解锁计算机的密码。

请注意,代码获取当前鼠标位置并将其向上和向右移动 1 个像素以解锁屏幕。祝你好运。

-- input your values in these variables
set pword to "my password"
set mousetools to (path to home folder as text) & "path:to:MouseTools"

-- get the current mouse location and add a pixel to each coordinate
set currentLocation to paragraphs of (do shell script quoted form of POSIX path of mousetools & " -location")
set newX to ((item 1 of currentLocation) as number) + 1
set newY to ((item 2 of currentLocation) as number) - 1

-- move the mouse
do shell script quoted form of POSIX path of mousetools & " -x " & (newX as text) & " -y " & (newY as text)

delay 1

-- keystroke your password
tell application "System Events"
    keystroke pword
    delay 1
    keystroke return
end tell

【讨论】:

  • 它没有为我解锁计算机:/。也在 10.10.2 上运行。它确实成功地移动了鼠标,所以我知道该部分正在工作,即使屏幕被锁定,在我按下一个键解锁它后,我的鼠标位于不同的区域(我更改了要移动的像素数量以进行测试)。但它没有解锁。移动鼠标后屏幕也不亮。
  • 我在屏幕保护程序运行的情况下对其进行了测试,它停止了屏幕保护程序并解锁了屏幕。无论如何,这是我最好的主意,所以也许你可以通过使用屏幕保护程序而不是它当前锁定屏幕的方式让它为你工作。
  • 哦,一定是这样。我没有使用屏幕保护程序,我正在让显示器进入睡眠状态。
【解决方案2】:

我还没有在 10.10 上测试过这个,但你不妨看看

  /usr/bin/caffeinate 

查看手册页:

man -s8 caffeinate

我的想法是,您要么使用 caffeinate 来执行脚本的其余部分,并在脚本运行时防止显示器进入睡眠状态,要么设置一个超时值,拒绝显示器进入睡眠状态。此行为在超时发生后停止。

此实用程序位于我的 /usr/bin 中,但它可能已与 Apple 的 Dev-Tools 一起安装。

which caffeinate

会告诉你它是否安装在你的系统上。

如果你没有它,那么你可以用 pmset 和 sleep 来做一些事情,所以你指定一个进程何时唤醒,并杀死你拥有的 pmset noidle 进程在后台启动。 (将进程发送到后台后,最新发送到后台的进程号存储在 $! 中。)

【讨论】:

  • ++ 听起来像是正确的方法; caffeinate 确实带有原始 OS 10.10(和 10.11)安装;附带说明:允许您指定列表部分的man选项是-S,而不是-s - 不过,奇怪的是,后者似乎也有效;对于单个部分,您可以直接指定部分编号 (man 8 caffeinate),或者在没有歧义的情况下完全省略部分编号 (man caffeinate)。
【解决方案3】:

在 macOS Sierra(10.12) 上对此进行了测试。

do shell script "caffeinate -u -t 3"
tell application "System Events"
    keystroke "<password>"
    delay 1
    keystroke return
end tell

参考:

【讨论】:

    【解决方案4】:

    在 OS X El Capitan 上进行了尝试和测试 ------------------------------------------------------------

    tell application "System Events"
    
      if name of every process contains "ScreenSaverEngine" then
    
        tell application "ScreenSaverEngine"
    
             quit
    end tell
    
    set pword to "password here"
    
    delay 1
    
    tell application "System Events"
    
        keystroke pword
    
        delay 1
    
        keystroke return
    end tell
    
              end if
         end tell
    end run
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多