Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript - Placeholders no longer work after update to Pages 5.6

Hi,

I was successfully using an applescript that completes tagged placeholders in a Pages document with values from stored variables. After updating to Pages 5.6 the script run normally but the placeholder texts are no longer completed. The tags in the document template are still OK.


The line in the script is as follows:


set (every placeholder text whose tag is "dniPadre") to padre_dni

I have several of these lines but with different tags.


Thanks in advance for your answer.

MacBook Pro (Retina, 15-inch, Early 2013), OS X El Capitan (10.11)

Posted on Oct 22, 2015 1:01 PM

Reply
15 replies

Oct 22, 2015 1:32 PM in response to rderrico

I just created a new Pages v5.6 document with the following Placeholder text items on the left, and the replacement text on the right after running the included script. This is the same script that was working for Pages v5.5.3, and I made no changes to it for v5.6. Placeholders work just fine for me.

User uploaded fileUser uploaded file

property tagDoc : "[Document]" as text

property tagDate : "[Creation Date]" as text



tell application "Pages"


activate

tell front document


set theTags to the tag of every placeholder text

repeat with i from 1 to count of theTags

set thisTag to item i of theTags

if thisTag is equal to tagDoc then

set (every placeholder text whose tag is thisTag) to (name as text)

else if thisTag is equal to tagDate then

set (every placeholder text whose tag is thisTag) to "2015-08-16"

end if

end repeat


end tell

end tell

Nov 10, 2015 11:31 PM in response to rderrico

I think I have narrowed the issue somewhat. I've also had AppleScripts which create Pages documents based on templates using placeholder text inexplicably stop working after upgrading to Pages 5.6.


I can get an AppleScript to replace placeholder text in a Pages 5.6 document, but not if that document was made by the script using a template. The script doesn't work if it first makes a new document with template and then tries to replace the placeholder text in that document.



I think it can be illustrated with the following script, presuming you have the appropriate template installed:


tell application "Pages"


if document 1 exists then

set theDoc to document 1

else

set theDoc to makenewdocumentwith properties {document template:template "Example Template"}

end if


tell theDoc

if (the first placeholder text whose tag is "example placeholder") exists then say "Bingo!"

set every placeholder text whose tag is "example placeholder" to "Success!"

end tell


end tell


Try running that script twice and comparing the results.


Interestingly, if you set that script inside a repeat loop to repeat twice, the placeholder text doesn't get replaced. You have to actually run the script twice to get expected results.

Nov 11, 2015 5:40 AM in response to YetAnotherAppleUser

If you want to avoid the left truncation of posted AppleScript, first click the Use advanced editor in the upper right corner of this basic editor. Then post your AppleScript, select the code, and from the advanced editor toolbar, click on Style : Paragraph. It won't have the same formatting as in the AppleScript Editor, but it will be entirely readable.


Also, are you saving the freshly made document, and reopening it in the AppleScript — before attempting any tag activity?

Nov 11, 2015 12:36 PM in response to VikingOSX

I found that if I select inside the document that functionality is restored. For my script I added the bold italicized part, and it worked just fine:



tell application "Pages"


activate


set rptMonth to "October 2015"

set thisDocument to makenewdocumentwith properties {document template:template "kctr5"}


tell application "System Events"

tell process "Pages"

key code 0 usingcommand down

end tell

end tell


tell thisDocument

set (every placeholder text whose tag is "%MONTH%") to (rptMonth as text)

end tell

end tell


I know it's a hack for now until Apple fixes the bug, but hope it works for you.

Nov 11, 2015 12:20 PM in response to bdr518

Hi bdr518,


I tried your hack and it worked fine! However, if in the script I replace some placeholders, then I complete some cell values of a table that is in the document, and then try to replace other placeholders, these last ones don't get replaced. So I modified the script to replace all the placeholders at once, and then to complete the table cells. It seems that the text of the document gets "out of focus"...


Another problem that I am having with Pages 5.6 is that when I fill a merged table cell with a value, the cell gets unmerged again, so it changes the formatting of the table. But I modified the script to merge the cells again and it seems to work fine.


Thank you very much for all your answers!

Nov 11, 2015 12:36 PM in response to rderrico

Try this when you want to replace placeholders after editing a table:

tell application "System Events"

tell process "Pages"

key code 24 using command down --May need to do this line twice

key code 0 usingcommand down

end tell

end tell


The theory here is you hit cmd-return to switch between having the cell selected and the table, then you can select all content in the document. Again, still a hack.

Nov 11, 2015 3:07 PM in response to bdr518

Very helpful hack! Thank you! At first it didn't work for me because my script didn't tell Pages to activate. It had Pages do its thing in the background and then export a PDF of the document which would open for the user. So of course the trick of selecting all and directing focus to the text of the Pages document didn't work with Pages in the background. I've now changed it to tell Pages to activate and now the hack works like a charm.


Has Apple been informed of this issue? I hope they're aware of it.


Thanks too to VikingOSX for the tips about posting AppleScript. That was my first time. 🙂

May 21, 2016 4:28 AM in response to RebeccaDCrouch

Rebecca,


You can launch the Script Editor (Launchpad : Other : Script Editor). If you have previously saved a script as AppleScript (foo.applescript) source, it may be in the File menu : Open Recent sub-menu. Ultimately, you will save a copy of the script as Text (foo.applescript), and with the option key + File menu : Save As, as an application (foo.app) using the Application format. Optionally, you can hide the application extension.


On OS X 10.11.5 and using Pages v5.6.2, I just created (and saved) a new document that incorporated some Placeholder text. When I ran my (unrevised) AppleScript against it, the Placeholder text was properly replaced.


Is that what you wanted to know?

Applescript - Placeholders no longer work after update to Pages 5.6

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.