iWork Pages - comparing the character style of a word in text

I have to Find and Replace a character style in a text.

I am trying this Applescript code:

set oldStyle to character style "oldStyle"

set oneWord to item 1 of theText
set charStyleWord to the character style of oneWord  -- it crashes here
if charStyleWord is equal to oldStyle then ....

the script stops on the line I marked saying
“can’t get character style of XXX”

XXX represents the first word of the document.

what am I missing? How do I get the style used in a word and compare it to another?

thanks

Hi, macos_boy.

‘oneWord’ is probably some text which has been returned to the script. You can only get the ‘character style’ of text in the Pages document:

tell application "Pages"
	set charStyleWord to the character style of <blah blah blah> of document x
end tell

Thanks. I have omitted because I thought it was obvious, but this block of code is inside a tell Pages, tell document 1. This is the complete code. My idea is to search for words on document 1 and create a list of these words and which pages they appear on document 2

In this example, it is searching for all words that are marked with the character style equal to “oneStyle”


set theCurrentPage to 0

tell application "Pages"
	set nom1 to name of document 1
	set nom2 to name of document 2
	tell document nom1
		set aStyle to character style "Accentuation" --"oneStyle"
		repeat with aWord in every word
			if character style of aWord is aStyle then
				set thePageNumber to the page number of the containing page of aWord
				if thePageNumber is not equal to theCurrentPage then
					copy thePageNumber to theCurrentPage
					my rapporteur(nom2, text of aWord, thePageNumber)
				end if
			end if
		end repeat
	end tell
end tell

on rapporteur(nom_2, theText, the_PageNumber)
	tell application "Pages" to tell document nom_2 to tell body text
		make new paragraph at end with data (theText & ", " & the_PageNumber) as text
	end tell
end rapporteur

We can’t “speak” to document 2 in a block “speaking” to document 1.
Doing that is understood as
tell document 2 of document 1 .
which is clearly incorrect.
This is why I moved the task in a handler.

I’m a bit surprised because your script report a single word per page even it there are several ones.

Here is a modified version returning every occurrences.


tell application "Pages"
	set nom1 to name of document 1
	set nom2 to name of document 2
	tell document nom1
		set aStyle to character style "Accentuation" --"oneStyle"
		repeat with aWord in every word
			if character style of aWord is aStyle then
				my rapporteur(nom2, text of aWord, page number of the containing page of aWord)
			end if
		end repeat
	end tell
end tell

on rapporteur(nom_2, theText, the_PageNumber)
	tell application "Pages" to tell document nom_2 to tell body text
		make new paragraph at end with data (theText & ", " & the_PageNumber) as text
	end tell
end rapporteur

As you may see, I don’t use index which is an efficient way to fasten the code.

Yvan KOENIG (VALLAURIS, France) samedi 2 juillet 2011 20:48:46

Wow Yvan, thanks for your fantastic code.

There’s just one tiny problem with this script. It prints out the wrong page number. The problem is that the script is not considering the page number a section is starting. For example: if I have my section starting at page 100 and it finds a word in the first page it will print “1” as the page and not 100.

any thoughts on how to fix that?

thanks.

Hi Yvan,

I have modified your code to

tell application "Pages"
	set nom1 to name of document 1
	set nom2 to name of document 2
	tell document nom1
		set aStyle to character style "TermoIngles" --"oneStyle"
		repeat with aWord in every word
			if character style of aWord is aStyle then
				set pagex to page number of the containing page of aWord
				set start to page number start of the containing section of page pagex
				set numeroTotal to pagex + start
				my rapporteur(nom2, text of aWord, numeroTotal)
			end if
		end repeat
	end tell
end tell

on rapporteur(nom_2, theText, the_PageNumber)
	tell application "Pages" to tell document nom_2 to tell body text
		make new paragraph at end with data (theText & ", " & the_PageNumber) as text
	end tell
end rapporteur

and it is now working perfectly.

THANKS! You are a legend!