The number of the current paragraph? How can I get it?

HI,

I’m desperateley looking for a way to get the number of the current paragraph (= where the cursor is in) in Pages. Is there an easy way?

Thanks a lot for any help!

Gary

(P.S.: This is the big hurdle that I need to solve in my struggle to set the paragraph styles in Pages. I have a document with a couple of style keywords and I want to set the style of these paragraphs according to these keywords: macscripter.net/viewtopic.php?id=29093)

I got it!
After a couple of hours of experimenting I found a solution that works for me. I figured I don’t really need the paragraph number: the character number is just as fine.

Yopu can call the following subroutine with (my) getInsertionPoint() in your scripts. «getInsertionPoint» retrieves the character position aka the Insertion Point of the frontmost Pages document. When something is selected, it returns the first character of the selection. When nothing s selected it returns the character position right to the insertion marker.

on getInsertionPoint()
	tell front document of application "Pages"
		set prpts to the properties of selection as list
		set charOffset to item 7 of prpts 
		-- this relies on the fact that the character offset is #7 in the properties list. It breaks, should Pages change this item list in the future!
		return charOffset
	end tell
end getInsertionPoint

Have fun,

Gary

I was going to post on your other post. But you posted here as I was writing this.

It seems it quite simple. And works with the curser placement point or selection.

tell application "Pages"
	set sel to (get selection)
	set para to paragraph of sel
end tell

Here is a way to get the needed info.


tell application "Pages" to tell document 1
	set off7 to character offset of (get properties of the selection)
	repeat with i from 2 to count of paragraphs
		if character offset of paragraph i > off7 then exit repeat
	end repeat
	set numParagraph to i - 1
end tell

Yvan KOENIG (from FRANCE samedi 2 mai 2009 19:14:18)

This one returns the contents of the paragraph, not its index :wink:

Yvan KOENIG (from FRANCE samedi 2 mai 2009 19:17:02)

Which version are you using ?
Here with Pages '09, your script returns false
The index is at offset 42.

What need for this instable trick when there is a standard way to get an info from the properties ?

Yvan KOENIG (from FRANCE samedi 2 mai 2009 19:26:18)

I’m running Pages 08 V.3.0.3 on 10.5.6.

And yes, you’re certainly right. It’s very unstable… I just don’t know AppleScript enough (I almost discovered your solution, as I knew the key name of the properties list was «charcter offset». But I just didn’t know how to reference it in AppleScript… :frowning:

I will play around with your suggestion(s) and then post my findings.

Thanks a lot!

Gary

Here’s the complete (preliminary) script, that works fine on my Pages 08:

tell application "Pages"
	activate
	display dialog (my getInsertionPoint())
end tell

on getInsertionPoint()
	tell front document of application "Pages"
		set prpts to the properties of selection as list
		set charOffset to item 7 of prpts
		# this relies on the fact that the character offset is #7 in the properties list. It breaks, should Pages change this item list in the future!
		return charOffset
	end tell
end getInsertionPoint

Yvan

I was just showing how to get the Paragraph. And thought it would be easy to then find its index. :rolleyes:
(I only started looking at this about 10mins before I posted)

tell application "Pages" to tell document 1
	set off7 to character offset of (get selection)
	set tparas to (character offset of paragraphs)
end tell
repeat with i from 1 to number of items in tparas
	set this_item to item i of tparas
	if this_item > off7 then exit repeat
end repeat
set numParagraph to i - 1

I may be missing something, but why do you need to refer to the properties?

Edit

tell application "Pages" to tell document 1
set t to (character offset of paragraph of (get selection))
	set tg to (character offset of paragraphs)
end tell
repeat with i from 1 to number of items in tg
	set this_item to item i of tg
	if this_item = (t as number) then exit repeat
end repeat
set numParagraph to i

@Yvan,

it’s very slow when you need to run through every single paragraph just to find only one paragraph number. Imagine having thousands of lines. To bad that the paragraph number isn’t part of the properties list.

Well, we have now one (slow) script that returns the paragraph number and a faster one that returns the Insertion Point number. This problem solved.

Having seen how you reference the properties (thank you), I have written the whole script that I had in mind. I will post the code to the other thread (macscripter.net/viewtopic.php?id=29093) as this here was only part of the problem.

By the way: I can get now a property item, but I didn’t manage to set it, say for example the length of the selection (item 20). I can’t imagine how the syntax could be.

Thanks a lot,

Gary

Have you tried either of my scripts in my last post, on My old G4 12’’ they take about 2seconds to complete on a page with 564 paragraphs.
Compared to the 35seconds with the scripts that iterate through each of the paragraphs.

I will try to respond to all of you in a single message.

(1) the second post was refering to properties.
I guessed that the OP had something in mind related to these properties so I feel useful to show how to work with them.

(2) as the properties are grabbed only once, the impact on the total time is quite null.

(3) the question wasn’t about the offset of the selected character but was about the index of the paragraph.
grabbing this one requires a lup.

(4) it appears that grabbing the entire list of paragraphs’s first char is faster than grabbing them one by one so Mark Hunte’s lup is faster than mine.
But it may be enhanced.


tell application "Pages" to tell document 1
	set t to (character offset of paragraph of (get selection)) as integer
	set tg to (character offset of paragraphs)
end tell
repeat with i from 1 to number of items in tg
	if t = item i of tg then exit repeat
end repeat
set numParagraph to i

Selecting a range of characters starting from the selection point is really easy:


tell application "Pages" to tell document 1
	set t to (character offset of (get selection)) as integer
	select (characters t thru (t + 20))
end tell

Yvan KOENIG (from FRANCE dimanche 3 mai 2009 11:16:25)

But can Always be enhanced… :smiley: