Export All Stories Script

I recently posted this useful script by Olav Kvern on another forum, but thought I’d post it here as well so that more users would be aware of it.

This script extracts all the separate stories in an InDesign document, concatenates and saves them into a single text file. When you do the export you have your choice of formats (.txt, .rtf, and tagged text), and whether or not you want separator lines inserted between the stories.

This script can be very useful when you want a quick and efficient way to re-purpose the text content of an InDesign document in another application.

To use the script, just copy and paste the javascript code below into a text editor and save it as a text only file with a .jsx extension. Then put it in your InDesign Scripts folder. This script was written for InDesign CS3, but runs on CS2 as well (on both Mac and Windows, because it’s a javascript).


//An InDesign CS3 JavaScript
//Exports all of the text in the active document as a single
//text file. To do this, the script will create a new document,
//combine the stories in the new document using export/import,
//and then export the text from the new document.
if(app.documents.length != 0){
if(app.documents.item(0).stories.length != 0){
function myGetFileName(myDocumentName){
var myFilePath = File.saveDialog("Save Exported File As:");
if(myFilePath != null){
myDisplayDialog(myDocumentName, myFilePath);
function myDisplayDialog(myDocumentName, myFilePath){
//Need to get export format, story separator.
var myExportFormats = ["Text Only", "Tagged Text", "RTF"];
var myDialog = app.dialogs.add({name:"ExportAllStories"});
var myExportFormatDropdown = dropdowns.add({stringList:myExportFormats, selectedIndex:0});
var myAddSeparatorCheckbox = checkboxControls.add({staticLabel:"Add separator line", checkedState:true});
var myResult = myDialog.show();
if(myResult == true){
var myExportFormat = myExportFormats[myExportFormatDropdown.selectedIndex];
var myAddSeparator = myAddSeparatorCheckbox.checkedState;
myExportAllText(myDocumentName, myFilePath, myExportFormat, myAddSeparator);
function myExportAllText(myDocumentName, myFilePath, myExportFormat, myAddSeparator){
var myStory;
var myTempFolder = Folder.temp;
var myTempFile = File(myTempFolder + "/tempTextFile.txt");
var myNewDocument = app.documents.add();
var myDocument = app.documents.item(myDocumentName);
var myTextFrame = myNewDocument.pages.item(0).textFrames.add({geometricBounds:myGetBounds(myNewDocument, myNewDocument.pages.item(0))});
var myNewStory = myTextFrame.parentStory;
for(myCounter = 0; myCounter < myDocument.stories.length; myCounter++){
myStory = myDocument.stories.item(myCounter);
//Export the story as tagged text.
myStory.exportFile(ExportFormat.taggedText, myTempFile);
//Import (place) the file at the end of the temporary story.
//If the imported text did not end with a return, enter a return
//to keep the stories from running together.
if(myCounter != myDocument.stories.length -1){
if(myNewStory.characters.item(-1).contents != "\r"){
myNewStory.insertionPoints.item(-1).contents = "\r";
if(myAddSeparator == true){
myNewStory.insertionPoints.item(-1).contents = "—————————————-\r";
case "Text Only":
myFormat = ExportFormat.textType;
myExtension = ".txt"
case "RTF":
myFormat = ExportFormat.RTF;
myExtension = ".rtf"
case "Tagged Text":
myFormat = ExportFormat.taggedText;
myExtension = ".txt"
myNewStory.exportFile(myFormat, File(myFilePath));
function myGetBounds(myDocument, myPage){
var myPageWidth = myDocument.documentPreferences.pageWidth;
var myPageHeight = myDocument.documentPreferences.pageHeight
if(myPage.side == PageSideOptions.leftHand){
var myX2 = myPage.marginPreferences.left;
var myX1 = myPage.marginPreferences.right;
var myX1 = myPage.marginPreferences.left;
var myX2 = myPage.marginPreferences.right;
var myY1 = myPage.marginPreferences.top;
var myX2 = myPageWidth – myX2;
var myY2 = myPageHeight – myPage.marginPreferences.bottom;
return [myY1, myX1, myY2, myX2];