Well, instructing Pages to conditionally format paragraph styles via AS really was a pain. AS slowed down as it’s a 102 page document. Iterating through the paragraphs was just too long.
So, I want to SE the find/replace window and go that route.
Can anybody point me in the right direction? I want to use the find/replace window though System Events, but I can’t get into the Advanced pane of the Find window. Also not sure how to set search parameters (including paragraph style) in that window when I get to it.
It seems that this one without GUI Scripting is reasonably fast.
tell application "Pages" to tell document 1
(*
I don't know the English names of the predefined styles so I use the French ones. *)
set style_1 to paragraph style "Sous-section 2"
set style_2 to paragraph style "Sous-section 1"
set les_styles to paragraph style of every paragraph
set i to 1
repeat with un_style in les_styles
if contents of un_style is style_1 then set paragraph style of paragraph i to style_2
set i to i + 1
end repeat
end tell -- Pages
Yvan KOENIG (VALLAURIS, France) vendredi 1 juillet 2011 16:09:30
You are only changing styles. I am starting with a file that has no styles. I’m trying to find a way to AppleScript through the Find/Replace window of Pages v4, by going through the Advanced Panel.
It takes up towards 30 minutes to handle the 100 pages of text, but it works.
Some questions:
why go from the bottom to the top?
Can you explain Text Item Delimiters?
Trying to figure out what you did here. Something tells me you aren’t looking at paragraphs and checking for a contains. I’d love to see how you approached this.
Here’s a script that’s a trifle faster. It works from the end because it’s usually faster to do so. One possible reason for this is that when characters are removed from text in an application document, the end of the text and all its styling has to be moved up to fill the gap. Working from the end means that the text being moved up has itself had text removed from it, so there’s less to move overall. Another possible factor is that to indentify a point in the document text, the application has count from the beginning every time. If it has to count through text which it’s only just edited and may not have tidied up yet internally, it’s either got to sort that out before continuing or work its way through the stored pieces. (This is just happy Saturday-afternoon theorising. Don’t take it as an assertion of fact! But working from the end is certainly faster here.)
property styleKeys : {{"#####", "Section"}, {"####", "Class"}, {"###", "Fee"}} -- NB. Style markers arranged longest to shortest.
on stylise()
tell application "Pages"
tell document 1
set paragraphTexts to paragraphs of body text
repeat with p from (count paragraphTexts) to 1 by -1
set paraText to item p of paragraphTexts
repeat with k from 1 to (count styleKeys)
set {marker, styleName} to item k of styleKeys
if (paraText begins with marker) then
set paragraph p of body text to text ((count marker) + 1) thru -1 of paraText
set paragraph style of paragraph p of body text to paragraph style styleName
exit repeat
end if
end repeat
end repeat
end tell
end tell
end stylise
stylise()
Once I got the chunks, I checked if there were one or two characters # at the beginning of a character to decide which style needed to be applied and of course, I removed the characters #.
(3) Of course, Nigel and me, we scanned the document starting from the end.
For me, it was a basic requirement as I worked using the indexes of characters in the body text.
For Nigel scheme, I guess that it’s a property of the scheme used by the app to retrieve a descriptor.
It resemble to what we may see when we try to select a large block of text in a WP window.
It’s faster to start from the end of the block and move to the top than starting from the beginning and moving to the bottom.
(4) Now it’s time for me to study carefully the Nigel’s script.
Yvan KOENIG (VALLAURIS, France) samedi 2 juillet 2011 17:16:22
Yes, Yvan, the notation that I was using to decipher which style that should be applied, is indeed at the beginning of a paragraph. I guess I should have explained that these are all paragraph styles and the syntax would be at the beginning of the paragraph:
I originally tried to get all paragraphs, but it wouldn’t do that. I like Nigel’s approach to starting with properties as an array of arrays. I entered another Body Bullets style.