title: Applescript: GUI Programming for recursively printing to PDF for Appleworks
body: <p><strong>The Goal:</strong> thousands of old Clarisworks and Appleworks documents need to become PDFs.</p>
  
  <p><strong>The Problem:</strong> Scripting Pages is not an option; it doesn't preserve formatting reliably. <em>It must be Appleworks</em>. This script works, when it works; but it skips files for reasons I cannot identify. </p>
  
  <pre><code>set appleworksFolder to choose folder
  tell application "Finder"
      set folderItems to (files of entire contents of appleworksFolder)
      repeat with I from 1 to number of items in folderItems
  
          set the_doc to item I of folderItems
          set doc_name to name of the_doc as text
  
              (* Some files are missing metatags and try to save as "Untitled Document",
                this block ensures a file name is unique, later *)
  
              tell application "Finder"
              set the clipboard to doc_name &amp; ".pdf"
          end tell
  
  
              (* Each file exists in a folder with a path.txt file that will later
                 be used to put the file back where it was originally stored prior
                 to this conversion process *)
  
          if name of the_doc is not "path.txt" then
              try
                  tell application "Finder"
  
              (* Many files no longer have name extensions and appear as UNIX
                 executables if not repaired *)
  
                      try
                          set nmex to name extension of the_doc as text
                      on error
                          set nmex to "ok"
                      end try
  
                      if nmex is not "cwk" or "CWK" then
                          set the_doc_str to the_doc as text
                          set doc_path to POSIX path of the_doc_str
                          do shell script "mv '" &amp; doc_path &amp; "' " &amp; "'" &amp; doc_path &amp; ".cwk'"
                      end if
                      delay 1
  
  
              (* In case Appleworks hangs or otherwise bungs up, I force-quit
                 it at the end of the script; this ensures its closed before 
                 it tries to open the next file *)
  
                      if (application process "Appleworks 6" of application "System Events" exists) then
                          do shell script "killall 'LaunchCFMApp'"
                          delay 1
                      end if
  
                      tell application "AppleWorks 6"
                          open the_doc
                      end tell
  
              (* Some of the documents are huge, this delay gives the app time to load
                  since this is all GUI-scripted *)
  
                      delay 5
                      tell application process "Appleworks 6" of application "System Events"
  
              (* This is where I think I am encountering problems; there are two 
                 possible warnings that may or may not appear on opening the doc;
                 that Appleworks needs to append a version number to the file (if
                 its old) or that the file may be damaged and thus would need to be
                 skipped. I get system beeps sometimes in this part, but I don't know
                 why! *)
  
                          if (button "OK" of window 1 of application process "AppleWorks 6" of application "System Events" exists) then
                              delay 0.5
                              keystroke return
                              delay 0.5
                          end if
                          delay 2
                          if (button "OK" of window 1 of application process "AppleWorks 6" of application "System Events" exists) then
                              delay 0.5
                              keystroke return
                              delay 0.5
                          end if
                          delay 2
              (* If the document loads, the Appleworks welcome pane won't be there;
                 this part of the script works flawlessly, when it happens. Sometimes
                 documents are outside of print margins, hence the press-ok-button 
                 conditional *)
  
                          if not (window "Starting Points" of application process "AppleWorks 6" of application "System Events" exists) then
                              tell application process "Appleworks 6" of application "System Events"
                                  keystroke "p" using command down
                                  delay 1
                                  click menu button "PDF" of window "Print" of application process "AppleWorks 6" of application "System Events"
                                  delay 1
                                  click menu item "Save as PDF…" of menu "PDF" of menu button "PDF" of window "Print" of application process "AppleWorks 6" of application "System Events"
                                  delay 1
                                  keystroke "v" using command down
                                  click button "Save" of window "Save" of application process "AppleWorks 6" of application "System Events"
                                  delay 8
                                  keystroke "w" using command down
                                  delay 0.5
                                  if (button 1 of window 1 of application process "AppleWorks 6" of application "System Events" exists) then
                                      delay 0.5
                                      keystroke "d" using command down
                                      delay 0.5
                                  end if
                                  delay 0.5
                              end tell
                          end if
                          do shell script "killall 'LaunchCFMApp'"
                          delay 3
                      end tell
                  end tell
              end try
          end if
      end repeat
  end tell
  </code></pre>
  
  <p>I want to this baby to just run through a couple of thousands files over a weekend and create PDFs, but each time I run it overnight I find a few hundred correctly processed documents, hundreds or thousands of skipped documents, and often a print dialogue for <em>the script itself</em>, which obviously comes from using Command+P outside of an Appleworks context. I'm an Applescript noob, to be sure, this has been driving me nuts for weeks!</p>