Applescript to find and replace preset 'variables' inside a pages document

Hi all,


Could anybody help me with an applescript or any advice? I have a siri shortcuts workflow however much of my time daily is spent using "find and replace" inside the pages app. For example Find [Name] and replace with "John Smith". There are around 12 variables.


Is there any way I can use variables from within a siri shortcut, and then run an apple script to find placeholders within the document and replace them with variables I have already been asked for?


In short, any way to automate find and replace inside a pages document using shortcuts or automator?


Many thanks in advance for any help...

MacBook Pro (M3 Pro, 2023)

Posted on Feb 13, 2024 3:31 PM

Reply
Question marked as Top-ranking reply

Posted on Feb 14, 2024 5:00 AM

Here is an AppleScript that replaces all placeholder text values in a Pages document that are listed in the keys_array with the dynamic list of Siri replacement strings that are in the values_array. The trick in a Shortcuts workflow is to pass a list of those replacement strings into a Run AppleScript input variable and use that as the values_array.


Code:


-- placeholder.applescript
(*
Replace every occurrence of key_array text strings in a Pages document with
their corresponding item from the values_array.

Reference: https://discussions.apple.com/thread/255475539?sortBy=oldest_first
Tested: macOS Sonoma 14.3, Pages v13.2
VikingOSX, 2024-02-14, Apple Support Communities, No warranties expressed or implied.
*)

use scripting additions

-- name of your placeholder text fields
set key_array to {"[Test_01]", "[Test_02]", "[Test_03]", "[Test_04]", "[Test_05]", ¬
	"[Test_06]", "[Test_07]", "[Test_08]", "Test_09]", "[Test_10]", ¬
	"Test_11]", "[Test_12]"}
	
-- the list of replacement items for this test
-- the real list will need to be passed from the Siri Shortcut action into a Run AppleScript action
set value_array to {"Var_01", "Var_02", "Var_03", "Var_04", "Var_05", ¬
	"Var_06", "Var_07", "Var_08", "Var_09", "Var_10", ¬
	"Var_11", "Var_12"}

if not ((count of key_array) = (count of value_array)) = true then return

tell application "Pages"
	launch
	if not (exists front document) then
		display alert "Pages document must be open for this script to function properly."
		if it is running then quit
		return
	end if
	
	tell front document
		repeat with i from 1 to count of key_array
			set theTags to (the tag of every placeholder text whose tag contains (item i of key_array))
			repeat with j from 1 to count of theTags
				set thisTag to item j of theTags
				set (every placeholder text whose tag is thisTag) to (item i of value_array) as text
			end repeat
		end repeat
	end tell
end tell
return


Results on a Pages document with 12 Placeholder text fields:


Similar questions

8 replies
Question marked as Top-ranking reply

Feb 14, 2024 5:00 AM in response to smh9321

Here is an AppleScript that replaces all placeholder text values in a Pages document that are listed in the keys_array with the dynamic list of Siri replacement strings that are in the values_array. The trick in a Shortcuts workflow is to pass a list of those replacement strings into a Run AppleScript input variable and use that as the values_array.


Code:


-- placeholder.applescript
(*
Replace every occurrence of key_array text strings in a Pages document with
their corresponding item from the values_array.

Reference: https://discussions.apple.com/thread/255475539?sortBy=oldest_first
Tested: macOS Sonoma 14.3, Pages v13.2
VikingOSX, 2024-02-14, Apple Support Communities, No warranties expressed or implied.
*)

use scripting additions

-- name of your placeholder text fields
set key_array to {"[Test_01]", "[Test_02]", "[Test_03]", "[Test_04]", "[Test_05]", ¬
	"[Test_06]", "[Test_07]", "[Test_08]", "Test_09]", "[Test_10]", ¬
	"Test_11]", "[Test_12]"}
	
-- the list of replacement items for this test
-- the real list will need to be passed from the Siri Shortcut action into a Run AppleScript action
set value_array to {"Var_01", "Var_02", "Var_03", "Var_04", "Var_05", ¬
	"Var_06", "Var_07", "Var_08", "Var_09", "Var_10", ¬
	"Var_11", "Var_12"}

if not ((count of key_array) = (count of value_array)) = true then return

