Search code examples
javascriptadobe-indesign

Stories are not exporting, and getting Undefined in the log


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 == "Headline"){
        myFileName = myStory.paragraphs[0].contents;
        myFilePath = myFolder + "/" + myFileName;
        myFile = new File(myFilePath);
        myStory.exportFile(myFormat, myFile);
    }
}
}
javascript

Hey Everyone,

I'm trying to reuse this script (InDesign Script extract stories if paragraph style) with the suggested edits. I set up some alerts to see what was happening when I run it, the stories with the paragraph styles get selected and show up. But the files aren't saving and I get an error message about Line 79 (if(myStory.paragraphs[0].appliedParagraphyStyle.name=="Headline"){). Error Number 45, String: Object is Invalid.


Solution

  • Can't reproduce the problem. I just copied the code and run it on my file and it works fine.

    There are two lines I'd advice to modify:

    var myFileName = myStory.paragraphs[0].contents.slice(0,-1); // added '.slice(0,-1)'
    var myFilePath = myFolder + "/" + myFileName + myExtension; // added '+ myExtension' 
    

    But these changes aren't crucial.

    So I guess the problem has to do with your INDD file, not with the code. If you share a sample of your INDD file I would try to figure out a solution.