indesign javascript导出文本循环

时间:2016-10-18 14:04:32

标签: javascript adobe-indesign jsx

我有以下编辑了几年的脚本。

我创建了一个循环来循环使用每种语言。运行循环具有隐藏和显示每种语言所需的效果。每当我运行脚本调用我的循环中的main函数时,它就会在英语后停止。

我知道将会有一个更干净整洁的方式来编写这个脚本的功能,但它已经过几年的编辑以适应不断变化的需求,而且我不是开发人员。

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 个答案:

答案 0 :(得分:0)

可能其他条件不存在或“PRODUCT HEADING”段落样式未应用于本地化条件文本。

这里的ANyway是一种旨在让事情变得更轻松并使这些东西变得更有效的方法。

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