使用 ExtendScript 设计条件文本样式

Indesign conditional text styling using ExtendScript

有谁知道是否可以使用 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','16','Times New Roman','8.75',
];

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','Times New Roman','Regular',
  'Courier New','Italic','Times New Roman','Italic',
  'Courier New','Bold','Times New Roman','Bold',
  'Courier New','Bold Italic','Times New Roman','Bold Italic',
  'Courier New','75 Black','Univers','75 Black',
  'Arial Unicode MS','Regular','Times New Roman','Regular',
];

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
    }
};