Unlocking your mac with applescript

Hiya all,

It’s been a while since I last posted (more than a year or so?)… but I’m back at it (actually this would be called SOG, or Studie Ontwijkend Gedrag in the Netherlands…

Anyway… I’d like to have a way to unlock my mac using my IR remote, but requiring a password if it’s being unlocked in any other way…

I already scripted most of the coding (very simple actually), though whenever I actually execute this code none of the windows are in focus after unlocking the display (Chris C had the same problem about a year ago; http://apple.stackexchange.com/questions/110910/toggling-screensaver-password-with-script).

beep
do shell script "pmset displaysleepnow"
delay 5
beep
tell application "System Events"
	set require password to wake of security preferences to false
end tell
do shell script "defaults write com.apple.screensaver askForPassword -bool false"

do shell script "caffeinate -u -t 1"

do shell script "defaults write com.apple.screensaver askForPassword -bool true"

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

To ‘fix’ the issue I added this code to the bottom (for testing purposes);


delay 20
do shell script "pmset displaysleepnow"
do shell script "caffeinate -u -t 1"

The other ‘fix’ is having System Events enter the password manually, but that isn’t as ‘sexy’ as I would like to have it.
Anyway, the problem is that when unlocking the screen with the requirement of a password disabled, the lock window still is active and ‘traps’ any and all focus onto itself. Therefor disable all text and most of the UI input’s.

Does anyone know a way to kill the lockwindow/refocus onto other applications?

P.s. I did try “Tell application ‘Finder’ to activate”, though that doesn’t work.

So, I’ve coded a little bit further…

I thought the lock window might’ve actually been present but hidden (as the focus is trapped by it), and tried to ether the password anyway (using below code). Though that doesn’t seem to work… I thought up another way to the same result but it [strikethrough]might[/strikethrough] will take a lot longer before the display “wakes up”.

-- To grab the user's password without hardcoding it into the script (credits to Inspira.io)
on getPassword(keychainItemName)
	set my_password to do shell script ("security 2>&1 >/dev/null find-generic-password -gl " & quoted form of keychainItemName & " | cut -c 11-99 | sed 's/\"//g'")
end getPassword

set my_password to getPassword("PassKeychainName")

tell application "System Events"
	log my_password
	keystroke my_password --password
	delay 1
	key code 52 -- enter
end tell


The second idea I came up with was to, after the script gets called lower the display’s screen brightness to 0 before actually waking it up. Then enter the users password, and then restoring the previous screen brightness.

I’ll try this idea soon…

Did it work? Your last idea?

In principle, yes. For example, I use a shortcut command that executes the commands via SSH:

SSH-Script unlock:

caffeinate -u -t 3 </dev/null &>/dev/null &

osascript -e 'tell application "System Events" to key code 0 using command down'
	
osascript -e 'tell application "System Events" to keystroke "YourPassWord"'
	
osascript -e 'tell application "System Events" to keystroke return'

SSH-Script lock:

l=$(/usr/libexec/PlistBuddy -c "print :IOConsoleUsers:0:CGSSessionScreenIsLocked" /dev/stdin 2>/dev/null <<< "$(ioreg -n Root -d1 -a)")

if [ "$l" != "true" ]; then
	osascript -e 'tell application "System Events" to keystroke "q" using {control down, command down}'
	osascript -e 'delay 1'
	pmset displaysleepnow
fi

I use it to synchronize the login with my new and old MacBook, which I operate together at home with Universal Control.