0

I'm converting AppleScript code to JXA and I'm stuck because I can't figure out how to replace placeholderTexts.

I tried to replace whose with where but no success.

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 is:

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

Thanks in advance for any help!

wchongo
  • 31
  • 4

2 Answers2

3

After trying a lot, I found 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]

wchongo
  • 31
  • 4
0

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

In my experience, this does not work. Replacing the first placeholderText effectively removes it from the placeholderTexts element. So this "array" is one element shorter than it was before the replacement. Which in turn means that you always have to do the set() on the first placeholderText element, i.e. p[0].set(newText).

chrillek
  • 21
  • 3