Pages Script to delete extra "space" at end of line (before the CR)

Hi… I do eBooks and a client has provided me a file where instead of just a “CR” (return) at the end of paragraphs there are lots of instances of a “space return.” I need to remove the extra space char which precedes the CR. Of course, doing a Pages “search and replace” removes the paragraph style.

Please… I need a script which will find “space return” and delete the “space” char.

Thanks in advance.

I’m quite sure that there is a faster scheme but this one do the job.


tell application "Pages" to tell document 1
	set lesParagraphes to every paragraph --of body text
	repeat with i from 1 to count of lesParagraphes
		if text -2 of item i of lesParagraphes is space then
			set character -2 of paragraph i to ""
		end if
	end repeat
end tell

Yvan KOENIG (VALLAURIS, France) mercredi 8 février 2012 12:27:55

This interesting variation works even when it encounters paragraphs with less than two characters:

tell application "Pages"
	tell body text of document 1
		set penultimateCharacters to character -2 of paragraphs
		repeat with i from 1 to (count penultimateCharacters)
			if (item i of penultimateCharacters is space) then
				set character -2 of paragraph i to "" -- Or perhaps 'delete character -2 of paragraph i'
			end if
		end repeat
	end tell
end tell

Thanks Nigel and Yvan for the scripts! I appreciate it very, very, much! Just what I needed!

Thanks Nigel

My memory said wrongly that
set penultimateCharacters to character -2 of paragraphs
would fail.


tell application "Pages"
	tell body text of document 1
		set penultimateCharacters to character -2 of paragraphs
		repeat with i from 1 to (count penultimateCharacters)
			if (item i of penultimateCharacters is space) then
				delete character -2 of paragraph i
			end if
		end repeat
	end tell
end tell

behave flawlessly.

Yvan KOENIG (VALLAURIS, France) mercredi 8 février 2012 17:36:51