Applescript to turn off Screen Saver in System Prefs?

Hi all,

I am trying to figure out a way to turn the screen saver completely off using AS.

I have figured out how to get to the System Prefs pane open and to move the slider
with the GUI scripting.

However, when I open the Systems Prefs up again, the setting hasn’t changed, even
with a “quit saving yes” in the command line at the end of the script.

what’s up? any thoughts?

here’s the code:


tell application "System Preferences"
	activate
	set current pane to pane "com.apple.preference.desktopscreeneffect"
end tell

tell application "System Events"
	tell application process "System Preferences"
		tell window "Desktop & Screen Saver"
			tell tab group 1
				tell group 1
					tell slider 1
						set originalValueScreen to value
						set value to maximum
					end tell
				end tell
			end tell
		end tell
	end tell
end tell

tell application "System Preferences" to quit saving yes


If you don’t mind your password in an AppleScript, here’s a short version that bypasses the Preference Panel completely. It sets the sleep time (dim) to never (which is 0) and the spindown time for hard disks to never. Setting them to any positive integer, sets the number of minutes before dimming or spindown happens.

do shell script "sudo pmset spindown 0 dim 0" password "put_yours_here" with administrator privileges

If you’d prefer not to have your password exposed in a script, here’s another that gives you a choice of sleep soon or sleep never. It requires that you create the password in Key Chain Access and name it “applescriptAllow” (of course you can change that), and make sure it is defined as generic or the search for it is veeeery slow. Credit for this script goes to several regular contributors to this forum but I didn’t include the attributions when I put it together, alas.

tell application "Keychain Scripting"
	launch
	tell current keychain
		tell (some generic key whose name is "applescriptAllow")
			set pass to password
		end tell
	end tell
end tell

tell AppleScript to activate
set response to do shell script "pmset -g" password pass with administrator privileges
set AppleScript's text item delimiters to return
set response_items to text items of response
set AppleScript's text item delimiters to ""
repeat with aItem in response_items
	if aItem contains "dim" then
		set current_time to (characters -2 thru -1 of aItem) as string
		exit repeat
	end if
end repeat
set response to display dialog "Enter number of minutes before sleeping display (current time is displayed)" default answer current_time buttons {"Sleep in 1 min", "Never sleep", "OK"}
set new_time to text returned of response
if button returned of response is "Sleep in 1 min" then
	set new_time to "1"
end if
if button returned of response is "Never sleep" then
	set new_time to "0"
end if
do shell script ("pmset dim " & new_time) password pass with administrator privileges

The above script may work better when using
set current_time to (characters -3 thru -1 of aItem) as string
instead of:


 set current_time to (characters -2 thru -1 of aItem) as string 

(it will correctly display & set dim values > 99 minutes as entered in “Energy Server”)

does that work? looks like you’re setting the disk spin down and the display dim
whereas i thought the original poster was asking how to switch off the screen saver so that it doesn’t display? (but presummably keep the screen lit)

anyway - i was looking for a way of setting display sleep and this looks like it
oddly enough, i can’t find any answer on the net for how to close the system preferences and save teh changes. GUI scripting is great for getting it to move things around, but i can’t get it to click the close button

I’ve been struggling with the Screen Saver prefs for a while, and came across this, to toggle between two values (in this case 3 minutes (180 seconds) and off (0)):

set SSIdleTime to do shell script "usr/bin/defaults -currentHost read com.apple.screensaver idleTime"
if SSIdleTime is "0" then
	do shell script "usr/bin/defaults -currentHost write com.apple.screensaver idleTime 180"
else
	do shell script "usr/bin/defaults -currentHost write com.apple.screensaver idleTime 0"
end if
  • apr400
    I am not getting your toggle to work. Any suggestions on how to implement it? This is exactly what I need!

If the screensaver is running at the time the script runs, would it effectively stop the screensaver? Even if not, I am still interested in it’s implementation.

Hi Simpleton,

the script of apr doesn’t stop the screen saver,
but you can stop it with

