9 Replies Latest reply: Jul 15, 2012 4:01 PM by Jacques Rioux
David52 Level 1 Level 1 (10 points)

I have very many (350) pages documents existing in a set of nested folders

anmd i would like to change them all to word documents (as the person I need

to send these to has a PC and Office, not Pages)

 

How to do? I have never used automator before


powerbook 1.5GHz, Mac Mini core Duo, LaCie 250GB rugged ext. HD, Mac OS X (10.6.7), Time Capsule 1TB, AEX, iMac 27" 3.06 core 2 duo
  • 1. Re: Automator to change pages to word
    twtwtw Level 5 Level 5 (4,690 points)

    what kind of documents are ou starting with?  are they text files, rich text, or Pages documents with complex formatting?  it makes a difference in how to approach the task.

  • 2. Re: Automator to change pages to word
    David52 Level 1 Level 1 (10 points)

    Rich text, with a very few photos (so few, in fact, that I could afford to lose them)---they are recipe

    collections taken from different sources (in AppleWorks, no less, then converted to Pages)

     

    I don't know how they were made, it is a collection I have been given to "curate" and make accesible

    to different people with different systems.

  • 3. Re: Automator to change pages to word
    twtwtw Level 5 Level 5 (4,690 points)

    well, a quick google turned up this thread.  I've tweaked the applescript I found there a bit.  Open the applescript editor, paste in the following code, and save it as an application.  It will create a new droplet app; drag and drop your files onto it and they'll be converted.  You're going to have to let it work. Pages needs to be frontmost while the script runs, which means you won't be able to do anything else until it's done.

     

    I suggest you convert them to pdf files rather than word files (I've set it up to do pdf by default).  Everyone can open pdfs (not everyone has Office installed on their machine), pdfs will deal with the pictures and formatting properly, and (unlike Word) pdfs can't be edited easily.

     

    on open fileList

      -- change this line to the extension of whatever output file you want. 

              set newFileExt to "pdf"

      -- change this line to point to the folder you want output files to go to.  preset to desktop folder. 

              set savePath to path to desktop folder as text

     

              repeat with thisFile in fileList

                        set thisFile to thisFile as text

                        tell application "System Events" to tell disk item thisFile

                                  set {fileName, fileExt} to {get name, get name extension}

                        end tell

                        set newName to text 1 thru -(1 + (count of fileExt)) of fileName

                        tell application "Pages"

                                  activate

      -- tricky: apparently Pages is smart enough to create the correct file type based on the extension

                                  set newFileName to savePath & newName & newFileExt

                                  set x to make new document with properties {path:newFileName}

                                  open thisFile

                                  save document 1 in file newFileName

                                  close every window saving no

                        end tell

              end repeat

    end open

  • 4. Re: Automator to change pages to word
    Jacques Rioux Level 4 Level 4 (1,960 points)

    Hi,

     

    You can save as (PDF or Word) without (make new document) , Pages can remain in the background

     

    --------------------------------------------------------

    on open fileList

         -- change this line to point to the folder you want output files to go to.  preset to desktop folder.

         set savePath to path to desktop folder as text

         tell application "Pages" -- iWork 09

              repeat with thisFile in fileList

                   try

                        tell (open thisFile)

                             set newName to my getDocName(get name)

                             save as "SLDocumentTypePDF" in file (savePath & newName) -- Pages automatically adds the extension

                             close saving no

                        end tell

                   end try

              end repeat

         end tell

    end open

     

    on getDocName(t)

         if t ends with ".pages" then return text 1 thru -7 of t

         return t

    end getDocName

    --------------------------------------------------------

     

    If you want to save as Word document,  replace in the script  "SLDocumentTypePDF" by "Microsoft Word 97 - 2004 document"

  • 5. Re: Automator to change pages to word
    twtwtw Level 5 Level 5 (4,690 points)

    Jacques - nice.  Just out of curiosity, where is the SLDocumentTypePDF keyword documented?  I found another help page that lists out the keywords for pdf, doc, plain text, rich text, and a rich text bundle (SLDocumentTypePDF, SLDocumentTypeMSWord, SLDocumentTypePlainText, SLDocumentTypeRichText, and SLDocumentTypeRichTextBundle), but I don't know if that's the complete list.

  • 6. Re: Automator to change pages to word
    Jacques Rioux Level 4 Level 4 (1,960 points)

    You can get this information in the file "Info.plist" of the application.

    In property "Document types" --> Item x -->  Document Type Name.

     

    This varies depending on the version of "iWork" 

  • 7. Re: Automator to change pages to word
    Jacques Rioux Level 4 Level 4 (1,960 points)

    edit :

     

    but only the types that can export, not the types that can open

    example (iWork 09)  :"Microsoft Word document" and "SLDocumentTypeAppleWorks" doesn't works, "Microsoft Word 97 - 2004 document" work

  • 8. Re: Automator to change pages to word
    RJV Bertin Level 3 Level 3 (950 points)

    For some reason, this approach doesn't (always?) work for me, so I combined both approaches above, and added in a bit of logic to make both files share the same modification date. Oh, and each file will be exported to the folder it is in.

     

     

    on open fileList
          -- change this line to point to the folder you want output files to go to.  preset to desktop folder.
          -- set savePath to path to desktop folder as text
              tell application "Pages" -- iWork 09
                        repeat with thisFile in fileList
                                  set curName to name of (info for thisFile) as text
                                  set savePath to my dirName(thisFile, true) as text
                                  set newName to my getDocName(savePath & curName)
                                  set curName to (savePath & curName)
                                  set exportFile to newName
                                  set openDocument to open thisFile
                                  try
                                            tell openDocument
                                       save as "SLDocumentTypeMSWord" in file exportFile -- Pages automatically adds the extension
                                       close saving no
                                            end tell
                                  on error error_message number error_number
                                    --display dialog "Error saving " & exportFile & return & return & error_message buttons {"ok"} default button 1
                                    -- tricky: apparently Pages is smart enough to create the correct file type based on the extension
                                            set exportFile to exportFile & ".doc"
                                            set x to make new document with properties {path:exportFile}
                                            try
                                         save openDocument in file exportFile
                                            on error error_message number error_number
                                         display dialog "Error saving " & exportFile & return & return & error_message buttons {"Cancel"} default button 1
                                            end try
                                    close x saving no
                                    close openDocument saving no
                                    -- get the POSIX paths for the 2 files:
                                            set exportFile to POSIX path of exportFile
                                            set curName to POSIX path of curName
                                    -- construct and execute a command to make the 2 files share the same modification date.
                                    do shell script ("sh -c 'touch -r " & curName & " " & exportFile & "'")
                                  end try
                        end repeat
              end tell
    end open
    
    
    
    on getDocName(t)
              if t ends with ".pages:" then return text 1 thru -8 of t
              if t ends with ".pages" then return text 1 thru -7 of t
              return "bla"
    end getDocName
    
    
    
    on dirName(thePath, wantPath)
              set thePath to (thePath as text)
              if thePath contains ":" then
                        set pathDelim to ":"
              else
                        set pathDelim to "/"
              end if
              set saveDelim to AppleScript's text item delimiters
              set AppleScript's text item delimiters to {pathDelim}
              set pathAsList to text items of thePath
              if the last character of thePath is pathDelim then
                        set idx to (the number of text items in thePath) - 2
              else
                        set idx to -2
              end if
              if wantPath then
                        set folderName to ((text items 1 through idx of pathAsList) as text) & pathDelim
              else
                        set folderName to item idx of pathAsList
              end if
              set AppleScript's text item delimiters to saveDelim
              return folderName
    end dirName
    
    
  • 9. Re: Automator to change pages to word
    Jacques Rioux Level 4 Level 4 (1,960 points)

    Hi,

     

    RJV Bertin wrote:

     

    For some reason, this approach doesn't (always?) work for me

                                  try
                                            tell openDocument
                                       save as "SLDocumentTypeMSWord" in file exportFile -- Pages automatically adds the extension
                                       close saving no
                               
    

     

    For sure it never works.  I had already said in my previous message..

    You must use --> save as "Word 97-2004 document"