GUI script Safari when screen is locked?

Can you GUI script an app when your screen is off (account locked, of course, but computer is still on)?

I have a GUI-script based AppleScript to print to PDF from Safari (below). I really like the way that Safari renders PDFs (on Mac, especially in reader view).

I use a version of this script to render PDFs while I am actively working on my Mac. The version below, however, is intended to render PDFs while I am away from my Mac, and the computer is locked with the screen off. I send a text file containing a URL to a folder monitored by Hazel, which reads the URL to the script and kicks it off.

This all works well when I am testing/monitoring the script. But, when my Mac is unattended and the screen is off (I never put my Mac to sleep), the script doesn’t do anything… no PDF is produced.

Before I interrogate other avenues of my approach, I wonder… is it possible to control the GUI of an app via AppleScript screen is off?

set SafariURL to read file theFile
set SaveFolderPath to "~/Desktop/"
tell application "Safari"
	activate
	make new document at front with properties {URL:SafariURL}
	tell application "System Events"
		tell process "Safari"
			repeat while exists button "Stop loading this page" of group 3 of toolbar 1 of window 1
				delay 0.02
			end repeat
			try
				click menu item "Show Reader" of menu "View" of menu bar 1
			end try
			delay 3
			click menu item "Print…" of menu "File" of menu bar 1
			repeat until exists sheet 1 of window 1
				
			end repeat
			tell splitter group 1 of sheet 1 of window 1
				click menu button "PDF"
				click menu item "Save as PDF" of menu 1 of menu button "PDF"
			end tell
			
			repeat until exists button "Save" of sheet 1 of sheet 1 of window 1
				delay 0.02
			end repeat
			keystroke "g" using {command down, shift down}
			repeat until exists combo box 1 of sheet 1 of sheet 1 of sheet 1 of window 1
				delay 0.02
			end repeat
			tell sheet 1 of sheet 1 of sheet 1 of window 1
				set value of combo box 1 to SaveFolderPath
				click button "Go"
			end tell
			click button "Save" of sheet 1 of sheet 1 of window 1
			delay 0.5
			repeat while exists button "Cancel" of sheet 1 of window 1
				delay 0.2
			end repeat
		end tell
	end tell
	set winlist to count of every window
	repeat with i from 1 to winlist
		close (every tab of window i whose URL = SafariURL)
	end repeat
end tell

Go System Preferences → Desktop & Screensavers, set Start after to Never

:rolleyes: I want my screensaver more than to make this specific solution work.

Are you saying GUI scripting cannot work with a screensaver on?

GUI scripting sends commands to what is on front current time. So when screensaver pop up, your commands go to screensaver instead of correct application.

Download Caffeine for Mac (free). Give it permission to control your computer. Then add to start of your script tell application “Caffeine” to turn on, and to end of your script tell application “Caffeine” to turn off

Thank you to those who replied, but I edited my question to avoid confusion. Stopping the normal course of my screen going off and account locking (regardless of whether or not the screensaver is on) is not a solution to my problem.

No, without waking up the computer’s screen.
Yes, after waking up.

Add following to top of your script:


set thePassword to "********" -- current user password

tell application "System Events" to tell screen saver preferences to if its running then
	stop
	delay 1
	keystroke thePassword & return
end if