Pages: Is there a way to set styles based on keywords in the text?

Hi,

I’m new here (and to AppleScript; though I have some experience in Pascel, C, Bash and UserTalk (anyone remember this? :-)) and I wonder if I can apply styles to specific paragraphs in a Pages dcument that begin with a special marker string.

For example: Many paragraphs start with “%!hd1”. This string needs to be removed and the “Header 1” style, which is already defined in Pages should be applied to every such paragraph.

I thought that there would be something in the lines of:

tell front document of application "Pages"
	set paragraph style of every paragraph who begins with "%!hd1" to "Header 1"
end tell

I have tried for some hours now but no luck. Does anyone have any idea how this could be done?

Background: I’m creating the document from a database, writing it first into a text file and want to make it look pretty in Pages.

Thanks so much for any help!

Gary

OK,

its really much more complicated than I thought :frowning: But I have found at least part of the solution, a very ugly script using System Events. Now, I have successfully selected the first occurence of “%!” in my text and want to change its paragraph style to “Header 1”.

How can I do this?? How can I apply a style to the current selection? Or how can I find the number of the current paragraph?
Referencing the paragraph by number is no problem:

set paragraph style of paragraph n to "Body"

)

“Pages got an error: Can’t set paragraph style of selection of document 1 to "Header 1".”

tell application "Pages"
	activate
	tell front document of application "Pages"
		select word 1 # we want to start from the beginning
		tell application "System Events"
			keystroke "f" using command down
			keystroke "%!" using shift down
			keystroke return
			delay 0.3
			keystroke "w" using command down
		end tell
		set paragraph style of selection to "Header 1"
	end tell
end tell

Please help!

Thanks a lot,

Gary

Thanks to Yvan Koenig in macscripter.net/viewtopic.php?id=29125 I could write a solution. It is a little bit ugly because I needed to workaround my missing understanding of AppleScript. But here, it goes:

A text is given that has keywords of different styles at the beginning of some lines: “%!bnl” or “%!hd1”. These indicate the styles that the paragraphs should be set to.

tell application "Pages" to tell document 1
	repeat with i from 2 to count of paragraphs
		if (length of paragraph i ≥ 5) then # does i begin with our string marker?
			set s to (character 1 of paragraph i)
			set s to s & (character 2 of paragraph i)
			if s = "%!" then
				delete character 1 of paragraph i
				delete character 1 of paragraph i
				set s to (character 1 of paragraph i)
				set s to s & (character 2 of paragraph i)
				set s to s & (character 3 of paragraph i)
				delete character 1 of paragraph i
				delete character 1 of paragraph i
				delete character 1 of paragraph i
				if s = "bnl" then
					set paragraph style of paragraph i to "Body New Line"
				else if s = "hd1" then
					set paragraph style of paragraph i to "Header 1"
				end if #s=bnl, hd1, .
			end if # %! style marker found			
		end if #length ≥ 5
	end repeat
end tell

It’s really ugly because I couldn’t figure out a way to select or to delete more than one characters… :frowning:

This script is also a little slow, to bad we can’t find the searchstrings directly. The repeat wouldn’t be necessary and would give some speed.

Thanks a lot for your help!

Gary

Model: MacBook Pro 1.8GHz, Pages 08 3.0.3
Browser: Safari 525.27.1
Operating System: Mac OS X (10.5)

Hi Gary does this do what you want with better speed?

tell application "Pages" to tell document 1
	--set paras to (character offset of paragraphs where it contains "%!")
	--set paras to (character offset of paragraphs where it's first text contains "%!")
	(* get only paragraphs that contain the style string marker*)
	set paras to (character offset of paragraphs where character 1 is "%" and character 2 is "!" or character 2 is "%" and character 3 is "!") -- The commented out lines above work but chose this one in case there is a return character being included as the first character.--
	
	(*Get all character offset of paragraphs in one go as alist *)
	set AllParas to (character offset of paragraphs) (**)
end tell
(**)
set biglist to {}
repeat with i from 1 to number of items in paras
	set theInPoint to item i of paras (*character offset of a paragraph found to have the style string marker*)
	repeat with i from 1 to number of items in AllParas
		set this_item to item i of AllParas (*character offset of paragraph in page *)
		if this_item = (theInPoint as number) then
			copy i to end of biglist (*For List of Paragrapths that should be changed*)
			tell application "Pages" to tell document 1
				(* get character offset's of the style string marker chararcters in the paragraph
			to use for matching string marker to what style to use and for the delete later*)
				set tCHARS to (character offset of first character of paragraph i where it is "%")
				(* the match *)
				set StyleChars to characters tCHARS thru (tCHARS + 4) as string
				
				if StyleChars contains "bnl" then
					set paragraph style of paragraph i to "Body New Line"
				else if StyleChars contains "hd1" then
					set paragraph style of paragraph i to "Header 1"
				end if
				(* the delete *)
				delete (characters tCHARS thru (tCHARS + 4))
			end tell
			exit repeat
		end if
	end repeat
end repeat
(*Paragrapths that should have been changed*)
return biglist


Why not this short simple code ?


tell application "Pages" to tell document 1
	set dn to name
	repeat with i from 1 to count of paragraphs
		set parI to paragraph i
		if parI starts with "%!bnl" then
			my setParagraphStyle(dn, i, "Body New Line")
		else if parI starts with "%!hd1" then
			my setParagraphStyle(dn, i, "Header 1")
		end if --s=bnl, hd1, .	
	end repeat
end tell

on setParagraphStyle(d, i, n)
	tell application "Pages" to tell document d
		tell paragraph i
			repeat 5 times
				delete character 1
			end repeat
		end tell
		set paragraph style of paragraph i to n
	end tell
end setParagraphStyle

Here, under 10.4.11,

set paras to (character offset of paragraphs where character 1 is “%” and character 2 is “!” or character 2 is “%” and character 3 is “!”)

is failing with the message:
“Erreur dans Pages : NSReceiverEvaluationScriptError: 3”

Same behavior with:
set paras to (character offset of paragraphs where it contains “%!”)
and
set paras to (character offset of paragraphs where it’s first text contains “%!”)

I really don’t understand the need for the double test with “%!” at index 1 or at index 2 in the paragraph.
The return characters are the paragraphs separators they aren’t embedded in the paragraphs.

I tried a lot of syntaxes to extract the list of paragraphs starting with “%!” in a single instruction using whose or where.

All of them failed with the described error message.

Yvan KOENIG (from FRANCE dimanche 3 mai 2009 10:55:23)

Hi Yvan
Actually on the pages v9 doc that I was testing on it kept including a return. (Why I do not know?) So therefore missing a paragraph. Hence why I was making sure with the 1,2,3,4 check.

Also I suspect that you script may take a Long time iterating through a large doc?.

I can not test on 10.4.x at the mo… but wil do later. all syntax works ok here.

Mark, Yvan,

thanks. Both scripts run just fine on my system (10.5.6 on a MacBook Pro). Both are significantly faster than the first version from Yvan. Mark’s script took 48 seconds and Yvan’s latest version 43 seconds on a MacBook Pro with 2.83GHz for 350 paragraphs (amounts to ≈ 15 pages).

Thanks a lot!

Gary

I made my tests with Pages '09.
I often work on Pages documents and I never encountered a return included in a paragraph.

It seems that results passed by gczychi give an other advice.

And of course, between a version running only under 10.5./6 and one running also under 10.4 I don’t hesitate a quarter of second :wink:

Yvan KOENIG (from FRANCE dimanche 3 mai 2009 21:51:31)