使用applescript登录mac [英] Login to mac using applescript

查看:32
本文介绍了使用applescript登录mac的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I wrote a little script to open Safari on a remote machine:

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

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

I'd run the applescript remotely because I can connect via SSH.

但是,如果机器没有实际登录(即,我还没有进入它并输入我的详细信息),则什么也不会发生.请注意,我仍然可以使用 SSH 连接到机器,因此仍然可以运行脚本.

However, if the machine is not physically logged in (ie, I haven't gone up to it and entered my details), nothing happens. Note, I can still connect to the machine using SSH so can still run scripts.

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

So can I log the machine in remotely using applescript?

推荐答案

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 是退格符).

Yes. This is what I use. Just change the first two lines. I do it as root; haven't tested it with sudo. The pauses ensure the text has appeared and the insertion cursor has moved to the next field before continuing. The extra stuff at the beginning ensures the credentials end up in the right fields (ASCII char 8 is backspace).

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

Known bug: sometimes Snow Leopard's login window will not allow text input despite the blinking insertion cursor. I've not found a way to send the click needed to get actual focus in the name field (a single click with a mouse (even via vnc) makes the script work fine sigh).

确认!忘了说,使用 System Events 进行 GUI 交互依赖于 OS X 内置的 Universal Access 系统.它只是一个复选框,只需要打开一次.系统偏好设置 > 通用访问 > 启用辅助设备访问.

ACK! Forgot to mention that using System Events for GUI interaction depends on the Universal Access system built into OS X. It's just a check-box and only needs to be turned on once. System Preferences > Universal Access > Enable access for assistive devices.

这篇关于使用applescript登录mac的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