Recursively Convert Open Document Format (.odt) Files to Pages

So a friend got a new Mac and had over 900 files in open document text format that needed to be brought into Pages, but Pages can’t import them. So this is my noob attempt at a solution. Thanks to a lot of wonderful tips throughout this forum I was able to get it up and running. It converted the files in about 20 minutes on a MacBook Air, M2 running Ventura.

It uses no third party extensions, just plain AppleScript. A few of many lessons learned in the process…

  1. Pages seems to like HFS paths better.
  2. Having the user select the files directly, rather than a folder, is faster.
  3. Putting the .Pages extension prevents a pesky “you don’t have permission to save this file” error.

Also, a good speed boost was to disable window animations, a big time waster.

I’m sure many mods and improvements can be done to handle different file types so feel free.

Sorry not sure why the markup isn’t working… code download below:


ODTtoPages.applescript (1.7 KB Download)

AppleScript Code
display alert "Instructions for this utility" message "This automates converting text files that Pages will not open like .odt and saving them as Pages documents. Please choose the files and a destination folder."

set the doList to choose file with prompt "Choose all files to Convert" default location (path to home folder) with multiple selections allowed

set myFolderPathD to (choose folder with prompt "Choose the Destination Folder" default location (path to home folder)) as text

tell application "Finder" to set theCount to the number of items in doList
tell application "Pages" to activate

-- disable window animations to speed up the processing
do shell script "defaults write NSGlobalDomain NSAutomaticWindowAnimationsEnabled -bool false"

repeat with i from 1 to theCount
   tell application "TextEdit" --use textedit to open the .odt files
      activate
      open item i of doList
      set theText to text of document 1 as text -- this stores the contents in theText
      set theFname to the name of document 1 as text
      set theName to (theFname's text 1 thru text item -5) --strip off the extension 
      close document 1
      
      tell application "Pages"
         activate
         make new document with properties {name:theName}
         -- now that the file exists in Pages format, it can be edited and saved
         set the body text of document 1 to theText
         set size of the body text of document 1 to 13
         set font of the body text of document 1 to "Times New Roman"
         set size of paragraph 1 of body text of document 1 to 16
         save document 1 in file (myFolderPathD & theName & ".Pages")
         tell document 1 to close
      end tell
   end tell
end repeat

-- restore the window animations to default
do shell script "defaults write NSGlobalDomain NSAutomaticWindowAnimationsEnabled -bool true"

I cleaned up your script a little

display alert "Instructions for this utility" message "This automates converting text files that Pages will not open like .odt and saving them as Pages documents. Please choose the files and a destination folder."

set the doList to choose file with prompt "Choose all files to Convert" default location (path to home folder) with multiple selections allowed

set myFolderPathD to (choose folder with prompt "Choose the Destination Folder" default location (path to home folder)) as text

set theCount to the number of items in doList -- no need to tell "Finder"
tell application "TextEdit" to activate -- get it loaded into memory
tell application "Pages"
	activate
	repeat until running -- in case it takes awhile to load
		delay 0.2
	end repeat
end tell

-- disable window animations to speed up the processing
do shell script "defaults write NSGlobalDomain NSAutomaticWindowAnimationsEnabled -bool false"
set tid to text item delimiters
set text item delimiters to "."
repeat with i from 1 to theCount
	tell application "TextEdit" --use textedit to open the .odt files
		open item i of doList
		set theText to text of document 1 as text -- this stores the contents in theText
		set theFname to the name of document 1 as text
		set theName to (text items 1 thru -2 of theFname) as text --strip off the extension
		-- (by using text item delimiters, we don't care how long the file extension is) 
		close document 1
	end tell
	tell application "Pages" -- Best not to have nested 'tell' blocks
		activate
		make new document with properties {name:theName}
		-- now that the file exists in Pages format, it can be edited and saved
		set the body text of document 1 to theText
		set size of the body text of document 1 to 13
		set font of the body text of document 1 to "Times New Roman"
		set size of paragraph 1 of body text of document 1 to 16
		save document 1 in file (myFolderPathD & theName & ".Pages")
		tell document 1 to close
	end tell
end repeat
set text item delimiters to tid
-- restore the window animations to default
do shell script "defaults write NSGlobalDomain NSAutomaticWindowAnimationsEnabled -bool true"

1 Like

Awesome, thanks. May I ask a noob question: how you got the script to post so neatly? I tried the three ticks “```” before and after it made a mess.

I used 3 ticks before and after ```
(the ticks go on a line by themselves.)

Exists faster and simpler way to convert .odt files to .pages files.

2 Likes

Following script will convert ODT files to PAGES files, retaining the original markup as well:
.

set temp to path to temporary items folder from user domain as text
set tempPath to quoted form of (POSIX path of temp & "temp.doc")

display alert "Instructions for this utility" message "This automates converting text files that Pages will not open like .odt and saving them as Pages documents. Please choose the files and a destination folder."
set the doList to choose file with prompt "Choose all files to Convert" default location (path to home folder) with multiple selections allowed
set myFolderPathD to (choose folder with prompt "Choose the Destination Folder" default location (path to home folder)) as text

-- disable window animations to speed up the processing
do shell script "defaults write NSGlobalDomain NSAutomaticWindowAnimationsEnabled -bool false"
repeat with odt in doList
	set posixPath to POSIX path of odt
	do shell script "textutil -convert doc " & quoted form of posixPath & " -output " & tempPath
	tell application "Pages"
		open file (temp & "temp.doc")
		export document 1 to file (myFolderPathD & my getBaseName(posixPath) & ".pages") as Pages 09
		close document 1
	end tell
end repeat
-- enable window animations
do shell script "defaults write NSGlobalDomain NSAutomaticWindowAnimationsEnabled -bool true"


on getBaseName(posixPath)
	set {ATID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "/"}
	if (posixPath ends with "/") then
		set baseName to text item -2 of posixPath
	else
		set baseName to text item -1 of posixPath
	end if
	if (baseName contains ".") then
		set AppleScript's text item delimiters to "."
		set baseName to text 1 thru text item -2 of baseName
	end if
	set AppleScript's text item delimiters to ATID
	return baseName
end getBaseName
1 Like

Very clever to use textutil to save the formatting, was wondering how that could be done. Using the temp folder probably has advantages as well.