Batch converting .pages to .docx and .pdf?

I’m leaving a job and want to leave my replacement everything that I’ve thrown together over the last two years. Unfortunately, I wasn’t able to get a Mac for her, and so I’d like to convert the many documents I made in Pages into a Word file (and maybe a PDF file too while I’m at it).

I’ve begun Googling for it, but I’m going to have to test out some of the solutions. I thought I’d ask here in case none of them wind up doing a great job – I don’t know how to write AppleScript, and I don’t know what the best tool would be. I do have Keyboard Maestro.

What I’ve Found

I’ve found some paid Automator actions that would do the trick, but I’d rather not drop $15 for something like this.

I’ve found an AppleScript that does the PDF portion, but not Word Doc. Might use this as a starting point to take the next steps.

I’ve found a script that doesn’t work that apparently did once upon a time. (It’s choking for me on the path to portion).

Yep, it’s easy. Just a matter of changing lines 1 and 17. Here it is for anyone wondering

set exportFileExtension to "docx"
set this_folder to (choose folder with prompt "Pick the folder containing the files to process:") as string
tell application "System Events"
	set these_files to every file of folder this_folder
end tell
repeat with i from 1 to the count of these_files
	set this_file to (item i of these_files as alias)
	set this_info to info for this_file
	tell application "Finder"
		set {fType, nExt} to ({file type, name extension} of file this_file)
		set documentName to the name of this_file
		set exportFileName to documentName & "." & exportFileExtension
	end tell
	if nExt contains "pages" then
		tell application "Pages"
			open this_file
			export front document to file (this_folder & exportFileName) as Microsoft Word
			close saving no
		end tell
	end if
end repeat

Now if anyone knows anyway to combine both of these actions into the same script, I’m at a loss there.

You actually only need to add two lines to do what you want.

For clarity sake, I renamed your exportFileExtension variable to make it clear what it is creating since you are now creating two files. Other than that, I added the set exportFileNamePDF and the export as PDF line. Then you should be all set.

set exportFileExtensionWord to "docx"
set exportFileExtensionPDF to "pdf"
set this_folder to (choose folder with prompt "Pick the folder containing the files to process:") as string
tell application "System Events"
	set these_files to every file of folder this_folder
end tell
repeat with i from 1 to the count of these_files
	set this_file to (item i of these_files as alias)
	set this_info to info for this_file
	tell application "Finder"
		set {fType, nExt} to ({file type, name extension} of file this_file)
		set documentName to the name of this_file
		set exportFileNameWord to documentName & "." & exportFileExtensionWord
		set exportFileNamePDF to documentName & "." & exportFileExtensionPDF
	end tell
	if nExt contains "pages" then
		tell application "Pages"
			open this_file
			export front document to file (this_folder & exportFileNameWord) as Microsoft Word
			export front document to file (this_folder & exportFileNamePDF) as PDF
			close saving no
		end tell
	end if
end repeat

1 Like

Thanks a ton! I had no idea where to start on that fix, which speaks to how little I know when it comes to basic scripting principles in general.

1 Like

That’s a fab script, thanks. Any idea why the ‘close’ line isn’t working?

I keep getting this error when trying this:

error “Finder got an error: Can’t make alias “Macintosh HD:Users:username:desktop:.DS_Store” into type file.” number -1700 from alias “Macintosh HD:Users:username:desktop:.DS_Store” to file