Find and Replace in Pages with different Styles

Hello,

I’ve found somewhere in this forum a script to find and replace some text with Applescript.
This script is working perfect, except for the ones that have a different style of text.

For example, if the first word in the document is bold, everything went bold after running the script.

This problem is not only with this kind of style, but also when I use a bullet list in the document.

The current state of the script is displayed at the bottom of this post.

My question is if anyone has a answer for me what I can use to change the script so that it will work like expected.
If anyone has another script that wil work the same way, but not having this problem it is also good.

This is the current state of the script:


            global editedText
            set before to "apple"
            set after to "google"
            tell application "Pages"
                tell document 1
                    set BodyText to body text
                end tell
            end tell
            
            my findAndReplace2(before, after, BodyText)
            
            tell application "Pages"
                tell document 1
                    set body text to editedText
                end tell
            end tell
            
            on findAndReplace2(toFind, toReplace, theText)
            set astid to AppleScript's text item delimiters
            set AppleScript's text item delimiters to toFind
            set textItems to theText's text items
            set AppleScript's text item delimiters to toReplace
            tell textItems to set editedText to beginning & toReplace & rest
            set AppleScript's text item delimiters to astid
            return editedText
            end findAndReplace2

(1) I was unable to compile your script because before and after can’t be used as variable names (they are reserved words).

(2) the described behavior is normal.
To get rid of this normal feature, you must use an other path.


property lesMots : {}

on run
	set before_ to "apple"
	set after_ to "google"
	
	tell application "Pages" to tell document 1
		set my lesMots to words
		considering case
			repeat with i from 1 to count of my lesMots
				if item i of my lesMots is before_ then set word i to after_
			end repeat
		end considering
	end tell
	set my lesMots to {}
end run

Yvan KOENIG (from FRANCE jeudi 25 juin 2009 12:20:45)

Hello,

Sorry for the first error, the reason of this is that those words where in dutch, I translate them for the example to an english word.
In the example in the bottom of this post I translate them back into dutch word’s. voor stands for before, and na stands for after.

The script that you have posted is not working, if I have page started, entered some text with the words that he must replace and run the script, then nothing changed in te document.
The output of the scripteditor is {}

Below is an example of the script in the current state.


property lesMots : {}

on run
	set voor to "apple"
	set na to "google"
	
	tell application "Pages" to tell document 1
		set my lesMots to words
		considering case
			repeat with i from 1 to count of my lesMots
				if item i of my lesMots is voor then set word i to na
			end repeat
		end considering
	end tell
	set my lesMots to {}
end run

(1) I often use local words too to name variables to get rid of conflicts.
There is no need to translate them to English as you did.

voor is perfect
GWBush or FidelCastro or AlCapone would behave flawlessly too.
If you translate too quickly as you did, you will replace it by before and it will noty compile.

(2) My guess is that in the original document, the spelling is not “apple” but “Apple”.

I deliberately used the

Considering case
instruction.

If you don’t wish this behavior, disable it and of course disable also the ‘end considering’ one.

Here the script behaves flawlessly:

Here is the log report:

tell application “Pages”
get every word of document 1
{“global”, “editedText”, “set”, “to”, “Apple”, “set”, “to”, “apple”, “tell”, “application”, “Pages”, “tell”, “document”, “1”, “set”, “lesMots”, “to”, “words”, “end”, “tell”, “end”, “tell”, “my”, “findAndReplace2”, “BodyText”, “tell”, “application”, “Pages”, “tell”, “document”, “1”, “set”, “body”, “text”, “to”, “editedText”, “end”, “tell”, “end”, “tell”, “on”, “findAndReplace2”, “toFind”, “toReplace”, “theText”, “set”, “astid”, “to”, “AppleScript’s”, “text”, “item”, “delimiters”, “set”, “AppleScript’s”, “text”, “item”, “delimiters”, “to”, “toFind”, “set”, “textItems”, “to”, “theText’s”, “text”, “items”, “set”, “AppleScript’s”, “text”, “item”, “delimiters”, “to”, “toReplace”, “tell”, “textItems”, “to”, “set”, “editedText”, “to”, “beginning”, “toReplace”, “rest”, “set”, “AppleScript’s”, “text”, “item”, “delimiters”, “to”, “astid”, “return”, “editedText”, “end”, “findAndReplace2”}
set word 8 of document 1 to “google”
end tell

As you may see, word 5 (Apple) is not replaced
Word 8 (apple) is replaced.

Yvan KOENIG (from FRANCE jeudi 25 juin 2009 13:12:17)

If you don’t want to take care of case, here is a more efficient code.


on run
	set before_ to "apple"
	set after_ to "google"
	
	tell application "Pages" to tell document 1
		set every word whose contents is before_ to after_
	end tell
end run

Yvan KOENIG (from FRANCE jeudi 25 juin 2009 13:30:42)

Hello,

First, thanks for your answer.
I’ve tried allready if the apple Apple make some difference, but it don’t.

From your last post, i’ve got another piece of code that really works good!
The document is alway’s the same at the point where this script is running, so I used the code on the bottom of the post.

I have only one (new) question left.

One of the documents where this script is made for, make use of a textblock with vertical text in it. Is there also a possibility for this to work?

