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

查看:95
本文介绍了使用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).

确认!忘了提及使用系统事件进行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天全站免登陆