There may be an occasion when you want to export each section of a Pages document as separate text file. Here’s an AppleScript script for doing just that.
DO THIS ►DOWNLOAD a ZIP archive of a Pages document containing the text of Alice in Wonderland, with each chapter as its own section.
(A special “thank you” to Project Gutenberg for making this content available to the public!)
Here’s the script:
Export Sections to Text Files
01
globalglobalErrorMessage
02
-- reset the global error variable
03
setglobalErrorMessagetomissing value
04
tellapplication "Pages"
05
activate
06
try
07
-- check for open document
08
if not (existsdocument 1) then errornumber 1000
09
tell the frontdocument
10
-- check for document body
11
ifdocument bodyisfalsethen errornumber 1001
12
-- prompt the user for a destination folder
13
setdestinationFolderto ¬
14
(choose folderwith prompt ¬
15
"Choose the folder in which to place a folder containing the output files:")
16
-- prompt the user for a basename for the files:
17
repeat
18
display dialog "Enter the base name to use for the exported files:" default answer ""
19
set theexportBasenameto thetext returnedof theresult
20
if theexportBasenameis not "" then exit repeat
21
end repeat
22
-- export the sections as files
23
repeat withifrom 1 to thecountofsections
24
setthisTextto thebody textofsectioni
25
setthisFileNametoexportBasename & (iasstring)
26
settargetFileHFSPath to (destinationFolderasstring) & thisFileName & ".txt"
Mention of third-party websites and products is for informational purposes only and constitutes neither an endorsement nor a recommendation. MACOSXAUTOMATION.COM assumes no responsibility with regard to the selection, performance or use of information or products found at third-party websites. MACOSXAUTOMATION.COM provides this only as a convenience to our users. MACOSXAUTOMATION.COM has not tested the information found on these sites and makes no representations regarding its accuracy or reliability. There are risks inherent in the use of any information or products found on the Internet, and MACOSXAUTOMATION.COM assumes no responsibility in this regard. Please understand that a third-party site is independent from MACOSXAUTOMATION.COM and that MACOSXAUTOMATION.COM has no control over the content on that website. Please contact the vendor for additional information.