quit application "ScreenSaverEngine"

That script will not stop the ScreenSaver. a UNIX program called pmset determines screensaver parameters but resetting the preferences file will not alter the pmset settings unless you log out and in again. pmset only works from the preferences pane because closing the pane resets it and updates the pref file.

I guess it’s not surprising everybody wants to stop the screensaver from engaging to keep their scripts running. I too gave scripting the screensaver a shot for the same reason, but I closed my prefences panes with a GUI click command and just like jfowler I found that the screen saver values did not change.

Adam, according to what you just said,

why doesn’t that allow the GUI method work when we close the pane?

StefanK, thanks for the tip. Can you restart “ScreenSaverEngine” again later, or will you have to do a restart on the computer?

For anybody interested, my attempt at shutting down the screensaver was a general one of trying to set the time which presented a problem because the time scale is not linear. So I tried to do it by sliding the slider forward and reading the name of static text 11 to tell where I was. It didn’t work any better than jfowler’s, but it was kinda cool to actually see the slider slide. Also, I just discovered the script has at least one bug - - when the slider is originally set to never, there was an error. I just discovered it and didn’t bother to see what the problem is. Probably trying to read the word ‘never’ as a number maybe. I’ll look at it later.

adjustScreenSaverTime(20)

to adjustScreenSaverTime(myTime)
	-- Independent
	
	(* myTime is in minutes *)
	set validTimes to {3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 30, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 55, 60, 70, 81, 90, 100, 109, 120}
	set n to count of validTimes
	set staticText to "0"
	set highTime to item 1 of validTimes
	if myTime < highTime then set staticText to (highTime as string) & " minutes"
	repeat with i from 2 to n
		set lowTime to item (i - 1) of validTimes
		set highTime to item i of validTimes
		if myTime > lowTime and myTime ≤ highTime then
			set staticText to ""
			set hours to highTime div 60
			if hours = 1 then set staticText to "1 hour"
			if hours = 2 then set staticText to "2 hours"
			set minutes to highTime mod 60
			if hours = 0 then set staticText to (minutes as string) & " minutes"
			if hours = 1 then set staticText to staticText & " " & (minutes as string) & " minutes"
		end if -- myTime > lowTime and myTime ≤ highTime
	end repeat -- with i
	if myTime > highTime then set staticText to "Never"
	set mystaticText to staticText
	
	tell application "Finder"
		activate
		tell application "System Events"
			tell process "Finder"
				click menu item 5 of menu 1 of menu bar item 1 of menu bar 1
			end tell -- process "Finder"
		end tell -- application "System Events"
	end tell -- "Finder
	
	delay 1
	
	tell application "System Preferences"
		activate
		tell application "System Events"
			tell process "System Preferences"
				click button 3 of scroll area 1 of window 1
				delay 1
				tell group 1 of tab group 1 of window 1
					set sliderValue to minimum value of slider 1
					set minSValue to minimum value of slider 1
					set maxSValue to maximum value of slider 1
					set staticText to name of static text 11
					if mystaticText = "Never" then
						set value of slider 1 to maxSValue
					else
						repeat until staticText = mystaticText or sliderValue ≥ maxSValue
							set value of slider 1 to sliderValue
							set staticText to name of static text 11
							set sliderValue to sliderValue + 3
						end repeat
					end if -- mystaticText = "Never"
				end tell -- group 1 of tab group 1 of window 1
				delay 1
				click button 1 of window 1
			end tell -- process "System Preferences"
		end tell -- application "System Events"
	end tell -- "System Preferences
	
	tell application "Script Editor"
		activate
	end tell -- "Script Editor"
	
end adjustScreenSaverTime

Greetings, folks!

Sorry to dredge up an old thread, but I have been working on ways (GUI, mostly) to temporarily kill the screen saver with no luck, and alas, [Saint!] Stefan’s script doesn’t work in OS X 10.10.5. Thoughts?

Blessings, and thank you!

Richard Fairbanks