HELP - How to replace placeholderTexts in Pages (iWork) using JXA?

I’m getting error on last line trying to replace every placeholderText tag.

I'm stuck for a couple of days trying to fix the error.


Thanks a lot for any kind of help.


Here's the code:


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.


}

}


The error line in AppleScript works well :


set ( everyplaceholder textwhose tag is thisTag) to theDate


Replies

“The error line in AppleScript works well”


I think you’ve found your solution: stick to AppleScript. The language may be a mess, but at least it knows how to speak Apple events right. ScriptingBridge and JXA don’t work right; never have, never will.

After trying a lot, I found the solution. Thanks for everyone who tried to help me, specially hhas01.

Here is the solution:


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


Notes:

1. If you want to replace one placeholderText use [0]

2. If you want to replace placeholderText that appears more than once, just repeat the lines as number of times of placeholder using variable [i]