Here’s my current working code block:


tell application "Pages"
	get every word of document 1
	set word 1 of document 1 to "test1"
	set word 2 of document 1 to "test2"
	set word 3 of document 1 to "test3"
	set word 4 of document 1 to "test4"
	set word 5 of document 1 to "test5"
end tell

P.s. I just sea your last post before is submit this one, so that post is not needed anymore. The problem is solved with this, only one question is left.

Model: Pages '09
AppleScript: 2.0.1
Browser: Firefox 3.0.11
Operating System: Mac OS X (10.5)

What means the question about a text box with vertical text ?
If it’s a rotated text box, it will behave exactly like other text blocks.
If it’s a box in which every letter appears on a different line there is no simple response.
We may get this feature at least three ways:
(1) insert RETURN between every characters
(2) insert lineBreak between every characters
(3) use monospaced font then reduce the block width so that only one character fit in a line.

Cas 3 is easy to treat:


set avant to "Busch"
set apres to "Obama"

tell application "Pages"
	tell document 1
		try
			set (words whose contents is avant) to apres
		end try
		set nbTbox to count of text boxes
		if nbTbox > 0 then
			repeat with i from 1 to nbTbox
				tell object text of text box i
					set (words whose contents is avant) to apres
				end tell
			end repeat
		end if
	end tell
end tell

Yvan KOENIG (from FRANCE jeudi 25 juin 2009 19:33:52)

Hello,

I look later to you’re last post, I want to make the other parts from the script first working.

I have a little script builded in applescript (copy, paste and some editing to work stand-alone) and this part is giving some problems.

This is the current script:


tell application "Pages"
	get every word of document 1
	set word 1 of document 1 to "organisatie test"
	set word 2 of document 1 to "contactpersoon test"
	set word 3 of document 1 to "adres test"
	set word 4 of document 1 to "postcode test"
	set word 5 of document 1 to "woonplaats test"
	set word 6 of document 1 to "land test"
	set word 8 of document 1 to "datum test"
	set word 10 of document 1 to "onderwerp test"
	set word 11 of document 1 to "aanhef test"
end tell

The problem with this script is that he replace a word in the script. But if the new word is with a space in the middle, he is not working good.
The next word is counting from the new document version the second word, and not from the origin.

I can’t say in the second part from the script that he must replace the third word, because the new word is not static but entered by the user in a dialog box.
It can be 1 word, 2 words but also for example 8 words.

How can I get a method that he work’s good? He must count the word from the original document, or he may count the words entered in the previous box and calculate for example:
4(original location) + 5 (words entered by user in the dialog box, in this example between the “”’ symbols) = the 9’th word that may be replaced.

How can i resolve this problem?

Model: Pages '09
AppleScript: 2.0.1
Browser: Firefox 3.0.11
Operating System: Mac OS X (10.5.7)

I don’t understand how your script was modifying itself.

Here it apply (with odd results) on the Pages document.

The culprit is your script’s design.

(1) get every word of document 1 is perfectly useless
(2) when it replace the word 1 by two words, the old word 2 becomes word 3 :wink:
This is a very classical problem.
The correct soluce would be to start from the higher index.


tell application "Pages"
	set word 11 of document 1 to "aanhef test"
	set word 10 of document 1 to "onderwerp test"
	set word 8 of document 1 to "datum test"
	set word 6 of document 1 to "land test"
	set word 5 of document 1 to "woonplaats test"
	set word 4 of document 1 to "postcode test"
	set word 3 of document 1 to "adres test"
	set word 2 of document 1 to "contactpersoon test"
	set word 1 of document 1 to "organisatie test"
end tell

tell application "Pages"
	tell document 1
		set word 11 to "aanhef test"
		set word 10 to "onderwerp test"
		set word 8 to "datum test"
		set word 6 to "land test"
		set word 5 to "woonplaats test"
		set word 4 to "postcode test"
		set word 3 to "adres test"
		set word 2 to "contactpersoon test"
		set word 1 to "organisatie test"
	end tell
end tell

Yvan KOENIG (from FRANCE lundi 29 juin 2009 18:31:50)

Hello,

I have made the last thing work with the normal text replacing.
Now I wan’t to go back to the part of the text box…

I have a text box in my document with text rotated in it, but this text must be replace by another text.

I have the answer that stands at the bottom of this post, but I can’t get it work, the script run’s without problems, but the text won’t be replaced.

Does anyone have a solution?

Model: Pages '09
AppleScript: 2.0.1
Browser: Firefox 3.0.11
Operating System: Mac OS X (10.5)

I don’t guess what you did wrongly.

I opened the script as it is in your message and , happlily, Busch is replaced by Obama in the main text layer, in a text box with the standard orientation in a text box rotated by 30° and in a text box rotated by 90°.

Maybe you typed the first name as “Bush” when I typed “Busch” :wink:

Yvan KOENIG (from FRANCE mardi 30 juin 2009 10:51:57)

Hello,

That was not the problem, I have made the script to work allready in my own script.

After I deleted the current textbox and replaced it, the script worked, and I don’t have made changes to the script itself.

So strange enough, without editing the script the problem is solved.