If you have Pages 3.0 and Mac OS X 10.5 (Leopard) create a new script and paste the following code:
on open theFiles
tell application "Pages"
repeat with aFile in theFiles
open aFile
set docName to name of front document
-- Remove .pages extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".pages"
-- Add .doc extension.
set docName to first text item of docName & ".doc"
set AppleScript's text item delimiters to prevTIDs
-- Save file to Desktop.
set docPathAndName to (path to desktop as string) & docName
save front document as "SLDocumentTypeMSWord" in docPathAndName
close front document
end repeat
end tell
end open
If you instead have Pages 2.0 and Mac OS X 10.4 (Tiger) create a new script and paste the following code:
on open theFiles
tell application "Pages"
repeat with aFile in theFiles
open aFile
set docName to name of front document
-- Remove .pages extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".pages"
-- Add .doc extension.
set docName to first text item of docName & ".doc"
set AppleScript's text item delimiters to prevTIDs
-- Save file to Desktop.
set docPathAndName to (path to desktop as string) & docName
save front document as ".doc" in docPathAndName
close front document
end repeat
end tell
end open
In both cases, save the script as an "Application".
Drag your 1000 Pages documents to the new file.
Sit back and wait for all the files to be processed.
Part of this code written by Dale Gillard of dmg software. Thanks Dale!
In case you wonder where the document type "SLDocumentTypeMSWord" is defined, it can be found in the Pages package Contents/Info.plist. Look for the node CFBundleDocumentTypes and number 6. The other numbers refer to other document types you also can use in this kind of script.
In case you want to convert AppleWorks documents to Pages, Yvan Koenig published an AppleScript to do just that at Apple's discussion forums.
In case you want to convert to PDF instead of MS Word, you can easily change the scripts above to do just that. If you are on Mac OS X 10.4, replace ".doc" with ".pdf". If you are on Mac OS X 10.5, replace "SLDocumentTypeMSWord" with "SLDocumentTypePDF". You can also use this script by Yvan Koenig.
If you want to export all files in a folder back to the same folder can also use the script by Yvan Koenig.
31 comments:
I would like to know how to code it so it would save the docs in the same folders they originally resided.
Thanks :)
Yes, that would be useful. Any reader interested in adding that?
Hey, So I tried the script and could not get it working with Pages 08. So I have modified it to work now and added several enhancements: 1) Now it prompts you to choose a folder to save all exported files, 2) allows you to export in all formats that pages supports, and 3) adds some key error checking.
The script is pasted below:
----
on open theFiles
tell application "System Events"
if process "Pages" exists then
display dialog "whoops, please close Pages before running droplet!"
end if
end tell
tell application "Pages"
activate
delay 1
close front document
set theList to {"doc", "rtf", "pdf", "txt"}
set theType to (choose from list theList OK button name "Select" with title "Pages Export" with prompt "Choose one or more formats to export using Pages" with multiple selections allowed) as text item
set s to theType as string
set theLocation to choose folder
set theLocation to theLocation as string
repeat with aFile in theFiles
open aFile
if theType contains "doc" then
set asType to ".doc"
set docName to name of front document
-- Remove .pages extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".pages"
-- Add .doc extension.
set docNameNew to first text item of docName & asType
set AppleScript's text item delimiters to prevTIDs
-- Save file to Desktop.
set docPathAndName to theLocation & docNameNew
save front document as asType in docPathAndName
end if
if theType contains "rtf" then
set asType to ".rtf"
set docName to name of front document
-- Remove .pages extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".pages"
-- Add .doc extension.
set docNameNew to first text item of docName & asType
set AppleScript's text item delimiters to prevTIDs
-- Save file to Desktop.
set docPathAndName to theLocation & docNameNew
save front document as asType in docPathAndName
end if
if theType contains "pdf" then
set asType to ".pdf"
set docName to name of front document
-- Remove .pages extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".pages"
-- Add .doc extension.
set docNameNew to first text item of docName & asType
set AppleScript's text item delimiters to prevTIDs
-- Save file to Desktop.
set docPathAndName to theLocation & docNameNew
set s to save front document as asType in docPathAndName
end if
if theType contains "txt" then
set asType to ".txt"
set docName to name of front document
-- Remove .pages extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".pages"
-- Add .doc extension.
set docNameNew to first text item of docName & asType
set AppleScript's text item delimiters to prevTIDs
-- Save file to User Specified Location
set docPathAndName to theLocation & docNameNew
save front document as asType in docPathAndName
end if
try
close front document saving no
end try
end repeat
quit
end tell
end open
----
Thanks for pointing that out, Dane! Unfortunately I cannot get your script to work either. Just as with the original script, it complains that it cannot save the document in .doc format. I wonder why...
Hi Magnus,
Hmm. Well, in case something is happening in the copy and paste you could download the zipped droplet here: http://danespringmeyer.com/opendatashare/pages2word.zip
What version of OS X and pages are you using?
One helpful way to debug the applescript is to open it in script editor, comment out the "On open" and "End open" lines at start and finish and then you can run the thing from within script editor as a normal app rather than droplet. That way it is easier to see where an error takes place.
Well, I just tried the previous script and it actually does work for me. It's just that the script was getting held up by pages 8 asking if I wished to save the document...
Why do you think it is not working for you?
Dane
Oh, I see from above that you may be running Leopard? 10.5? I have things working in 10.4.10 with documents created in Pages 06 being batch converted with these scripts using Page 08.
ps. this script works better if you have the default templates pop-up disabled in pages 08...
dane
Very strange. So we both have Pages 3.0 but I have Mac OS X 10.5 and you 10.4.10.
And as far as I understand from your later posts, you could get even the original script to work in the end?
Then it seems the issue is with Leopard, but I have no idea how that cat can affect this kind of things.
The message is simply "The document cannot be exported to the ".doc" format.
But why would a change of OS affect that?
Okay to be exact I am running pages 3.0.1 (though you likely are as well).
In the modified script that I posted there are options to save to pdf, rtf, and text as well. Do those work for you or do they return the same error?
It seems probable that the script could be uncomptatible with leopard, since in 10.5 applescript has gone 2.0 and seen a number of changes. See here for the overall new thinking: http://www.apple.com/applescript/resources.html and here for all the details: http://developer.apple.com/releasenotes/AppleScript/RN-AppleScript/index.html
Thanks for your help, Dane. It turned out that the document type name had changed, as described in the newly edited post above.
This script would be a lifesaver! If I could only get it to work...
I tried to find the new document file type for the new applescript, but could not. I have 10.5 and pages '08 and get a different error--"invalid file type index.xml is missing"
Any suggestions?
C Willis, I know there is no error handling in the scripts, but it seems your problem is due to some document corruption. Can you export it manually?
Thanks, Magnus
I can export the documents manually. When I run the script it goes through the steps of choosing a file type, choosing a destination folder, then it hangs and a window pops up that says "Unable to open "[name of folder]" because the document is invalid. The index.xml file is missing. I have tried it with different source folders, and get the same results.
When I try it with a single .pages document, it runs through all the steps, but then gives an error window that reads " The document cannot be exported to the ".doc" format.
Any help would be appreciated! Thanks.
C Willis, this may be a stupid question, but you said above that you were on 10.5 and '08? Are you sure you use the right version of the script? It seems very strange that it would complain about ".doc" as file type, as ".doc" is not mentioned as file type in the 10.5/'08 script.
I don't think that's a stupid question! In all probability I am using the incorrect script! I am running 10.5.1 with Pages "08. I am using the script posted by dane found at the top of this post. Did I somehow miss the updated one??
One updated script is the very first one in my original post. Dane's script only works in 10.4.x, but it has some nice additional functionality.
Either use the original script or change Dane's script replacing ".doc" with "SLDocumentTypeMSWord", wherever it appears. There may be more things to do with it, but you need to change at least that.
Magnus, Thanks for your help. The problem was two-fold: 1. I was using the wrong script--I was using the one for 10.4 and not the updated one for 10.5. 2. I was dragging the folder containing the documents I wanted to convert onto the script app. and not the files themselves.
Now everything is working as it ought to. Thank you, again, for your help.
I modified the script to make it work with PAges 3 and Leopard... Could not figure out how to export to rtf and txt, so it's limited to PDF and DOC, which is fine at least for me :-)
Enjoy!
on open theFiles
tell application "System Events"
if process "Pages" exists then
display dialog "whoops, please close Pages before running droplet!"
end if
end tell
tell application "Pages"
activate
delay 1
close front document
set theList to {"doc", "pdf"}
set theType to (choose from list theList OK button name "Select" with title "Pages Export" with prompt "Choose one or more formats to export using Pages" with multiple selections allowed) as text item
set s to theType as string
set theLocation to choose folder
set theLocation to theLocation as string
repeat with aFile in theFiles
open aFile
if theType contains "doc" then
set asType to "SLDocumentTypeMSWord"
set docName to name of front document
-- Remove .pages extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".pages"
-- Add .doc extension.
set docNameNew to first text item of docName & ".doc"
set AppleScript's text item delimiters to prevTIDs
-- Save file to Desktop.
set docPathAndName to theLocation & docNameNew
save front document as asType in docPathAndName
end if
if theType contains "pdf" then
set asType to "SLDocumentTypePDF"
set docName to name of front document
-- Remove .pages extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".pages"
-- Add .doc extension.
set docNameNew to first text item of docName & ".pdf"
set AppleScript's text item delimiters to prevTIDs
-- Save file to Desktop.
set docPathAndName to theLocation & docNameNew
set s to save front document as asType in docPathAndName
end if
try
close front document saving no
end try
end repeat
quit
end tell
end open
I fixed it!!! Finally it works for the most recent OS and Pages for all of the file types. Here it is!!!
on open theFiles
tell application "System Events"
if process "Pages" exists then
display dialog "whoops, please close Pages before running droplet!"
end if
end tell
tell application "Pages"
activate
delay 1
close front document
set theList to {"doc", "rtf", "pdf", "txt"}
set theType to (choose from list theList OK button name "Select" with title "Pages Export" with prompt "Choose one or more formats to export using Pages" with multiple selections allowed) as text item
set s to theType as string
set theLocation to choose folder
set theLocation to theLocation as string
repeat with aFile in theFiles
open aFile
if theType contains "doc" then
set asType to "SLDocumentTypeRichText"
set docName to name of front document
-- Remove .pages extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".pages"
-- Add .doc extension.
set docNameNew to first text item of docName & ".doc"
set AppleScript's text item delimiters to prevTIDs
-- Save file to Desktop.
set docPathAndName to theLocation & docNameNew
save front document as asType in docPathAndName
end if
if theType contains "rtf" then
set asType to "SLDocumentTypeRichText"
set docName to name of front document
-- Remove .pages extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".pages"
-- Add .doc extension.
set docNameNew to first text item of docName & ".rtf"
set AppleScript's text item delimiters to prevTIDs
-- Save file to Desktop.
set docPathAndName to theLocation & docNameNew
save front document as asType in docPathAndName
end if
if theType contains "pdf" then
set asType to "SLDocumentTypePDF"
set docName to name of front document
-- Remove .pages extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".pages"
-- Add .doc extension.
set docNameNew to first text item of docName & ".pdf"
set AppleScript's text item delimiters to prevTIDs
-- Save file to Desktop.
set docPathAndName to theLocation & docNameNew
set s to save front document as asType in docPathAndName
end if
if theType contains "txt" then
set asType to "SLDocumentTypePlainText"
set docName to name of front document
-- Remove .pages extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".pages"
-- Add .doc extension.
set docNameNew to first text item of docName & ".txt"
set AppleScript's text item delimiters to prevTIDs
-- Save file to User Specified Location
set docPathAndName to theLocation & docNameNew
save front document as asType in docPathAndName
end if
try
close front document saving no
end try
end repeat
quit
end tell
end open
Fantastic job, it did just like I needed to convert the Pages documents I develop at work for DOCs for the rest of the office. Fantastic!
I tried the script in Snow Leopard and it works. But I would like to open the PagesScript.app using Terminal and pass in the file(s) as parameter.
I tried this
$ open PagesScript.app ~/TestFile.pages
The script opens Pages with the file but it doesn't export the file as it does if I drag and drop the file on the PagesScript.app. Am I doing something wrong? How can I fix the problem?
Thank you,
Edgar
P.S.: I have to open this script from the Terminal because I want to be able to call it remotely using ssh.
Edgar, I think "open" is the wrong way to go. I think it simply opens all the argument files independently.
You could try using the command "osascript" instead. At least it takes arguments. Type "man osascript" in the terminal to get a brief introduction.
You would definitely have to modify the scripts above anyhow.
If you or someone else finds a good solution, feel free to post back.
This is exciting stuff... I hate to ruin the party with a stupid question, but can you walk me (a novice Mac user) through how to add this code into Script Editor and compile it properly? Where do I save it? Do I just post the whole block of code into Script Editor or should any of this be pasted into separate scripts? Will this convert automatically every document on the CPU or just when files are saved?
Thanks!
The Tallest Elf,
Nice blogs of yours!
The blog entry was actually written in an attempt to go step by step. I do not know how to write it much clearer.
However, I can answer your questions.
You can save the script wherever you want, as long as you can find it again.
You just paste the full text - that is the one displayed in monospaced font. There is only one script per version.
It will convert only the files you drag to the script you saved.
As you can see, people have changed and improved the script in different ways. If you search Apple's forums you will find other scripts which may do other things you want. Especially look out for the scripts by Yvan Koenig.
Not sure when things changed, but using "SLDocumentTypeMSWord" no longer works in current (iWork '09) Pages on OS X 10.6.
What will work is changing the relevant line of these scripts to read:
save front document as "Microsoft Word 97 - 2004 Document" in docPathAndName
Good luck!
That was Awesome! I really needed that script!
I took the most recent script in the comments section (the one that works with 10.5/10.6) and made it an Automator workflow app. Now you can drag multiple .pages documents onto it, it will ask you the same "what type" and "where" questions initially, and then it will batch convert all of the files.
here is a link to the file:
http://www.grassrootsaudio.com/software/Pages-to-Other.zip
hope this helps!
-- Jeff
Interesting article, added his blog to Favorites
I had a problem running this script under 10.7 Lion with Pages 4.1.
It seems that this version of Pages does not open the template chooser when activated from AppleScript so the code that attempted to close the template chooser fails.
I also noticed that the check for Pages being open at the start of the script did not exit the script after the warning dialog so I added a "return" command after the "display dialog" command.
I've also updated the .doc type to "Microsoft Word 97 - 2004 Document"
on open theFiles
tell application "System Events"
if process "Pages" exists then
display dialog "whoops, please close Pages before running droplet!"
return
end if
end tell
tell application "Pages"
activate
set theList to {"doc", "rtf", "pdf", "txt"}
set theType to (choose from list theList OK button name "Select" with title "Pages Export" with prompt "Choose one or more formats to export using Pages" with multiple selections allowed) as text item
set s to theType as string
set theLocation to choose folder
set theLocation to theLocation as string
repeat with aFile in theFiles
open aFile
if theType contains "doc" then
set asType to "Microsoft Word 97 - 2004 Document"
set docName to name of front document
-- Remove .pages extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".pages"
-- Add .doc extension.
set docNameNew to first text item of docName & ".doc"
set AppleScript's text item delimiters to prevTIDs
-- Save file to Desktop.
set docPathAndName to theLocation & docNameNew
save front document as asType in docPathAndName
end if
if theType contains "rtf" then
set asType to "SLDocumentTypeRichText"
set docName to name of front document
-- Remove .pages extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".pages"
-- Add .doc extension.
set docNameNew to first text item of docName & ".rtf"
set AppleScript's text item delimiters to prevTIDs
-- Save file to Desktop.
set docPathAndName to theLocation & docNameNew
save front document as asType in docPathAndName
end if
if theType contains "pdf" then
set asType to "SLDocumentTypePDF"
set docName to name of front document
-- Remove .pages extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".pages"
-- Add .doc extension.
set docNameNew to first text item of docName & ".pdf"
set AppleScript's text item delimiters to prevTIDs
-- Save file to Desktop.
set docPathAndName to theLocation & docNameNew
set s to save front document as asType in docPathAndName
end if
if theType contains "txt" then
set asType to "SLDocumentTypePlainText"
set docName to name of front document
-- Remove .pages extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".pages"
-- Add .doc extension.
set docNameNew to first text item of docName & ".txt"
set AppleScript's text item delimiters to prevTIDs
-- Save file to User Specified Location
set docPathAndName to theLocation & docNameNew
save front document as asType in docPathAndName
end if
try
close front document saving no
end try
end repeat
quit
end tell
end open
Thanks very much for the original script!
AndyB
I found this post because I was looking for a way to convert AppleWorks files to Pages.
I have modified the script to open AppleWorks 6 files and export them to the various formats that Pages supports including the .pages format itself.
I believe that Pages will only open AppleWorks v6 files, so you will need to convert v5 and earlier into v6 before you can use them with this script.
Unfortunately the script is too long to post with this explanation so I will post it after this.
AndyB.
on open theFiles
tell application "System Events"
if process "Pages" exists then
display dialog "whoops, please close Pages before running droplet!"
return
end if
end tell
tell application "Pages"
activate
set theList to {"pages", "doc", "rtf", "pdf", "txt"}
set theType to (choose from list theList OK button name "Select" with title "Pages Export" with prompt "Choose one or more formats to export using Pages" with multiple selections allowed) as text item
set s to theType as string
set theLocation to choose folder
set theLocation to theLocation as string
repeat with aFile in theFiles
open aFile
if theType contains "pages" then
set asType to "SLDocumentTypeNativePublication"
set docName to name of front document
-- Remove .cwk extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".cwk"
-- Pages automatically adds .pages
set docNameNew to first text item of docName
set AppleScript's text item delimiters to prevTIDs
-- Save file to Desktop.
set docPathAndName to theLocation & docNameNew
save front document as asType in docPathAndName
end if
if theType contains "doc" then
set asType to "Microsoft Word 97 - 2004 Document"
set docName to name of front document
-- Remove .cwk extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".cwk"
-- Add .doc extension.
set docNameNew to first text item of docName & ".doc"
set AppleScript's text item delimiters to prevTIDs
-- Save file to Desktop.
set docPathAndName to theLocation & docNameNew
save front document as asType in docPathAndName
end if
if theType contains "rtf" then
set asType to "SLDocumentTypeRichText"
set docName to name of front document
-- Remove .cwk extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".cwk"
-- Add .doc extension.
set docNameNew to first text item of docName & ".rtf"
set AppleScript's text item delimiters to prevTIDs
-- Save file to Desktop.
set docPathAndName to theLocation & docNameNew
save front document as asType in docPathAndName
end if
if theType contains "pdf" then
set asType to "SLDocumentTypePDF"
set docName to name of front document
-- Remove .cwk extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".cwk"
-- Add .doc extension.
set docNameNew to first text item of docName & ".pdf"
set AppleScript's text item delimiters to prevTIDs
-- Save file to Desktop.
set docPathAndName to theLocation & docNameNew
set s to save front document as asType in docPathAndName
end if
if theType contains "txt" then
set asType to "SLDocumentTypePlainText"
set docName to name of front document
-- Remove .cwk extension.
set prevTIDs to AppleScript's text item delimiters
set AppleScript's text item delimiters to ".cwk"
-- Add .doc extension.
set docNameNew to first text item of docName & ".txt"
set AppleScript's text item delimiters to prevTIDs
-- Save file to User Specified Location
set docPathAndName to theLocation & docNameNew
save front document as asType in docPathAndName
end if
try
close front document saving no
end try
end repeat
quit
end tell
end open
This post is old, so I don't know if anyone is still following it. However, when I use the script in Automator (OSX Lion 10.7), it converts fine, but the filename is the old filename plus the new filename. For example, "this_filename.pages" becomes "this_filename.pagesthis_filename.pdf". Can anyone give me a suggestion about how to fix it? Thanks.
Post a Comment