Login to mac using applescript
I wrote a little script to open Safari开发者_如何学Python on a remote machine:
tell application "Safari"
activate
set URL of first document to "http://www.stackoverflow.com/"
end tell
I'd run the applescript remotely because I can connect via 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.
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
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).
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).
Edit: 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.
You can setup autologin via defaults
command in mac depending on your security requirements. https://apple.stackexchange.com/questions/264068/can-autologin-be-set-up-from-the-command-line-how
精彩评论