Skip navigation

HT2488: Mac Basics: Automator

Learn about Mac Basics: Automator

HT2488 Automator to change pages to word

3020 Views 9 Replies Latest reply: Jul 15, 2012 4:01 PM by Jacques Rioux RSS
David52 Calculating status...
Currently Being Moderated
May 25, 2012 5:14 PM

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
  • twtwtw Level 5 Level 5 (4,585 points)
    Currently Being Moderated
    May 25, 2012 5:23 PM (in response to David52)

    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.

  • twtwtw Level 5 Level 5 (4,585 points)
    Currently Being Moderated
    May 25, 2012 6:12 PM (in response to David52)

    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

  • Jacques Rioux Calculating status...
    Currently Being Moderated
    May 26, 2012 7:30 AM (in response to David52)

    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"

  • twtwtw Level 5 Level 5 (4,585 points)
    Currently Being Moderated
    May 26, 2012 7:49 AM (in response to Jacques Rioux)

    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.

  • Jacques Rioux Level 4 Level 4 (1,925 points)
    Currently Being Moderated
    May 26, 2012 9:31 AM (in response to twtwtw)

    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" 

  • Jacques Rioux Level 4 Level 4 (1,925 points)
    Currently Being Moderated
    May 26, 2012 9:49 AM (in response to Jacques Rioux)

    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

  • RJV Bertin Calculating status...
    Currently Being Moderated
    Jul 10, 2012 9:12 AM (in response to Jacques Rioux)

    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
    
    
    MPB 13"/i7@2.7 8Mb, Mac OS X (10.6.8), 12" PillowBook 1.0Ghz 0.7/ext-80Gb; PB15" RIP; dual G5@1.8Ghz
  • Jacques Rioux Level 4 Level 4 (1,925 points)
    Currently Being Moderated
    Jul 15, 2012 4:01 PM (in response to RJV Bertin)

    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"

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.