Find and replace a string of characters with bold text, in Pages

My question is a follow on to this post: How do I Find and Replace w Bold? - Apple Community

A script was recommended as a work around for this function, and it worked great for individual words, but not for a string of characters like this: "[C]" or "[A]", etc. I use this format to represent chords in song sheets, and I like to make them bold. I did this all the time on a windows machine, and now that I've converted to MAC I'm trying to find a way to do it here. For some reason this script doesn't work on my chords, although it works great with plain 'words'. I'm hoping someone familiar with coding these scripts can tell me what needs to be changed to make it work on a string of characters like [C] or [A].

Thanks!

MacBook Pro 16″, macOS 12.3

Posted on May 16, 2022 5:31 AM

Reply
Question marked as Top-ranking reply

Posted on May 16, 2022 8:59 AM

The way Apple defines a word in Pages body text causes it to filter characters and thus, it returns an "A" when it encounters whitespace around "[A]". So I have code that can match capital letters A through G, bold them in the original font's bold face, and even color them if you want. It does not bold the square brackets as that would require scanning all the characters in the document and program logic on what to do when encountering the "[x]" triad. More development and testing time that I do not have to donate.


The following was tested with Pages v12 on macOS 11.6.5 and the document font in this case was Helvetica Regular.

Before:

After:


Note: I already tried a list named letterNotation containing these characters and used as where it is in letterNotation and it failed, so I had to do the mutliple or it clauses to get it done.


Code:


use scripting additions

set highlightColor to "blue"
set txtFormat to "Bold"

tell application "Pages"
	activate
	tell body text of front document
		repeat with aword in (words where it is "A" or it is "B" or it is "C" or it is "D" or it is "E" or it is "F" or it is "G")
			
			log (aword as text)
			tell application "Font Book"
				set ffnames to name of (font family (font of aword))'s typefaces
			end tell
			set font_name to (aword's font & "-" & txtFormat)
			set font_name_alt to (aword's font & space & txtFormat)

			if ffnames contains font_name or ffnames contains font_name_alt then
				-- failure is tolerated if missing font_name or highlightColor
				try
					set aword's font to font_name
					set aword's color to highlightColor
					set matchCnt to matchCnt + 1
				on error
					set aword's font to font_name_alt
					set aword's color to highlightColor
				end try
			end if
		end repeat
	end tell
end tell
return



Similar questions

8 replies
Question marked as Top-ranking reply

May 16, 2022 8:59 AM in response to Big22hair

The way Apple defines a word in Pages body text causes it to filter characters and thus, it returns an "A" when it encounters whitespace around "[A]". So I have code that can match capital letters A through G, bold them in the original font's bold face, and even color them if you want. It does not bold the square brackets as that would require scanning all the characters in the document and program logic on what to do when encountering the "[x]" triad. More development and testing time that I do not have to donate.


The following was tested with Pages v12 on macOS 11.6.5 and the document font in this case was Helvetica Regular.

Before:

After:


Note: I already tried a list named letterNotation containing these characters and used as where it is in letterNotation and it failed, so I had to do the mutliple or it clauses to get it done.


Code:


use scripting additions

set highlightColor to "blue"
set txtFormat to "Bold"

tell application "Pages"
	activate
	tell body text of front document
		repeat with aword in (words where it is "A" or it is "B" or it is "C" or it is "D" or it is "E" or it is "F" or it is "G")
			
			log (aword as text)
			tell application "Font Book"
				set ffnames to name of (font family (font of aword))'s typefaces
			end tell
			set font_name to (aword's font & "-" & txtFormat)
			set font_name_alt to (aword's font & space & txtFormat)

			if ffnames contains font_name or ffnames contains font_name_alt then
				-- failure is tolerated if missing font_name or highlightColor
				try
					set aword's font to font_name
					set aword's color to highlightColor
					set matchCnt to matchCnt + 1
				on error
					set aword's font to font_name_alt
					set aword's color to highlightColor
				end try
			end if
		end repeat
	end tell
end tell
return



May 18, 2022 2:45 PM in response to Big22hair

I have revised the script and now it handles singular (Verdana, Helvetica, etc.) or compound (Book Antiqua, EB Garamond, Bodoni 72 Oldstyle, etc.) fonts equally well. If something is already bolded, or the particular font used has no installed bold style, then the text will only receive coloration.


use scripting additions

set highlightColor to "blue"
set txtFormat to "Bold" as text

tell application "Pages"
	activate
	tell body text of front document
		repeat with aword in (words where it is "A" or it is "B" or it is "C" or it is "D" or it is "E" or it is "F" or it is "G")
			
			set font_name to aword's font
			# we need the base font name whether singular, or a compound name broken into component names
			set font_name_parts to (do shell script "sed -E 's/([A-Z])/ \\1/g;s/- / /;s/ //1' <<<" & font_name)
			
			# derive the base font name so one can get its family members later
			if (count of font_name_parts) = 3 then
				set font_name to ((word 1 of font_name_parts) & space & (word 2 of font_name_parts)) as text
			else if ((count of font_name_parts) ≤ 2) then
				set font_name to word 1 of font_name_parts
			end if
			
			tell application "Font Book" to set family_names to (name of (font family font_name)'s typefaces) as list
			
			set bold_font to (font_name & space & txtFormat) as text
			
			if (family_names contains bold_font) then
				set aword's font to bold_font
			end if
			set aword's color to highlightColor
		end repeat
	end tell
end tell
return


May 18, 2022 11:33 AM in response to Big22hair

An issue that I have discovered is that if a font of a chosen word is already Helvetica Bold, the script asks Font Book for the name of that word's font and it returns "Helvetica-Bold", which is the Postscript name of the font, but the font name that is in the list of typefaces for Helvetica bears the full name "Helvetica Bold". Thus the hyphenation causes the list membership check to fail.


I am in the process of rewriting the script to accommodate these naming disparities.

May 16, 2022 3:08 PM in response to VikingOSX

Thanks! I've done a little programming (in the distant past) so I know how time consuming coding and testing can be, I appreciate your help. Perhaps the error message I get is a simple fix, if not don't worry about it... Here is the error message:


error "Font Book got an error: Can’t get font family \"Helvetica-Bold\"." number -1728 from font family "Helvetica-Bold"

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.

Find and replace a string of characters with bold text, in Pages

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