Automating Pages

KosmicTask Help

Automating Pages

Automating Pages

So we know that Pages is scriptable and we can view its dictionary of commands in the AppleScript Editor dictionary browser. We want to export our document so we search the dictionary for "export" and find nothing. This might lead us to conclude that Pages does not allow us to automate the exporting of documents. However, the first rule of everything is don't give up just yet.

The sorry truth is that the scripting dictionaries for many applications, even major ones like Pages, often lack the sort of informational detail that we really require. This can make them difficult to interpret. Anyhow, we are not alone and search the Internet for "iwork pages applescript export". In this case insight comes from a posting at macscripter.net (which is a good source of user submitted AppleScript solutions). The insight is that the export functionality we seek is actually implemented by the application save command rather than an explicit export command.

Armed with this insight we are able, after some experimentation, to produce the following function based AppleScript:

on KosmicTask(pagesDocFilePath)
  try
    -- we need a path to save our RTF document file into.
    -- the easy way is simply to append .rtf to our existing file path
    set rtfDocFilePath to pagesDocFilePath & ".rtf"

    -- save our pages document as RTF
    tell application "Pages"
      set my doc to open pagesDocFilePath
      save myDoc as "SLDocumentTypeRichText" in rtfDocFilePath
      close myDoc saving no
    end tell

    -- get a result file object from KosmicTask.
    -- the file will be automatically deleted when the task ends.
    tell application "KosmicTask"
      set resultFile to result file with name "result.html"
    end tell

    -- the shell script below will expect a POSIX path
    set posixPath to POSIX path of file resultFile

    -- build our command
    set command to "textutil -convert html -output " & quoted form of posixPath
    set command to command & " " & quoted form of rtfDocFilePath

    -- do command via shell
    do shell script command

    -- return file contents and message
    return {kosmicFile:resultFile, kosmicInfo:"file returned"}

  on error errorMessage number errorNumber
    return {kosmicError:errorMessage}
  end try
end KosmicTask

The major change is telling Pages to open our document and save it as Rich Text. After that our script is pretty much as it was before.

  -- save our pages document as RTF
  tell application "Pages"
    set myDoc to open pagesDocFilePath
    save myDoc as "SLDocumentTypeRichText" in rtfDocFilePath
    close myDoc saving no
  end tell

That's our task complete. If we build and run the task we can successfully convert any Pages document to HTML. Furthermore we can share this task over the network and let other users convert their Pages documents too.

The only real problem that we might have with this task is the AppleScript language itself. AppleScript makes simple automation tasks relatively easy to accomplish but the syntax is entirely idiosyncratic and often non intuitive. The language is also rather dated and unsupported on other platforms. Indeed, programmers often used AppleScript despite its obvious deficiencies simply because it was the only way to achieve certain sorts of automation. Thankfully there are now other powerful ways of automating applications on the mac. So let's rework our task again, this time in something a lot more popular and contemporary, Ruby.