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).

 

//ExportAllText.jsx
//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){
myGetFileName(app.documents.item(0).name);
}
}
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"});
with(myDialog.dialogColumns.add()){
with(dialogRows.add()){
with(dialogColumns.add()){
var myExportFormatDropdown = dropdowns.add({stringList:myExportFormats, selectedIndex:0});
}
}
with(dialogRows.add()){
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;
myDialog.destroy();
myExportAllText(myDocumentName, myFilePath, myExportFormat, myAddSeparator);
}
else{
myDialog.destroy();
}
}
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.
myNewStory.insertionPoints.item(-1).place(myTempFile);
//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";
}
}
}
switch(myExportFormat){
case "Text Only":
myFormat = ExportFormat.textType;
myExtension = ".txt"
break;
case "RTF":
myFormat = ExportFormat.RTF;
myExtension = ".rtf"
break;
case "Tagged Text":
myFormat = ExportFormat.taggedText;
myExtension = ".txt"
break;
}
myNewStory.exportFile(myFormat, File(myFilePath));
myNewDocument.close(SaveOptions.no);
myTempFile.remove();
}
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;
}
else{
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];
}


13 Responses to Export All Stories Script

  1. Matthew Treder says:

    Thanks, that’s absolutely indispensable. Glad to know about this one. (It really should ship with ID!)

    Matthew

  2. Tim Cole says:

    Yes, it’s a very useful script. Take advantage of ID’s keyboard shortcut editor, and assign a shortcut to the script. At that point you can forget that it’s a script and not a menu command.

  3. Haeme Ulrich says:

    On my InDesign, the Script runs only after deleting the ID preferences. I have/had this behavior also with other Scripts in CS3.

  4. Check http://www.rorohiko.com/textexporter.html it has a great list of options for that type of export, and it’s free !

  5. Tim Cole says:

    Haeme, you’re probably suffering from a problem I described in an earlier post: http://blogs.adobe.com/indesignchannel/2007/05/when_bad_things_happen_to_good.html

    What’s happened is that a script has probably altered your scripting preferences without restoring them after it’s finished. Run the one line script I posted in this other article and it should fix your problem.

  6. Tim Cole says:

    Thanks, Branislav, for the URL for the Rorohiko plug-in and script. This script requires a plug-in to be installed as well, but it’s well worth the effort because, a) it’s free, and b) the options it adds for text export are excellent. No layer selection yet, but definitely more advanced than the script I posted.

    Thanks to Rorohiko for making it available in this way.

  7. Tim

    When I evangelize InDesign for Adobe or during my trainings, or even in my French community of ID users, this script is the absolute #1 hit !

  8. Anne-Marie says:

    In CS3 you can also just export the document to XHTML, then copy/paste the text in the browser window to your word processing document.

    Anne-Marie Concepcion

  9. Suresh Choudhary says:

    this script is very helpful.

  10. Tracey says:

    This script was super helpful and just saved me a ton of time! I thought i could get the export to XML to work but it didn’t work as needed, this was much simpler.

    [TC: Hi Tracey, glad the script worked for you. If you like that script, you’ll love a free plug-in at rorohiko.com. I’m going to blog about these soon, but check out the free InDesign plug-ins page. They’ve got a great text exporter plug-in. You need to also download their free Active Page Item plug-in (be sure to get the latest version) in order for Text Exporter to work. Text Exporter is one of several very useful plug-ins that Kris at Rorohiko has made available. Check them out.]

  11. Betty Hoover says:

    Thank you so much for this script! It is really very helpful!

    Betty

  12. Gareth says:

    This script is brilliant but could it be altered to export no information from master pages?

    [TC: Gareth, checkout http://www.rorohiko.com. Chris has a great collection of free plug-ins for InDesign. One of them is called Text Exporter, and it has a preference for including or excluding text on master pages. There’s a lot of other good stuff there too. Be sure to download both Text Exporter and the AIPD enabler plug-in that it requires.]

  13. winna says:

    I’ve just installed the LB Text Exporter and APID Tool Assistant for InDesign CS2. They export wonderfully, but after each export, InDesign crashes. Have you heard of this happening? I’m on a Mac with Leopard, and I currently have CS2 and CS3 installed…but I want to use the exporter only for CS2 right now. Any tips?