As with all the iWork applications, Pages doesn’t save files automatically, so it’s AppleScript to the rescue again to protect you from lost work…
It’s one of the near-irrefutable properties of Sod’s law that when disasters happen, they happen at the worst possible time.
So it was that there was a level of inevitability surrounding the chain of events that began one Saturday morning with me successfully persuading someone to buy iWork instead of Microsoft Office, and would end a few days later with his first application crash and the complete loss of an afternoon’s worth of carefully crafted text in Pages, which had gone unsaved.
‘You didn’t warn me that Pages wouldn’t automatically save my work,’ he wailed. ‘I’d never have had that problem if I’d stuck with Word.’
While I would have liked to protect my integrity by pointing out that Word doesn’t have an automatic save function either, I thought it would be more astute to let that pass. However, for the record, if he’s reading this, it has an autorecovery function, which is triggered when Word quits unexpectedly but which doesn’t automatically save documents in the background. And I didn’t make the point I was dying to make, which was that this autorecovery feature, which pieces bits of work together since the document was last saved, produces results that frustrate as many times as they save your bacon.
Still, his broader point hit home. It does seem strange that Pages – or Numbers or Keynote for that matter – has no way of protecting you against lost work. I can understand that there are some strong arguments against incorporating an automatic saving routine, as in some cases it can be dangerous. For example, if you were to accidentally delete large chunks of a document, there’s a risk that an automatic save function could kick in and overwrite your document with no hope of recovering the overwritten data. However, other word processing applications have solved this problem. Nisus Writer (nisus.com) will automatically save your work at user-defined intervals and keep document backups with user-defined suffixes.
However, despite an ongoing clamour for an automatic save function, so far Apple hasn’t delivered. Still, I’ve found that in those few areas where Pages falls down against its rivals, AppleScript comes to the rescue. A couple of months ago in this column, I detailed how to automatically save Pages documents in a Word-compatible format, and you can use AppleScript again to add a rudimentary autosave feature to Pages. To add this to Pages, just enter the following short script in the Mac OS X AppleScript Editor application:
on run
display dialog “Now automatically saving Pages.”
end run
on idle
tell application “System Events”
set t to count (every process whose name is “Pages”)
if t > 0 then
tell application “Pages”
if path of document 1 exists then
save document 1
else
display dialog “This document has not been saved” buttons {“Save”, “Cancel”} default button “Save”
if button returned of result is “Save” then
set myFile to choose file name with prompt “Save file as:”
save document 1 in myFile
end if
end if
end tell
end if
end tell
return 600
end idle
on quit
save document 1
quit application “Pages”
continue quit
end quit
When that script is saved as an application and run, it first displays a dialog box to show that the autosave script is running – you could, of course, replace this step with another instruction, such as launching the application.
Once it’s running, the script checks with the operating system whether Pages is active by counting the number of processes called ‘Pages’ running. This will only return a value of 0 if Pages isn’t running; if that’s the case, the script takes no further action. However, if Pages is up and running, the script checks to see if it has been saved before. It does this by checking whether it has an established file path. If it has such a property, the document has been saved before and, in that case, the script sends a simple ‘save’ command to save it again. If the file is untitled and hasn’t been saved before, the script triggers a dialog inviting the user to name and save the file in a chosen location. Once this is done, the application will continue to save the file at defined intervals.
To make sure the script runs repeatedly rather than just once, it sits between ‘on idle’ and ‘end idle’ statements). By default, an idle handler will trigger automatically and runs the steps within its handler every 30 seconds. That’s probably overkill in our case and would result in the script being run almost constantly while Pages is open. So instead I’ve customised the delay to a longer period by returning a different number (in this case, 600), which means the script will only execute every 600 seconds, or 10 minutes. Obviously, you can substitute any interval that you want here.
The important thing to remember about idle handlers is that they only work in scripts that have been defined as ‘stay open’ applications. So when you come to save the script, ensure you choose ‘Application’ as the file format and the ‘Stay Open’ checkbox below it is checked in the Save dialog box.
The final part of the script, between the ‘on quit’ and ‘end quit’ statements, makes sure that when you quit the script application, the active document is saved and then Pages itself quits.
As the script is saved as an application, it’s launched by double-clicking its icon. To have it protecting a Pages document at all times, you’ll need to have the script running constantly. The easiest way to do this is to add it to your ‘Login Items’ in your Accounts System Preferences pane.
I make no claims that the script I’ve put together is a work of art. It works effectively, but there are several ways to improve it. For example, as it stands the script saves the document automatically, but it would be useful to have a versioning ability to ensure that you don’t inadvertently overwrite over a previous version that you want to keep.
Furthermore, at the moment, it only checks the foremost document to see if it has been saved, although it would be relatively easy to add a step that cycles through all open Pages documents and does the same thing. It’s also possible to tweak the script slightly so that it works with other iWork applications.
It’s perhaps reassuring to some that AppleScript isn’t the only option for adding automatic save functionality to iWork applications. If you don’t want to get your hands dirty with coding, there are applications that will do the hard work for you. My favourite ForeverSave Lite (tool-forcesw.com/foreversave-lite), a free program that adds an automatic save function to just about any Mac OS X application. You can set up automatic saves at user-specified intervals or when you switch to another application. A Pro version of the software – which still costs under a tenner – also includes backup and versioning features, so you can store multiple copies of the document.
I suspect a copy of that will be winging its way to a certain person very soon.














