0

I have the following script which has been edited over several years.

I created a loop to cycle through each language. Running the loop has the desired effect with hiding and showing each language. Whenever I run the script calling the main function in my loop it stops after the english.

I know there will be a cleaner tidier way of writing what this script does but it has been edited over several years to fit changing needs and I am not a developer.

var languages = ['ENGLISH', 'FRENCH', 'DUTCH', 'ITALIAN', 'GERMAN'];


for(var i=0;i<languages.length;i++){
    myFunction( languages[i] );
}

function myFunction(value) {
    if (value == 'ENGLISH') {
        app.activeDocument.conditions.item("ENGLISH").visible = true;
        app.activeDocument.conditions.item("FRENCH").visible = false;
        app.activeDocument.conditions.item("DUTCH").visible = false;
        app.activeDocument.conditions.item("ITALIAN").visible = false;
        app.activeDocument.conditions.item("GERMAN").visible = false;

}
if (value == 'FRENCH') {
    app.activeDocument.conditions.item("ENGLISH").visible = false;
    app.activeDocument.conditions.item("FRENCH").visible = true;
    app.activeDocument.conditions.item("DUTCH").visible = false;
    app.activeDocument.conditions.item("ITALIAN").visible = false;
    app.activeDocument.conditions.item("GERMAN").visible = false;

}
if (value == 'DUTCH') {
    app.activeDocument.conditions.item("ENGLISH").visible = false;
    app.activeDocument.conditions.item("FRENCH").visible = false;
    app.activeDocument.conditions.item("DUTCH").visible = true;
    app.activeDocument.conditions.item("ITALIAN").visible = false;
    app.activeDocument.conditions.item("GERMAN").visible = false;

}    
if (value == 'ITALIAN') {
    app.activeDocument.conditions.item("ENGLISH").visible = false;
    app.activeDocument.conditions.item("FRENCH").visible = false;
    app.activeDocument.conditions.item("DUTCH").visible = false;
    app.activeDocument.conditions.item("ITALIAN").visible = true;
    app.activeDocument.conditions.item("GERMAN").visible = false;

}    
if (value == 'GERMAN') {
    app.activeDocument.conditions.item("ENGLISH").visible = false;
    app.activeDocument.conditions.item("FRENCH").visible = false;
    app.activeDocument.conditions.item("DUTCH").visible = false;
    app.activeDocument.conditions.item("ITALIAN").visible = false;
    app.activeDocument.conditions.item("GERMAN").visible = true;

}

main(value);
}

function main(language){
    //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(language);
    }
    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(language){
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, language);
        }
    }
    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, language){
    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.length){
if (myStory.paragraphs[0].appliedParagraphStyle.name == "PRODUCT HEADING"){

        myFileName = myStory.paragraphs[0].contents.replace(/\s*$/,''); 
        myFileName2 = myFileName.replace(/\//g, ' ');
        myFilePath = myFolder + "/" + language + '_' + myFileName2 + myExtension;
        myFile = new File(myFilePath);
        myStory.exportFile(myFormat, myFile);


    }
}
}
}

1 Answers1

0

Maybe the other conditions are not present or the "PRODUCT HEADING" paragraph style isn't applied to the localized conditioned text.

ANyway here is an approach that intends to ease things a little bit and make the stuff more maintanable.

var main = function(language) {
 var doc = app.properties.activeDocument;
 
 if ( !doc ) return;
 
 displayCondition(doc,language);
 myDisplayDialog(language);
}

var displayCondition = function(doc, language ) {
 var myCond = doc.conditions.itemByName (language);
 if ( !myCond.isValid ) return;
 doc.conditions.everyItem().visible = false;
 myCond.visible = true;
}

var u;

app.doScript ( "main('DUTCH')",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );


function myDisplayDialog(language){
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, language);
        }
    }
    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, language){
    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.length){
if (myStory.paragraphs[0].appliedParagraphStyle.name == "PRODUCT HEADING"){

        myFileName = myStory.paragraphs[0].contents.replace(/\s*$/,''); 
        myFileName2 = myFileName.replace(/\//g, ' ');
        myFilePath = myFolder + "/" + language + '_' + myFileName2 + myExtension;
        myFile = new File(myFilePath);
        myStory.exportFile(myFormat, myFile);


    }
}
}
}

HTH

Loic
  • 2,173
  • 10
  • 13