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