Newsroom Update

New features come to Apple services this fall. Learn more >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Batch Convert pages to Word Document- MacOS Mojave

Is there a way to bulk convert pages files to .doc/.docx files in MacOS Mojave?

Posted on Aug 22, 2021 9:23 AM

Reply
Question marked as Best reply

Posted on Aug 23, 2021 10:21 AM

I have tested an Automator application that converts Pages documents to Word DOCX, and the same code works just fine on macOS 11.5.2 with Pages v11.1 and on macOS 10.14.6 with Pages v10.1. Per your request, the Automator application will first prompt you for the folder containing the Pages documents, and then for the folder that is to be the output folder for the Word documents. Then, it just runs to completion, and when done, displays:



From the Dock, select Launchpad, and then the Other category where you single-click Automator. It will launch with a panel where you select New Document, then Application, and then click the Choose button.


Now you have an Automator display with action Libraries on the left, and a large center workflow area. You drag and drop the following actions into the workflow window in order:

  • Library > Files and Folders > Ask for Finder Items. Do this twice.
  • Library > Utilities > Run AppleScript


Currently, it should look like this:


Notice I have changed the Ask for Finder Items Type to Folders.


Now, you can select everything in the Run AppleScript boilerplate and remove it. This will be replaced by the AppleScript that will process your Pages documents into Word files.


Copy/paste the following code into the now blank Run AppleScript and then click that hammer icon you see there.

(*
 Intended to be used in an Automator application's Run AppleScript action
 It accepts two input folders:1) source folder, and 2) output folder (in that order)
 It converts Pages documents found in the source folder to Word documents
 in the output folder.
 Reference: https://discussions.apple.com/thread/253072302
 Tested: macOS 11.5.2 (20G95), Pages 11.1, macOS 10.14.6 (18G9323, Pages 10.1
 VikingOSX, 2021-08-23, Apple Support Communities, No warranties/support expressed or implied.
*)

use scripting additions

property valid_kind : {"Pages document", "Pages Publication"}
property DOCX : ".docx"
property DELIM : {":", ".pages"}
property file_count : (0 as integer)
property app_icon : "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MultipleItemsIcon.icns"

on run {input, parameters}
	
	set Pages_Dir to (item 1 of input)
	set Word_Dir to (item 2 of input)
	
	try
		tell application "Finder"
			set pages_list to (every item in folder Pages_Dir whose kind is in valid_kind) as alias list
			if pages_list = {} then return
			
			repeat with anItem in pages_list
				tell application "System Events" to set package_status to {package folder} of anItem
				my export_file(anItem, Word_Dir, package_status)
			end repeat
		end tell
	on error errmsg number errnbr
		my error_handler(errnbr, errmsg)
		quit
	end try
	
	if file_count > 0 then
		display dialog "Export Complete." & return & tab & " Files exported to Word " & " :  " & (file_count as text) ¬
			buttons {"Done"} default button "Done" with title "Pages Export Facility" with icon POSIX file app_icon as alias
	end if
	quit
end run

on export_file(theFile, outdir, is_package_folder)
	
	set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, DELIM}
	if not is_package_folder then
		set exportDocument to ((outdir as text) & (item -2 of text items of (theFile as text)) & DOCX) as text
	else
		# because package folder files end in a colon adding an extra empty cell in the list
		set exportDocument to ((outdir as text) & (item -3 of text items of (theFile as text)) & DOCX) as text
	end if
	set AppleScript's text item delimiters to TID
	
	close access (open for access exportDocument)
	tell application "Pages"
		try
			set myDoc to open theFile as alias
			with timeout of 1800 seconds
				export myDoc to file exportDocument as Microsoft Word
			end timeout
			set file_count to (file_count + 1) as integer
			close myDoc saving no
		on error errmsg number errnbr
			my error_handler(errnbr, errmsg)
			quit
		end try
	end tell
	-- force extension visibility on exported Word document
	tell application "Finder"
		if exists (item exportDocument as alias) then
			set extension hidden of (item exportDocument as alias) to false
		end if
	end tell
	return
	
end export_file

on error_handler(nbr, msg)
	return display alert "[ " & nbr & " ] " & msg as critical giving up after 20
end error_handler

on quit {}
	-- perform these cleanup items, and then really quit
	tell application "Pages" to if it is running then quit
	set file_count to (0 as integer)
	set exportDocument to ""
	continue quit
end quit


Save this application to your Desktop (e.g. Pages2Word), and quit Automator. Double-click Pages2Word to run it and remember, the first folder prompt is the Pages document directory, and the second prompt is your Word document directory. When you run it, you will receive several prompts that ____ wants to control ____. Click yes to all of these, as its Apples new found sense of security being a PITA.

4 replies
Question marked as Best reply

Aug 23, 2021 10:21 AM in response to Bslim9

I have tested an Automator application that converts Pages documents to Word DOCX, and the same code works just fine on macOS 11.5.2 with Pages v11.1 and on macOS 10.14.6 with Pages v10.1. Per your request, the Automator application will first prompt you for the folder containing the Pages documents, and then for the folder that is to be the output folder for the Word documents. Then, it just runs to completion, and when done, displays:



