Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Getting Page count with AppleScript

Hi Everyone,

It been a long time since I visited so I hope everyone is well.

Does anyone know how to get the page count of a document with Applescript. The best I've done is to divide the total number of words by the average amount of words on a page. This isn't perfect but it give me something close. I'm using the following snippets to get the info, which is being used to popular real time statitics about the progress of book four on my website.

set WordCount to word count
set ParagraphCount to paragraph count
set PageCount to round (WordCount / 184)
set CharacterCount to character count

Paragraph count doesn't seem to come too close either. I think it just adds up the number of carridge returns in the document.

Cheers

Chris
http://www.thomasstrange.com

PowerMac G5 2.5 Dual; PowerBook G4 15, Mac OS X (10.4.10), Apple Cinema Display x 2

Posted on Jul 21, 2007 1:03 AM

Reply
Question marked as Best reply

Posted on Jul 21, 2007 1:38 AM

<pre>tell application "Pages"
tell document 1
page count
end tell
end tell</pre>

For me, the result matches the actual number of pages.

As for paragraph count, I assume you want to skip empty paragraphs.
The obvious "length of paragraph 1" doesn't seem to work, so here's a less than optimal solution:

<pre>tell application "Pages"
tell document 1
tell body text
set paragraphCount to 0
repeat with aParagraph in every paragraph
if length of (contents of aParagraph as text) > 1 then
set paragraphCount to paragraphCount + 1
end if
end repeat
end tell
end tell
end tell
paragraphCount</pre>

Titanium PowerBook Mac OS X (10.4.10)
5 replies
Question marked as Best reply

Jul 21, 2007 1:38 AM in response to Thomas Strange

<pre>tell application "Pages"
tell document 1
page count
end tell
end tell</pre>

For me, the result matches the actual number of pages.

As for paragraph count, I assume you want to skip empty paragraphs.
The obvious "length of paragraph 1" doesn't seem to work, so here's a less than optimal solution:

<pre>tell application "Pages"
tell document 1
tell body text
set paragraphCount to 0
repeat with aParagraph in every paragraph
if length of (contents of aParagraph as text) > 1 then
set paragraphCount to paragraphCount + 1
end if
end repeat
end tell
end tell
end tell
paragraphCount</pre>

Titanium PowerBook Mac OS X (10.4.10)

Jul 21, 2007 2:57 AM in response to Bekins

Thanks Bekins,

That really helped. I was trying to get the page count from the body text not from the document. DOH!

Thanks for the paragraph code, it would be nice if there was a more optimal solution. At the moment your code works fine but when the document grows past a couple of hundred pages then its going to put too much load on things.

I've almost got my script working properly now. I've already had some comments about the real time update on the site. It'll also force me to write a bit more often to try and keep the numbers growing.

Regards

Chris
http://www.thomasstrange.com

Getting Page count with AppleScript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.