AppleScript with a Pages Table to *Resize rows to fit cell contents*

Hello all, I'm using AppleScript to collect about 500 one-page account statements that are generated individually by a Numbers spreadsheet into a Pages document.  It's working great but Pages changes the row height when it pastes. When I manually uncheck the "Resize rows to fit cell contents" box on the Pages - Format - Table pane it fits perfectly, but I can't get it to work via AppleScript. 




Here's the code after copying the table from Numbers:


 


tell application "Pages" to activate


delay 0.5 -- vary as needed to give UI time to catch up


tell application "System Events" to keystroke "v" using {command down}


tell application "Pages"


set t to (front document's last table)


set t's position to {81, 136}


--set t's rowheight to 17 --not available when resizerows is checked


set t's resizerows to false -- unchecked/off


end tell




The last set statement gives error "Pages got an error: Can’t make resizerows of table 1 of document id \"..." into type specifier."




Is this a syntax error, or is this just not possible to do? Thanks for any help!

iMac (M1, 2021)

Posted on Jul 4, 2023 5:03 PM

Reply
Question marked as Top-ranking reply

Posted on Jul 5, 2023 5:03 AM

Here is what I suggest, since there is no Pages AppleScript dictionary support to deselect "Resize rows to fit cell contents." After the paste, set the delay long enough to give you time to manually deselect that checkbox, then the rows will be set to their (real number) height.


Also, note how I use the code tool </> from this editors toolbar to paste code into the discussion and after the code, I press return and click the </> tool again.


use scripting additions

tell application "Pages"
	activate
	tell front document
		tell application "System Events" to keystroke "v" using {command down}
		delay 3
		-- manually click the Resize rows to fit cell contents checkbox to disable it
		set t to its last table
		tell t
			set its position to {81, 136}
			tell its rows to set height to 17.0
		end tell
	end tell
end tell


Does that work for you?


Similar questions

4 replies
Question marked as Top-ranking reply

Jul 5, 2023 5:03 AM in response to johnfromtamarac

Here is what I suggest, since there is no Pages AppleScript dictionary support to deselect "Resize rows to fit cell contents." After the paste, set the delay long enough to give you time to manually deselect that checkbox, then the rows will be set to their (real number) height.


Also, note how I use the code tool </> from this editors toolbar to paste code into the discussion and after the code, I press return and click the </> tool again.


use scripting additions

tell application "Pages"
	activate
	tell front document
		tell application "System Events" to keystroke "v" using {command down}
		delay 3
		-- manually click the Resize rows to fit cell contents checkbox to disable it
		set t to its last table
		tell t
			set its position to {81, 136}
			tell its rows to set height to 17.0
		end tell
	end tell
end tell


Does that work for you?


Jul 5, 2023 5:32 AM in response to VikingOSX

Or, more appropriately, make the row height changes and then deselect the current table:


use scripting additions

tell application "Pages"
	activate
	tell front document
		tell application "System Events" to keystroke "v" using {command down}
		delay 5
		-- manually click the Resize rows to fit cell contents checkbox to disable it
		set t to its last table
		tell t
			set its position to {81, 136}
			tell its rows to set height to 17.0
		end tell
	end tell
end tell
-- and now deselect the entire table
tell application "System Events"
	tell application process "Pages"
		set frontmost to true
		keystroke "a" using {shift down, command down}
	end tell
end tell


Jul 7, 2023 6:19 AM in response to johnfromtamarac

There is no Pages AppleScript dictionary entry named resizerows, nor as an undeclared variable of any type, you cannot tell the table to set anything to it. Further, the AppleScript dictionary does not give you access to toggling the [ ] Resize rows to fit cell contents selection checkbox as it is buried deep in the GUI design.


If there had been a practical way to toggle that checkbox, it would have been in my code response to your post. I chose not to spend hours, or forever, attempting to plumb the GUI object hierarchy for the code to perform that toggle event. Even if I had got lucky, there would be no assurance that the next point release of Pages would not break any GUI scripting as Apple has been known to reorganize their internal application code structure resulting in broken scripts.

Jul 6, 2023 6:10 PM in response to VikingOSX

Thanks VikingOSX, it works by manually unchecking as you suggested- I was just hoping to script that somehow given there are 500 statements. And apparently it just ignores "set height to" when the box is checked.


Since it's not bombing on the "resizerows" reference in

set t's resizerows to 

but rather on the "to" value that follows, I'd like to play around with that a little before I give up. Anyway, many thanks and all the best-

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.

AppleScript with a Pages Table to *Resize rows to fit cell contents*

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