From the Dock, select Launchpad, and then the Other category where you single-click Automator. It will launch with a panel where you select New Document, then Application, and then click the Choose button.


Now you have an Automator display with action Libraries on the left, and a large center workflow area. You drag and drop the following actions into the workflow window in order:

  • Library > Files and Folders > Ask for Finder Items. Do this twice.
  • Library > Utilities > Run AppleScript


Currently, it should look like this:


Notice I have changed the Ask for Finder Items Type to Folders.


Now, you can select everything in the Run AppleScript boilerplate and remove it. This will be replaced by the AppleScript that will process your Pages documents into Word files.


Copy/paste the following code into the now blank Run AppleScript and then click that hammer icon you see there.

(*
 Intended to be used in an Automator application's Run AppleScript action
 It accepts two input folders:1) source folder, and 2) output folder (in that order)
 It converts Pages documents found in the source folder to Word documents
 in the output folder.
 Reference: https://discussions.apple.com/thread/253072302
 Tested: macOS 11.5.2 (20G95), Pages 11.1, macOS 10.14.6 (18G9323, Pages 10.1
 VikingOSX, 2021-08-23, Apple Support Communities, No warranties/support expressed or implied.
*)

use scripting additions

property valid_kind : {"Pages document", "Pages Publication"}
property DOCX : ".docx"
property DELIM : {":", ".pages"}
property file_count : (0 as integer)
property app_icon : "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MultipleItemsIcon.icns"

on run {input, parameters}
	
	set Pages_Dir to (item 1 of input)
	set Word_Dir to (item 2 of input)
	
	try
		tell application "Finder"
			set pages_list to (every item in folder Pages_Dir whose kind is in valid_kind) as alias list
			if pages_list = {} then return
			
			repeat with anItem in pages_list
				tell application "System Events" to set package_status to {package folder} of anItem
				my export_file(anItem, Word_Dir, package_status)
			end repeat
		end tell
	on error errmsg number errnbr
		my error_handler(errnbr, errmsg)
		quit
	end try
	
	if file_count > 0 then
		display dialog "Export Complete." & return & tab & " Files exported to Word " & " :  " & (file_count as text) ¬
			buttons {"Done"} default button "Done" with title "Pages Export Facility" with icon POSIX file app_icon as alias
	end if
	quit
end run

on export_file(theFile, outdir, is_package_folder)
	
	set {TID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, DELIM}
	if not is_package_folder then
		set exportDocument to ((outdir as text) & (item -2 of text items of (theFile as text)) & DOCX) as text
	else
		# because package folder files end in a colon adding an extra empty cell in the list
		set exportDocument to ((outdir as text) & (item -3 of text items of (theFile as text)) & DOCX) as text
	end if
	set AppleScript's text item delimiters to TID
	
	close access (open for access exportDocument)
	tell application "Pages"
		try
			set myDoc to open theFile as alias
			with timeout of 1800 seconds
				export myDoc to file exportDocument as Microsoft Word
			end timeout
			set file_count to (file_count + 1) as integer
			close myDoc saving no
		on error errmsg number errnbr
			my error_handler(errnbr, errmsg)
			quit
		end try
	end tell
	-- force extension visibility on exported Word document
	tell application "Finder"
		if exists (item exportDocument as alias) then
			set extension hidden of (item exportDocument as alias) to false
		end if
	end tell
	return
	
end export_file

on error_handler(nbr, msg)
	return display alert "[ " & nbr & " ] " & msg as critical giving up after 20
end error_handler

on quit {}
	-- perform these cleanup items, and then really quit
	tell application "Pages" to if it is running then quit
	set file_count to (0 as integer)
	set exportDocument to ""
	continue quit
end quit


Save this application to your Desktop (e.g. Pages2Word), and quit Automator. Double-click Pages2Word to run it and remember, the first folder prompt is the Pages document directory, and the second prompt is your Word document directory. When you run it, you will receive several prompts that ____ wants to control ____. Click yes to all of these, as its Apples new found sense of security being a PITA.

Aug 23, 2021 5:09 AM in response to Bslim9

I suggest you do them in isolated batches of 250 and output that quantum to your designated folder. That is an arbitrary number, but AppleScript must open each Pages document before it exports it to Word, and 1000+ would run longer than daylight. If you are doing this on a laptop, keep it connected to the charger. When the script ends, it will present a dialog with the total number of files exported to Word.


I want some time to look over the existing AppleScript here, test it again on my Mojave iMac, as well as Big Sur before I post it here. Right now, it has extra code in it to do other exports as well as Word and due to that, it is larger than just the export to Word should be. The current version offers the choice of conversion in-place or an output folder selection.


Just understand that as Pages is not a Word clone, the translation process in converting Pages documents to Word may not result in Word documents that appear exactly as they did in Pages. Nothing the AppleScript can do about the internal Pages translation engine.

Batch Convert pages to Word Document- MacOS Mojave

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.