使用applescript登录mac

时间:2010-12-04 23:53:36

标签: macos applescript

我写了一个小脚本在远程机器上打开Safari:

tell application "Safari"
    activate
    set URL of first document to "http://www.stackoverflow.com/"
end tell

我会远程运行applescript,因为我可以通过SSH连接。

但是,如果机器没有实际登录(即,我没有达到并输入我的详细信息),则没有任何反应。注意,我仍然可以使用SSH连接到计算机,因此仍然可以运行脚本。

那么我可以使用applescript远程登录机器吗?

1 个答案:

答案 0 :(得分:5)

name="theshortname";
password="thePassword";

osascript <<EOF
tell application "System Events"
    keystroke tab
    keystroke (ASCII character 8)
    keystroke tab
    keystroke (ASCII character 8)
    keystroke return
    delay 0.5
    keystroke tab
    keystroke "$name"
    keystroke tab
    delay 0.5
    keystroke "$password"
    delay 0.5
    keystroke return
    keystroke return
    keystroke return
    keystroke return
end tell
EOF

是。这就是我使用的。只需更改前两行即可。我是以root身份做的;没有用sudo测试它。暂停确保文本出现并且插入光标已移动到下一个字段,然后继续。开头的额外内容确保凭证最终出现在正确的字段中(ASCII字符8是退格键)。

已知错误:尽管插入光标闪烁,Snow Leopard的登录窗口仍然不允许输入文本。我没有找到一种方法来发送在名称字段中获得实际焦点所需的点击(用鼠标单击一下(甚至通过vnc)使脚本工作得很好)。

编辑:确认!忘了提到使用System Events进行GUI交互取决于OS X中内置的Universal Access系统。它只是一个复选框,只需要打开一次。系统偏好设置&gt;通用访问&gt;启用辅助设备的访问权限。