complex pagination: Want BOTH "page x of section" and "page x of document"

I've been working in word and it's not possible to do it there, at least not on a mac. The fields are not the same and it doesn't take formulas on a mac. Which seems to be what's required.

It may not be possible to do it in Pages either, but pages is a little wilder and more set up for thinking different right?

So far I've had no success.

I'm having the same problem in both apps.

If I enter a page number in the header and say insert page number for this section starting at 1, it does it.

If I then go to the footer and say, insert page numbers continuous for the entire document (link to previous) it does that, but, Look! The header has changed to match!

So is there a workaround or is it simply Either / Or?


Mac Studio, macOS 15.3

Posted on Feb 20, 2025 06:17 PM

Reply
9 replies

Feb 23, 2025 04:56 AM in response to nowsthetime

A table of contents is based on paragraph styles and although one could manually assign a custom paragraph style to the text in a Text box, Pages AppleScript support does not support paragraph styles. Even if it did, the Pages TOC only shows the page number where the designated paragraph style occurs.


I have also observed some behavior in the previously posted code that I felt required some additional work, so here it is below:


-- page_cnt_by_section.applescript
-- Revision 2
-- Tested: Pages v14.3, Sequoia 15.3.1
-- Author: VikingOSX, 2025-02-23, Apple Support Communities, no warranty expressed/implied

use scripting additions

tell application "Pages"
	activate
	tell front document
		try
			-- in case one wants to unlock these section text items
			set locked of (every text item whose object text contains "Section") of every page to false
			delete (every text item whose object text contains "Section") of every page
		end try
		
		repeat with sect from 1 to (count of its sections)
			repeat with apage from 1 to (count of pages in section sect)
				set padded_page to text 1 thru -1 of ("    " & apage)
				
				
				tell page apage of section sect to make new text item at its beginning with properties ¬
					{position:{70, 25}, width:132, height:28, object text:"Section " & sect & "  Page " & padded_page}
				tell object text of its text item of page apage of section sect
					-- for darker text, use Helvetica instead
					set font of every word to "Helvetica-Light"
					set color of every word to "black"
					set size of every word to 14
				end tell
				
			end repeat
		end repeat
		try
			-- in case one wants to lock these section text items
			-- set locked of (every text item whose object text contains "Section") of every page to true
		end try
	end tell
end tell
return


Feb 24, 2025 05:54 AM in response to nowsthetime

In further testing of the first code submission, I found that Pages, for some unexplained reason, was not putting the section text boxes rigorously in position {70, 25} and the command to delete those text items at that position was failing to do so, and overwriting those text boxes with another box.


Revision 2 of the code fixed the problem in the first paragraph by addressing Text items that contained the word "Section" and not by position. I also added purely optional code to lock and unlock those section Text boxes with each run of the script.

Feb 22, 2025 11:47 AM in response to VikingOSX

I decided to invest some time in automating this process using AppleScript. It uses a nested repeat loop to iterate through each section and then every page in the section. It writes a Text box at the top of every page of each section in the format: Section n Page n.


One can run this script n-tuple times if text is added to a section because it removes every Text box at the designated top page position in the document before regenerating the new Text boxes. Currently, this sets the object text in each Text box to black, Helvetica 14. One can omit that if desired.


Preparation

  • Turn off the document header in the Documents panel
  • Click on the first page's footer segment and insert Page n of m, where m is the total document pages.
  • The Pages document must be open before running the AppleScript


Limitations

  • There is no means to get the left paragraph margin and convert that to points to dynamically calculate the starting left margin of the Text box. It is set to 70 pixels (based on a 1-in left margin) in the AppleScript.
  • As written, one manually (or through autosave) saves the Pages document after the script has run.


Copy and paste the following code into Apple's Script Editor (shift+option+U, double-click Script Editor). Once pasted, click the hammer icon to compile the code into multi-color text. Then click run.

use scripting additions

tell application "Pages"
	activate
	tell front document
		delete (every text item of it whose position is {70, 25})
		
		repeat with sect from 1 to (count of its sections)
			repeat with apage from 1 to (count of pages in section sect)
				set padded_page to text 1 thru -1 of ("    " & apage)
				tell page apage of section sect to make new text item at beginning of it with properties ¬
					{position:{70, 25}, width:130, height:25, object text:"Section " & sect & "  Page " & padded_page}
				tell object text of its text item of page apage of section sect
					-- for darker text, use Helvetica instead
					set font of every word to "Helvetica-Light"
					set color of every word to "black"
					set size of every word to 14
				end tell
			end repeat
		end repeat
	end tell
end tell
return



Tested: Pages v14.3, Sequoia v15.3.1


Feb 22, 2025 01:50 AM in response to nowsthetime

Hi nowsthetime,

nowsthetime wrote:
pages is a little wilder and more set up for thinking different right?
So is there a workaround or is it simply Either / Or?

Yes, Pages inbuilt features are Either/Or with headers and footers.

Yes, Pages is a little wilder. Here we go with thinking different.

This is a clunky workaround.


Hide the page header (Document Panel > Document Tag > Untick Header) and replace it with a table:



Format the table (single row, 2 columns).

Format Panel > Arrange > Stay on Page.

Type the section title and "Page" into cell A1.

Type "1" into cell B1.


Copy and paste the table and move it to Page 2.



Formula in cell B1 is Table 1::B1+1


Repeat ad nauseum.

Note that the page footer is showing the document page number (as intended in Pages).

If you are interested in this clunky workaround, please reply and I and other users can develop it further.


Regards,

Ian.



Feb 22, 2025 10:30 AM in response to Yellowbox

Oh, Ian! Magnificent. I'm not going to do it.

After days of frustration working deep into the night, I've decided to continue with my Word doc. as follows: I've got a field in the header that shows the name of the specific chapter (that didn't even require section breaks) and in the footer the page number within the entire document. But I'm saving this! It's remarkable.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

complex pagination: Want BOTH "page x of section" and "page x of document"

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