我正在尝试在活动页面上找到所有“#”并将其更改为数字,即。 1,2,3。 。
以下代码是我认为可行的,但事实并非如此。相反,它将每个“#”更改为“0”。
app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;
app.findTextPreferences.findWhat = "#";
var finds = app.activeDocument.findText();
if (finds.length > 0) {
for (var i = 0; i < finds.length; i++)
{
app.changeTextPreferences.changeTo = "no: " + i;
app.activeDocument.changeText();
}
else
{
alert("Nothing has been found");
}
}
答案 0 :(得分:1)
正如ali haydar所说,changeText将全局应用并将打破以前的findText文本引用。你需要的是在你的循环中使用contents属性。
app.findTextPreferences = app.changeTextPreferences = NothingEnum.NOTHING;
app.findTextPreferences.findWhat = "#";
var finds = app.activeDocument.findText();
if (finds.length > 0)
{
for (var i = 0; i < finds.length; i++)
{
finds[i].contents = "no: " + String(i);
}
}
else
{
alert("Nothing has been found");
}
答案 1 :(得分:0)
事实上你甚至可以摆脱if条件:
app.findTextPreferences = app.changeTextPreferences = null;
app.findTextPreferences.findWhat = "#";
var finds = app.activeDocument.findText();
var n = finds.length;
var nStart = n;
while (n-- ) finds[n].contents = "no: " + String(n+1);
alert(nStart? nStart+" replacements made…" : "Nothing has been found");
答案 2 :(得分:0)
仅考虑第1页......
var main = function(){
var doc = app.properties.activeDocument,
finds,n;
app.findTextPreferences = app.changeTextPreferences = null;
app.findTextPreferences.findWhat = "#";
if ( !doc ) return;
finds = doc.findText();
n = finds.length;
while (n-- ) {
finds[n].parentTextFrames.length
&& finds[n].parentTextFrames[0].isValid
&& finds[n].parentTextFrames[0].parentPage.id==doc.pages[0].id
&& finds[n].contents = "no: " + String(n+1);
}
};
main();