如何使用JXA替换Pages(iWork)中的占位符文本



我正在将AppleScript代码转换为JXA,我被卡住了,因为我不知道如何替换占位符文本。

我试着用where代替which,但没有成功。

这是代码:

var PagesApp = Application("Pages");
var theseTags = PagesApp.documents[0].placeholderTexts.tag()
var uniqueTags = []
for(var i=0; i < theseTags.length; i++){
var thisTag = theseTags[i];
if (!(uniqueTags.includes(thisTag))) {
uniqueTags.push(thisTag);    
}
}
var theDate = "20200326"
for(var i=0; i < uniqueTags.length; i++){
var thisTag = uniqueTags[i];
if (thisTag.includes("theDate")) {
PagesApp.documents[0].placeholderTexts.whose({tag: thisTag}).tag = theDate; // Error: Error: Invalid key form.
}   
}

AppleScript中的错误行是:

set (every placeholder text whose tag is thisTag) to theDate

提前感谢您的帮助!

经过多次尝试,我找到了解决方案:

PagesApp.documents[0].placeholderTexts.whose( { tag: thisTag })[i].set(theDate);

注意事项:1.如果要替换一个占位符文本,请使用[0]2.如果您想替换多次出现的占位符文本,只需使用变量[i]将行作为占位符的次数重复即可

如果您想替换多次出现的占位符文本,只需使用变量[i]将行重复为占位符的次数

根据我的经验,这是行不通的。替换第一个placeholderText可以有效地将其从placeholderTexts元素中删除。所以这个";阵列";比更换前短了一个元件。这反过来意味着总是必须在第一个占位符Text元素上执行set(),即p[0].set(newText)

最新更新