用于更改一行首字母样式的 InDesign 脚本
InDesign script to change the style of the first letter of a line
我正在尝试为 InDesign 编写一个脚本来查找每一段每一行的第一个字符,如果是元音字母则将其更改为另一种颜色。由于这是我在 InDesign 脚本编写方面的第一次尝试,我下载了 Adobe 的脚本指南并设法做到了以下几点:
createCharacterStyle();
main();
function main() {
// I don't check if a document is open for now
var myDocument = app.documents.item(0);
var myStory = myDocument.stories.item(0);
var noOfParas = myStory.paragraphs.length;
for ( var theParaCounter = 0 ; theParaCounter < noOfParas ; theParaCounter++) {
var currentParaLinesCount = myStory.paragraphs.item(theParaCounter).lines.length;
for (var theLineCounter = 0 ; theLineCounter < currentParaLinesCount - 1 ; theLineCounter++ ) {
var theCurrentLine = myStory.paragraphs.item(theParaCounter).lines.item(theLineCounter).contents;
var theFirstChar = theCurrentLine.charAt(0);
if ( theFirstChar == 'a' || theFirstChar == 'e' || theFirstChar == 'i' ||
theFirstChar == 'o' || theFirstChar == 'u') {
theFirstChar.appliedCharacterStyle = 'Highlighted';
}
}
}
}
function createCharacterStyle() {
var myDocument = app.documents.item(0);
// Create the highlight color
try {
myColor = myDocument.colors.item('Red');
myName = myColor.name;
}
catch ( myError ) {
myColor = myDocument.colors.add({name:'Red', model:ColorModel.process, colorValue:[0,100,100,0]});
}
// Create a new Character Style
try {
myCharStyle = myDocument.characterStyles.item('Highlighted');
myName = myCharStyle.name;
}
catch ( myError ) {
myCharStyle = myDocument.characterStyles.add({name:'Highlighted'});
}
myCharStyle.fillColor = myColor;
myCharStyle.underline = true;
}
首先我创建字符样式(红色下划线),然后遍历这些行。循环有效,并找到第一个字符。问题是样式从未应用过。
任何帮助将不胜感激。谢谢!
作为快速修复,您可以替换以下行:
theFirstChar.appliedCharacterStyle = 'Highlighted';
与:
myStory.paragraphs[theParaCounter].lines[theLineCounter].characters[0].appliedCharacterStyle = 'Highlighted';
问题是您代码中的 theFirstChar
只是一个字符串,一个文本。它没有 属性 appliedCharacterStyle
。您必须从 story/paragraph/line 中获取对象 character
:stories[0].paragraphs[counter].lines[counter].character[0]
如果您想在其上应用字符样式。
注:paragraphs.item(theParaCounter)
等同于paragraphs[theParaCounter]
,lines.item(theLineCounter)
等同于lines[theLineCounter]
。
另外条件可以缩短:
if ('aeiou'.indexOf(theFirstChar.toLowerCase()) > -1) {
而不是:
if ( theFirstChar == 'a' || theFirstChar == 'e' || theFirstChar == 'i' ||
theFirstChar == 'o' || theFirstChar == 'u') {
.toLowerCase()
使条件不区分大小写。如果你需要的话。
main()
函数可以归结为:
function main() {
var doc = app.activeDocument;
var story = doc.stories[0];
var paragraphs = story.paragraphs.everyItem().getElements();
while (paragraphs.length) {
var lines = paragraphs.shift().lines.everyItem().getElements();
while (lines.length) {
var character = lines.shift().characters[0];
if ('aeiou'.indexOf(character.contents.toLowerCase()) < 0) continue;
character.appliedCharacterStyle = 'Highlighted';
}
}
}
我正在尝试为 InDesign 编写一个脚本来查找每一段每一行的第一个字符,如果是元音字母则将其更改为另一种颜色。由于这是我在 InDesign 脚本编写方面的第一次尝试,我下载了 Adobe 的脚本指南并设法做到了以下几点:
createCharacterStyle();
main();
function main() {
// I don't check if a document is open for now
var myDocument = app.documents.item(0);
var myStory = myDocument.stories.item(0);
var noOfParas = myStory.paragraphs.length;
for ( var theParaCounter = 0 ; theParaCounter < noOfParas ; theParaCounter++) {
var currentParaLinesCount = myStory.paragraphs.item(theParaCounter).lines.length;
for (var theLineCounter = 0 ; theLineCounter < currentParaLinesCount - 1 ; theLineCounter++ ) {
var theCurrentLine = myStory.paragraphs.item(theParaCounter).lines.item(theLineCounter).contents;
var theFirstChar = theCurrentLine.charAt(0);
if ( theFirstChar == 'a' || theFirstChar == 'e' || theFirstChar == 'i' ||
theFirstChar == 'o' || theFirstChar == 'u') {
theFirstChar.appliedCharacterStyle = 'Highlighted';
}
}
}
}
function createCharacterStyle() {
var myDocument = app.documents.item(0);
// Create the highlight color
try {
myColor = myDocument.colors.item('Red');
myName = myColor.name;
}
catch ( myError ) {
myColor = myDocument.colors.add({name:'Red', model:ColorModel.process, colorValue:[0,100,100,0]});
}
// Create a new Character Style
try {
myCharStyle = myDocument.characterStyles.item('Highlighted');
myName = myCharStyle.name;
}
catch ( myError ) {
myCharStyle = myDocument.characterStyles.add({name:'Highlighted'});
}
myCharStyle.fillColor = myColor;
myCharStyle.underline = true;
}
首先我创建字符样式(红色下划线),然后遍历这些行。循环有效,并找到第一个字符。问题是样式从未应用过。 任何帮助将不胜感激。谢谢!
作为快速修复,您可以替换以下行:
theFirstChar.appliedCharacterStyle = 'Highlighted';
与:
myStory.paragraphs[theParaCounter].lines[theLineCounter].characters[0].appliedCharacterStyle = 'Highlighted';
问题是您代码中的 theFirstChar
只是一个字符串,一个文本。它没有 属性 appliedCharacterStyle
。您必须从 story/paragraph/line 中获取对象 character
:stories[0].paragraphs[counter].lines[counter].character[0]
如果您想在其上应用字符样式。
注:paragraphs.item(theParaCounter)
等同于paragraphs[theParaCounter]
,lines.item(theLineCounter)
等同于lines[theLineCounter]
。
另外条件可以缩短:
if ('aeiou'.indexOf(theFirstChar.toLowerCase()) > -1) {
而不是:
if ( theFirstChar == 'a' || theFirstChar == 'e' || theFirstChar == 'i' ||
theFirstChar == 'o' || theFirstChar == 'u') {
.toLowerCase()
使条件不区分大小写。如果你需要的话。
main()
函数可以归结为:
function main() {
var doc = app.activeDocument;
var story = doc.stories[0];
var paragraphs = story.paragraphs.everyItem().getElements();
while (paragraphs.length) {
var lines = paragraphs.shift().lines.everyItem().getElements();
while (lines.length) {
var character = lines.shift().characters[0];
if ('aeiou'.indexOf(character.contents.toLowerCase()) < 0) continue;
character.appliedCharacterStyle = 'Highlighted';
}
}
}