Script behaves strangely in wee hours of the morning

Hello again! :slight_smile:

I have a script that opens up all my daily web comics in Safari for me to read. I don’t have the script on me (unfortunately), but basically it opens up a new Safari window, reads the date, and then goes through and for each web comic I have in the code whether to open it on which days of the week (using a string compare to the date).

To open each page, I call a user function which uses UI scripting to open a new tab in the current window and set the location.

Now normally, this script works just fine - every time I run it myself, I get exactly what I am expecting (one window with many tabs).

I have set it to run right before I get up in the morning using Cron, so that it will be fully loaded when I get to my computer. However, whenever it runs like this, it seems to sort of, but not quite, actually work. Every morning I get a new Safari window, usually with one random tab in it. Some days I get 2 or even 3 tabs in this window. This morning, I got 3 safari windows, each with one or two of my tabs.

I have set the computer never to go to sleep - not the computer, hard drive, or screen. All that happens is the screen saver kicks in. I have set up the Crontab so that my script runs at least an hour after all of the maintenance scripts run (mine starts at 5:30am, IIRC, and I adjusted the times of the other maintenance stuff).

Any ideas why this is working so funkily in the night, and not when I run it?

Oh, and when I set up crontab to run it in the next few minutes while I am watching, it also works fine. :rolleyes:


Set Cron to run the script in 1 minute from now. Activate the screen saver. What happened when the script ran?


Yep, it misbehaves. So I guess the question is, what is the Applescript command to stop the screensaver? Is there a “jiggle mouse” command? :wink:

Enter this script and then launch the screen saver:

delay 10

tell application "System Events"
	set currApp to the name of the first process whose frontmost is true
end tell

display dialog "You were in " & currApp

When you exit the screensaver and go back to the script editor, it should tell you the frontmost process was “ScreenSaverEngine”

It might be possible to stop this process before running your script. Your best bet is to GREP for the PID of the screensaver and kill -9 it via the script.

You should be able to come up with a SHELL SCRIPT you can run to kill it.

launch the screensaver in the background:

/System/Library/Frameworks/ScreenSaver.framework/Versions/A/Resources/ -background &

then use:


PID=`ps ax | grep ScreenSaverEngine | grep -v grep | awk '{print $1}'`
if [ $PID -gt 0 ]; then
    PID=`ps ax | grep ScreenSaverEngine | grep -v grep | awk '{print $1}'`
    kill -9 $PID
    echo "No screensaver running..."


Building on the previous code, I find this works to kill the screensaver in 10.3.1:


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Killing the screensaver process with that code works, but all it leaves behind is a black screen that still impedes the script progress. :wink:

I’ve figured out that just telling “System Events” to keystroke return will do the trick, though.

(actually, right now I’ve got it embedded in a “tell Finder” which is embedded in a “tell System Events” since I copied this code from elsewherre, but I’m not sure if stripping those two extra tell blocks will cause it to stop working or not.)

Thanks for your help!

Hey, that sounds really interesting. Where can I find out about this “System Events” thing? I thought I might have to go to QuicKeys to be able to trigger keystrokes. Is it an OSAX or part of one?

Thanks for any info.


You can find out about ui scripting here: GUI Scripting

It can be hit and miss, though.

You should also check out UI Browser from PreFab:
UI Browser

It will make discovering ui elements and writing AS much easier.

Dennis- Great references. Thanks a lot. --Jim