使用以下脚本苦苦挣扎。我修改了一个脚本,以便将所有故事都提取为txt文件,并使用段落中文本的文件名保存txt文件。
事实证明,我们并不想要提取所有的故事,但是我们想要的故事却为故事的第一段设置了段落风格。
我正在努力的部分是if语句的语法,它检查当前应用的段落样式是什么。
任何帮助表示赞赏,抱歉,如果我的问题不清楚
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 = "PRODUCT HEADING"){
myFileName = myStory.paragraphs[0].contents;
myFilePath = myFolder + "/" + myFileName;
myFile = new File(myFilePath);
myStory.exportFile(myFormat, myFile);
}
}
}
答案 0 :(得分:1)
appliedParagraphStyle
的类型为[Object ParagraphStyle]
,因此您需要将其与另一个段落样式进行比较(即 返回段落样式的app.activeDocument.paragraphStyles.item("PRODUCT HEADING")
),或比较样式的名称。
此外,不要使用=
来测试。单个=
是“适用”;要测试(in)相等,请使用双==
。 (Javascript也有'严格等于'比较:===
,但在这种情况下不应该使用它。)
如果您将比较行更改为
,您的脚本将有效if (myStory.paragraphs[0].appliedParagraphStyle.name == "PRODUCT HEADING")
此外,该行
myFileName = myStory.paragraphs[0].contents;
抓取整个段落用于文件名,通常这将包括末尾的段落返回。 (例外情况是此段落是故事中的最后一段。)由于您使用此字符串作为新文件名,因此必须删除段落返回(如果它在那里)。这可以通过多种方式完成,但最简单的方法是使用RegEx替换:
myFileName = myStory.paragraphs[0].contents.replace(/\s*$/,'');
因为这样也会免费删除末尾的所有杂散空格和标签。