BlueSense AppleScripts

How to quit the screensaver and disable the password in AppleScript

This part checks whether the screen saver is active or not, and enter the password if so. It’s wake the before.

When the screensaver is active with password set, windows, menus, keyboard are freezed.
If the script deactivate the password without entered the password before, the system will stay freezed and you will have to hard reboot your computer.
It's for this reason that is why this script is larger than others, It checks that the screen saver is active, and enters the password for you..

Display Sleep : If you want to wake and sleep the display, you can compile SleepDisplay, a Mac OS X Program to manage the display sleep : https://github.com/kimhunter/SleepDisplay/
If you just want to wake-up the screen, use the caffeinate command as below.


script DisableScreenSaverPassword
try
	set my_password to getPassword(KeychainPasswordName)

	-- Wake up the display
	if ScreenIsLocked() then
		# do shell script ("/usr/local/bin/SleepDisplay -wake") -- method 1 with SleepDisplay https://github.com/kimhunter/SleepDisplay
		do shell script ("/usr/bin/caffeinate -u -t 1") -- method 2 with caffeinate
		delay 3
	end if

	-- Enter the Screensaver Password
	tell application "System Events" to key code 53 using command down
	delay 2

	if (my ScreenSaverRunning()) or (my ScreenIsLocked()) then
		tell application "System Events"
			keystroke "a" using command down -- select all
			keystroke my_password --enter password
			delay 1
			key code 52 -- enter
		end tell
	end if

	delay 2

	-- Disable the password for the Screen Saver
	if not (my ScreenSaverRunning()) then
		-- method 1
		tell application "System Events" to set properties of security preferences to {require password to wake:false}

		--method 2
		(*
		tell application "System Events"
			tell security preferences
				activate
				set properties to {require password to wake:false}
			end tell
		end tell
		*)

		-- method 3
		(*
		do shell script ("defaults write ~/Library/Preferences/com.apple.screensaver.plist askForPassword -int 0")
		do shell script ("defaults write ~/Library/Preferences/com.apple.screensaver.plist askForPasswordDelay -float 0")
		*)
	end if

	-- Reactivate the Screen Saver at night between 20 PM and 8 AM
	if RunLaunchScreenSaverAfter8PM as boolean then
		set hour to hours of (current date)
		if hour ≥ 20 and hour ≤ 23 or hour ≥ 0 and hour < 8 then
			tell application id "com.apple.ScreenSaver.Engine" to launch
		end if
	end if

on error errString number errorNumber
	log (errString)
end try
end script