Automatically Reload Web Pages

  1. Create a news screen
  2. Simple Scripts
  3. Talking Clock

If you’ve paid attention to the “Save” screen for your scripts, there is an option which by default is unchecked called “Stay Open”. If you check this, your script will not quit when it is done. It will stay open, and can continue working.

AppleScripts work by sending and receiving “messages”. When we “told” Safari to make new documents and open URLs, our script sent the appropriate messages from our script to Safari.

If our script stays open, the system will also send it messages. Whenever an application “quits”, the system sends it a “quit” message. Our script gets that also (it automatically knows what to do when it receives that message: it quits). Another message our script receives is the “idle” message. The script receives the idle message when it isn’t doing anything.

Add the following lines to your “Open News Overview”:

on idle

tell application "Safari"

repeat with overviewWindow in every window

tell document of overviewWindow

set URL to URL

end tell

end repeat

end tell

return 60

end idle

This is what is called a “handler” in AppleScript. It “handles” messages. When you handle a message, you can send a return message. In this case, we’re telling the system that we’re done with our idle, and we would like to have another idle message in 60 seconds (“return 60”). By default, idle times are in 30-second increments. The first idle message is sent immediately after the script first goes idle in order to set the idle time to a different default.

This just repeats through every window of Safari and tells the document in that window to reload its URL (“set URL to URL”).

Automatically Reload Web Pages: Shell Tell

You’ll notice that we have two tells, one inside of the other. First, we tell Safari to do something, and inside of that we are telling one of Safari’s documents to do something.

Repetitive Tasks

One of the things you can use scripts for is to automate boring and repetitive tasks. One of the features that AppleScript has to make it easy to automate repetitive tasks is the “repeat” structure. (Other scripting languages might call it “while”, “for”, or “foreach”, among other names.)

Everything between “repeat” and “end repeat” will be repeated until our instructions are done. We told it to repeat “with overviewWindow in every window”. Within that repeat “block”, the variable “overviewWindow” will start with Safari’s first window the first time through, and then repeat for each of Safari’s windows, until there are no more windows left.

We could have done the same thing by typing those lines between the “repeat” four times, as we did when we opened the windows to load the web pages. But this makes it a lot easier. And it means that if we later add a fifth web page (we’ll need an awfully big monitor) we won’t need to change this part of the script at all.

  1. Create a news screen
  2. Simple Scripts
  3. Talking Clock