使用 ExtendScript 的 Indesign 条件文本样式

问题描述

有谁知道是否可以使用 ExtendScript 以某种方式设置文本样式,仅当相关段落满足特定条件时?

我拼凑了一个 ExtendScript 脚本,我在 InDesign 中使用它来修复段落样式工具未正确修复的文本样式,到目前为止它运行良好 - 将 Courier 和 Arial Unicode 文本更改为 Times New Roman并修复字体大小 - 但我真的很想包含一个将 Times New Roman Italic 更改为 Times New Roman Bold Italic 的功能 - 但仅当它出现的段落具有在 Univers 中设置的第一个字母时。有没有一种方法可以包含一个“if”语句,它只会在这些情况下触发这种样式更改?

这是我现有的代码

  var mydoc = app.activeDocument;

var theFontSize = [
  'Courier New','16','Courier New','8.75','Times New Roman',];

for (i = 0; i < (theFontSize.length/4); i++) {

  app.findTextPreferences = nothingEnum.nothing;
  app.changeTextPreferences = nothingEnum.nothing;
  app.findTextPreferences.appliedFont = theFontSize[i*4];
  if (theFontSize[(i*4)+1] != ''){
    app.findTextPreferences.pointSize = theFontSize[(i*4)+1];
  };
  app.changeTextPreferences.appliedFont  = theFontSize[(i*4)+2];
  if (theFontSize[(i*4)+3] != ''){
    app.changeTextPreferences.pointSize  = theFontSize[(i*4)+3];
    };
    mydoc.changeText();
  };

var theFontReplacements = [
  'Courier New','Regular','Italic','Bold','Bold Italic','75 Black','Univers','Arial Unicode MS',];

for (i = 0; i < (theFontReplacements.length/4); i++) {

  app.findTextPreferences = nothingEnum.nothing;
  app.changeTextPreferences = nothingEnum.nothing;
  app.findTextPreferences.appliedFont = theFontReplacements[i*4];
  if (theFontReplacements[(i*4)+1] != ''){
    app.findTextPreferences.fontStyle = theFontReplacements[(i*4)+1];
  };
  app.changeTextPreferences.appliedFont  = theFontReplacements[(i*4)+2];
  if (theFontReplacements[(i*4)+3] != ''){
    app.changeTextPreferences.fontStyle  = theFontReplacements[(i*4)+3];
  };
  mydoc.changeText();

};

app.findTextPreferences = nothingEnum.nothing;
app.changeTextPreferences = nothingEnum.nothing;

解决方法

这应该会让你开始。它会比使用 changeText 慢,但我认为您无法使用 changeText 完成您的要求。

// Change applied font of text set in Times New Roman Italic in paragraphs whose first character is set in Univers to Times New Roman Bold Italic

app.findTextPreferences.appliedFont = 'Times New Roman';
app.findTextPreferences.fontStyle = 'Italic';

var foundItems = app.activeDocument.findText(); // Set foundItems to all results of search according to app.findTextPreferences
for (var i = 0; i < foundItems.length; i++) { // For each result found
    var item = foundItems[i];
    if (item.paragraphs[0].characters[0].appliedFont.name.indexOf('Univers') == 0) { // If item’s first enclosing paragraph’s first character’s font name starts with “Univers”
        item.appliedFont = 'Times New Roman\tBold Italic'; // Set item’s font to Times New Roman italic
    }
};