paragraph color for a specific font size in the Mac page document

I want to change the paragraph color for a specific font size in the Mac page document.
This method works fine, but it takes quite a bit of time to process if there are many document pages.
I would like to know if this method is best.
Could it be further optimized?

tell application "Pages"
	tell document 1
		if document body is false then error number -128
		repeat with pagenum from 1 to count of page
			tell page pagenum
				set myList to shapes
				repeat with thisShape in myList
					repeat with p in every paragraph of object text of thisShape
						-- if font size is 12pt, then change the font color to red
						if (size of p) is equal to 12 then
							set the color of p to {65535, 0, 0}
						end if
					end repeat
				end repeat
			end tell
		end repeat
	end tell
end tell

1 repeat loop is sufficient:


tell application "Pages" to tell document 1
	repeat with theShape in (get shapes)
		tell (every paragraph of object text of theShape whose size is 12)
			set its color to {65535, 0, 0}
		end tell
	end repeat
end tell

Awesome!!! Your source code runs much faster(x7.2) than the code I wrote.
However, if the pages document exceeds 1000 pages, the following error occurs.

error “Pages got an error: AppleEvent timed out.” number -1712

Is there a good solution?

Default value for timeout of each code line is 2 minutes (120 seconds). The solution is changing the default value to some bigger timeout:


with timeout of 6000 seconds -- 100 minutes, for example
	tell application "Pages" to tell document 1
		repeat with theShape in (get shapes)
			tell (every paragraph of object text of theShape whose size is 12)
				set its color to {65535, 0, 0}
			end tell
		end repeat
	end tell
end timeout

Your script falls into an infinite loop
A document size of up to 100 pages is fine, but if it is more than 200 pages, this phenomenon is found.
If I find the cause, I will write it again here.
KniazidisR! Your answers have been a great help to me. Thank you!

“it goes to lockup state”

It is not clear what you mean by this
The loop in the script can’t be infinite, since the number of shapes in the document is finite. You should probably be patient and wait for the script to finish.

It is unlikely that you will need as much time as in the specified timeout, but it can be several minutes if there are a lot of shape objects.

I tested on my Mac with 2240 shapes. Getting this shapes took on my Mac about 5 min 16 sec

It’s hard to do anything about it. I just want to point out that getting the shapes list only takes 1/4 of the entire script execution time.

In terms of paragraph handling, I would improve the script aesthetically by providing a progress bar to loop. So that you no longer think that the script is “frozen”:


with timeout of 600 seconds -- 10 minutes, for example
	-- this tooks about 5 minutes for 2000 shapes
	tell application "Pages" to tell document 1 to set theShapes to shapes
end timeout

set shapesCount to count of theShapes
set progress total steps to shapesCount

using terms from application "Pages"
	repeat with i from 1 to shapesCount
		set theShape to item i of theShapes
		tell me to set progress description to "Shape " & i & " of " & shapesCount
		tell (every paragraph of object text of theShape whose size is 12)
			set its color to {65535, 0, 0}
		end tell
		tell me to set progress completed steps to i
	end repeat
end using terms from

I’m not sure either.
But, what is certain is the fact that a timeout occurs
I sent a sample file to your email address. I wish you refer to that
I apologize if it was unpleasant for me to send you an email without your consent

When testing with the sample file I sent, you need to test the font size as 10 instead of 12 as shown below.
tell (every paragraph of object text of theShape whose size is 10)

Thank you.

Hello farmingmoney again.

I received your sample by email. Unfortunately my Pages couldn’t open it. I do not know why. But I found the best way to solve your problem. Instead of getting a list of shapes, it’s better to handle each page directly. This makes it possible to avoid a timeout.

And, as I found, counting by pages is 3 times faster than counting by shapes. So, the following script is 3 times faster than my previous suggestions, and at least 6 times faster than your original script

I again leave the progress bar for user friendliness of the interface. It will also help the user to see how many pages are in total and approximately how long the script will need:

-- about 7 minutes for 400 pages with 2000 shapes
tell application "Pages" to tell document 1 to set thePages to pages

set countPages to count thePages
set progress total steps to countPages

repeat with i from 1 to countPages
	using terms from application "Pages"
		tell (item i of thePages)
			set (color of every paragraph of object text of every shape whose size is 12) to {65535, 0, 0}
		end tell
	end using terms from
	set progress completed steps to i
end repeat

And, the shortest working solution, but without progress bar is this:


tell application "Pages" to tell document 1
	with timeout of 6000 seconds
		set (color of paragraphs of object text of shapes of pages whose size is 12) to {65535, 0, 0}
	end timeout
end tell

Hello KniazidisR
Your new code helps with this problem right now. No more time-outs
I learned a lot of things through this case
The way to debug using “Displaying Progress” was particularly impressive
Your active activities on macscripter.net has been a great help to all.
I give you respect and thanks :wink: