Timeout in long Pages document

I need to change the font of every occurrence of a certain character in a 650-page Pages document. The following script times out. If I copy and paste 50 pages from the document into a new document, the script takes a long time, but finishes. If I copy and paste 100 pages into a new document, it times out.

How can I get this script or a functionally equivalent one to run successfully on the whole 600-page document?

tell application "Pages"
	activate
	tell body text of front document
		set font of every character where it is "»" to "Iowan Old Style"
	end tell
end tell

I don’t think a “with timeout of 6000 seconds” is going to help here. Just retrieving the “where-filtered” data from a 100 page simple document runs more than 10 minutes ( no idea how much more ).

Something like this should is more efficient.

--Running under AppleScript 2.8, MacOS 15.5
tell application "Pages"
    version -->14.2
    tell document 1
        activate
        repeat with pageIndx from 1 to length of (get pages)
            tell page pageIndx
                tell body text
                    set (font of every character where it is "»") to "Impact"
                end tell
            end tell
        end repeat
    end tell
end tell

But oddly ( set (font of every character where it is “»”) to “Impact” ) doesn’t behave as expected here.
Only the first of the “»” characters on a page are modified. Does this line affect multiple instances of the target character in your environment?

Repeating the set font line twice affects 2 instances of “»” on a page, triplicating that line makes it affect 3 instances. I don’t know why. It makes no sense to me.

This code modifies the font of all “»” characters ( 2 per page ) in a 100 page document. It runs to completion in 34 seconds here.

tell application "Pages"
	version -->14.2
	tell document 1
		activate
		repeat with pageIndx from 1 to length of (get pages)
			tell page pageIndx
				set occurrances to my characterInstanceCounter(body text)
				tell body text
					repeat occurrances times
						set (font of every character where it is "»") to "Arial"
					end repeat
				end tell
			end tell
		end repeat
	end tell
end tell


on characterInstanceCounter(theText)
	set AppleScript's text item delimiters to "»"
	set theTextItems to text items of theText
	return (length of theTextItems) - 1	
end characterInstanceCounter

Thank you, Paul! Beautiful. And you responded very quickly. I cannot thank you enough.

Why not use this construct instead:

use scripting additions

set charList to {"«", "»"} as list
set theFont to "BookAntiqua-Bold"
set theColor to "blue"
set fontSize to "16"

tell application "Pages"
	activate
	tell body text of front document
		repeat with achar in charList
			set font of (characters where it is achar) to theFont
			set color of (characters where it is achar) to theColor
			set size of (characters where it is achar) to fontSize
		end repeat
	end tell
end tell
return

Thus, this Hoefler Text original:

Screenshot 2025-08-26 at 1.51.43 PM

gains the following on every page where the charList characters appear:

Screenshot 2025-08-26 at 1.54.19 PM

Thanks for the script, VikingOSX!

It timed out.

Must I do something about “use scripting additions”?

Your code works very well on smaller documents or on a single page. But I think the document in question being 100 pages prevents Pages from ever returning a result to the first “body text” query within any reasonable timeframe, and perhaps at all. The app just spins the progress wheel here until it is force-quit.

Acting page-by-page reduces the text to be searched, and the time required to return a result, to very reasonable amounts and durations. The difference is dramatic. 10 minutes was insufficient for Pages to return a result from the whole doc while all 100 pages processed in half a minute when acted on page-by-page.

Yes, Paul. My previous example was a dawg. So I shifted my test to a 181-page (269,085 character) Pages document of the first chapter of “The Invisible Man,” and changed the font and color of every “?” and “!” character. That took 17s with the following script in Script Debugger 8.0.10 with decidely no timeout.

use scripting additions

set charList to {"?", "!"} as list
set theFont to "BookAntiqua-Bold"
set theColor to "blue"
set fontSize to "16"

tell application "Pages"
	activate
	tell front document
		repeat with pageIdx from 1 to (count of every page of it)
			tell body text of page pageIdx
				repeat with achar in charList
					set font of (characters of it where it is achar) to theFont
					set color of (characters of it where it is achar) to theColor
					# set size of (characters where it is achar) to fontSize
				end repeat
			end tell
			log (pageIdx) as integer
		end repeat
	end tell
end tell
return

I have always begun my AppleScript code with use scripting additions. Recommend you do too. Here are some of the reasons you may wish to use scripting additions from querying AI:

Scripting additions, also known as OSAX (Open Scripting Architecture eXtensions), are used in AppleScript code to extend the language’s capabilities and provide functionality beyond what is natively available in AppleScript or through scriptable applications.

Reasons for using scripting additions include:

Expanding AppleScript’s Vocabulary:

Scripting additions introduce new commands and classes that are not part of AppleScript’s core features. For instance, the “Standard Additions” scripting addition provides commands for user interaction (like displaying dialogs), file system operations (like reading/writing files), date functions, and text/mathematical operations. Without these additions, AppleScript would be unable to perform many basic actions unless they were provided by a specific application.

Accessing System-Level Features:

Scripting additions can provide access to functionalities that are not exposed by scriptable applications, allowing AppleScript to interact more directly with the macOS operating system.

Adding Functionality from Lower-Level Languages:

Scripting additions are often written in lower-level languages like C, enabling them to offer performance benefits or access to system resources that are not easily managed directly within AppleScript.

Creating Custom Commands:

Developers can create their own scripting additions to add specialized commands tailored to specific needs, further extending AppleScript’s versatility.

Reducing Reliance on Applications:

By providing system-level or common functionalities, scripting additions can reduce the need for AppleScript to interact with specific applications for every basic task, making scripts more independent and potentially more robust.

Thanks for the second go!

This script changes only the first instance or first two instances on each page.

Thanks for the explanation of scripting additions, VikingOSX.

This Ai answer is nonsense.
Third party Scripting Addition (OSAX) was disabled in macOS 10.15.

Think by yourself.

2 Likes

I didn’t need an aged AI explanation to govern my continued usage of the use scripting additions in my AppleScript code. Even Script Debugger 8.0.10 still inserts this preamble in new AppleScript documents.

I do not use third-party OSAX in any AppleScript code, and I cannot assume users of my scripts are only macOS 10.15 or later.

“use scripting additions” is a description to use macOS default installed “Standard additions”.
OK?

I believe that not treating obvious lies as lies, not re-examining the content in one’s own mind, and writing content that is clearly a lie at a glance is more than just a “crime”; it is a “betrayal of humanity.”

I simply advise you to delete the above post. It has no meaning, no value, nothing. It’s just harmful.

Don’t post empty content fabricated by AI to look cool.

I have no tolerance for discussions that degrade into exaggerations and personal attacks. I ask that everyone do their best not to allow that to happen.

I doubt very much that VikingOSX intended to lie or is lying. They are certainly not committing crimes or betraying humanity. And I doubt very much that their intention was to look cool. It is clear to me that they are trying only to be helpful. I appreciate the help, and I also appreciate the discussion about whether or not “use scripting additions” is appropriate. Let’s stay focused on that.

Let us assume good intentions, be kind to each other, and work together on solutions. If we don’t, this forum loses value.

I like your tidy script. Do you know why it changes only the first one or two occurrences of the characters on each page?

OK. Cool it please — everyone. Don’t attack other posters. Don’t tell them what they can or can’t post unless you’re a moderator or administrator here or you want to be suspended from the forum.

https://www.macscripter.net/guidelines#community

The command use scripting additions is in most cases optional. Commands from the StandardAdditions will usually be recognised whether the expression’s used or not. It only becomes necessary when some other use command appears in the script and you still want to use StandardAdditions commands.

display dialog "Hello" -- works
use framework "Foundation"

display dialog "Hello" -- doesn't compile
use framework "Foundation"
use scripting additions

display dialog "Hello"  -- works
use framework "Foundation"

using terms from scripting additions
	display dialog "Hello"  -- works
end using terms from
1 Like

I have spent several hours today trying to refine this AppleScript solution to change every occurrence of characters in charList on a given page to theFont and it has done nothing but waste my time. So I am done working on this.

Thank you very much for trying!

It would be helpful if you could post your very large Pages document so we can test on it.

Hi Robert. I’m sorry that I cannot because it is all personal writing. It is a standard Pages word processing document without text boxes. The challenge should be the same with any 650-page document, right? Here is a link to 650 pages of “War and Peace” in Pages format:

https://user.fm/files/v2-fdfdce5ba514fe195e22da07616082f9/War%20and%20Peace.pages

Many thanks for your willingness to work on this matter.

Please note that paulskinner’s script works. It takes three minutes. I must run it twice to change two characters. I’m satisfied. But I appreciate people’s interest in writing the best possible script.