Search code examples
textoutputadobe-indesignparagraph

Error Running jsx file from Indesign to export each text frame as a .txt file


Last year my colleague helped to build a script for Indesign.

Subsequently, after a system update we no longer have the script as Indesign CS6 was reinstalled, all we have is a version as below.

Using this code in Adobe Indesign to export each text frame that begins with a particular Paragraph stylesheet "PRODUCT HEADING" however I get an error message when I run the script...

Script is based on the ExportAllStories.jsx bundled with InDesign, plus a few mods found online.

//ExportAllStories.jsx
//An InDesign CS6 JavaScript
/*  
@@@BUILDINFO@@@ "ExportAllStories.jsx" 3.0.0 15 December 2009
*/
//Exports all stories in an InDesign document in a specified text format.
//
//For more on InDesign scripting, go to http://www.adobe.com/products/indesign/scripting/index.html
//or visit the InDesign Scripting User to User forum at http://www.adobeforums.com
//
main();
function main(){
    //Make certain that user interaction (display of dialogs, etc.) is turned on.
    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
    if(app.documents.length != 0){
        if (app.activeDocument.stories.length != 0){
            myDisplayDialog();
        }
        else{
            alert("The document does not contain any text. Please open a document containing text and try again.");
        }
    }
    else{
        alert("No documents are open. Please open a document and try again.");
    }
}
function myDisplayDialog(){
    with(myDialog = app.dialogs.add({name:"ExportAllStories"})){
        //Add a dialog column.
        myDialogColumn = dialogColumns.add()    
        with(myDialogColumn){
            with(borderPanels.add()){
                staticTexts.add({staticLabel:"Export as:"});
                with(myExportFormatButtons = radiobuttonGroups.add()){
                    radiobuttonControls.add({staticLabel:"Text Only", checkedState:true});
                    radiobuttonControls.add({staticLabel:"RTF"});
                    radiobuttonControls.add({staticLabel:"InDesign Tagged Text"});
                }
            }
        }
        myReturn = myDialog.show();
        if (myReturn == true){
            //Get the values from the dialog box.
            myExportFormat = myExportFormatButtons.selectedButton;
            myDialog.destroy;
            myFolder= Folder.selectDialog ("Choose a Folder");
            if((myFolder != null)&&(app.activeDocument.stories.length !=0)){
                myExportAllStories(myExportFormat, myFolder);
            }
        }
        else{
            myDialog.destroy();
        }
    }
}
//myExportStories function takes care of exporting the stories.
//myExportFormat is a number from 0-2, where 0 = text only, 1 = rtf, and 3 = tagged text.
//myFolder is a reference to the folder in which you want to save your files.
function myExportAllStories(myExportFormat, myFolder){
    for(myCounter = 0; myCounter < app.activeDocument.stories.length; myCounter++){
        myStory = app.activeDocument.stories.item(myCounter);
        myID = myStory.id;
        switch(myExportFormat){
            case 0:
                myFormat = ExportFormat.textType;
                myExtension = ".txt"
                break;
            case 1:
                myFormat = ExportFormat.RTF;
                myExtension = ".rtf"
                break;
            case 2:
                myFormat = ExportFormat.taggedText;
                myExtension = ".txt"
                break;
        }
        if (myStory.paragraphs[0].appliedParagraphStyle.name == "PRODUCT HEADING"){

              myFileName = myFileName.replace(/\s*$/,' ');
              myFileName2 = myFileName.replace(/\//g, ' ');
              myFilePath = myFolder + "/" + myFileName2;
            myFile = new File(myFilePath);
            myStory.exportFile(myFormat, myFile);
        }
    }
}

This results in an error on

        if (myStory.paragraphs[0].appliedParagraphStyle.name == "PRODUCT HEADING"){

Any advice would be appreciated.

There is definitely a block of text with the style PRODUCT HEADING (all caps) in the Indesign File. We run Indesign CS6 as previous

thanks!


Solution

  • Your problem is most likely with this part: myStory.paragraphs[0]. If the story has no paragraphs this will give you an error. You could add a condition before running this line, like this for example:

    if(myStory.paragraphs.length){
        if (myStory.paragraphs[0].appliedParagraphStyle.name == "PRODUCT HEADING"){
    
                myFileName = myFileName.replace(/\s*$/,' ');
                myFileName2 = myFileName.replace(/\//g, ' ');
                myFilePath = myFolder + "/" + myFileName2;
                myFile = new File(myFilePath);
                myStory.exportFile(myFormat, myFile);
            }
    }