tell application "Pages"
	launch
	if not (exists front document) then
		display alert "Pages document must be open for this script to function properly."
		if it is running then quit
		return
	end if
	
	tell front document
		repeat with i from 1 to count of key_array
			set theTags to (the tag of every placeholder text whose tag contains (item i of key_array))
			repeat with j from 1 to count of theTags
				set thisTag to item j of theTags
				set (every placeholder text whose tag is thisTag) to (item i of value_array) as text
			end repeat
		end repeat
	end tell
end tell
return


Results on a Pages document with 12 Placeholder text fields:


Feb 15, 2024 2:37 AM in response to smh9321

I do not use Siri, or use it to run any Shortcut, but there is a provision for Siri to do that. One could tell Siri to run a Shortcut that may only be the Run AppleScript action and then the matter of how to pass a list to it is no longer a concern.


The AppleScript dictionary for Pages has no provision for keywords to control Find and Replace.


The Shortcut that Siri invokes could use AppleScript display dialog to prompt you for a single instance of a Placeholder text variable and its replacement string to apply to an already open Pages document. That is how you get interactive content into AppleScript.


Or, an AppleScript choose file to prompt you for a CSV containing those 12 Placeholder items and their corresponding replacement strings. The AppleScript would build the two lists from that CSV and then perform the Placeholder processing. That code was written and successfully tested last evening.


The code I have already provided would change any of the key_array Placeholder names wherever they appeared in the entire Pages document with the corresponding replacement value, but for simplicity, I kept the demo to one page.

Mar 8, 2024 4:34 AM in response to Simon_Z_deepdive

Rather than attempt to pass several variables into AppleScript in a Shortcuts workflow, it would be better to write each variable, one per line, in a text file. Then, one can read that text file and get the individual lines into an AppleScript list:


use scripting additions

set vfile to ((path to desktop as text) & "variables.txt") as text
set value_array to read file vfile as text using delimiter linefeed


You don't need Finder or System Events to do the above and you can construct your external variables into a list (array) before invoking Pages in the Run AppleScript action to replace your respective Placeholder text items with the individual variable_array mapping.

Mar 8, 2024 5:39 AM in response to smh9321

Agreed. Since it is not a single-use (you) Shortcut, then you should initiate the dialogs in the AppleScript after you build the Placeholder key_array and dynamically build the value_array from your dialog responses.


set value_array to {}
set client_name to text returned of (display dialog "Enter Client Name: " default answer "") as text
copy client_name to the end of value_array


You may want to harden your dialog prompts so there must be an entry, and in the following example it will loop with a prompt until text is entered.


set value_array to {}
repeat while true
     set client_name to text returned of (display dialog "Enter Client Name:" default answer "") as text
    if not (client_name is "") = true then exit repeat
end repeat

︙

if not ((count of key_array) = (count of value_array)) = true then return



Feb 14, 2024 2:15 PM in response to VikingOSX

Viking, first thank you very much for your help. You have helped me in the past on these forums too and made my life much easier and more enjoyable!


In relation to this, i’m struggling to get this to work. I have added the ‘run applescript’ action at the end of my siri shortcut, however I don’t know how to get the pre-selected variables into the AppleScript.


To give an example, in my shortcut, i have a ‘client name’ variable. Let’s pretend this is John Smith. My shortcut takes a file, renames it ‘John Smith’ and moves it to another folder.


Is there any way that the applescript can take the shortcut variable (which is John Smith in this example) then change this everywhere in the document where it says [client name]?


I hope I have explained my question properly and apologise as I’m not too good with scripting.


Thanks again


Mar 7, 2024 6:38 PM in response to smh9321

Im currently in the process of writing a shortcuts workflow which is collecting contacts variables and handing it over to apple script where they should get translated and replace placeholder text.


After a few hours and this explanation, im stuck at the translating of variables.

There is the conversion from Test to Var as its suggested, I just don't manage to get the real Text from outside apple scripts displayed instead of Var_01.


Any hint is greatly appreciated

Mar 8, 2024 4:53 AM in response to VikingOSX

Personally speaking, the whole idea of what I am trying to do is work with shortcuts so that a dialogue prompt appears on the screen each time asking for ‘client name?’ And ‘client DOB’ etc. This is so new members of staff can quickly and easily edit a form in a way they understand. I think replacing everything in a text file would completely go over most people’s heads.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript to find and replace preset 'variables' inside a pages document

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