Delete paragraph in Pages

I’m trying to write an AppleScript to delete a certain paragraph in a Pages document, which has the font color “red”.

My working script is:

tell application "Pages"
    tell the front document
        tell the body text
            delete (every paragraph where its size is 11)
        end tell
    end tell
end tell

But when I change the section based on the size of the font to the color of the font, it doesn’t work anymore.

My unworking script is:

tell application "Pages"
    tell the front document
        tell the body text
            delete (every paragraph where its color is "red")
        end tell
    end tell
end tell

Since, depending on the situation (now, I have 12 situations, but the script has to be future proof and still be usable when there are more situations ), a certain paragraph should deleted in the text. So working with for sizes would be very unclear, since it means that now, the base text would have 13 font sizes …

Can someone help me?

Hi Amelie.

Unfortunately, you can’t just give the name of a colour as text. A ‘color’ is a three-integer list representing the amounts of red, green, and blue which make up the colour. For instance, when I colour a paragraph in Pages using the “Maraschino” pencil in the colour picker, the value of the color is {64633, 466, 1797}.

Another unfortunate thing is that Pages’s whose/where filters don’t seem to work with colors. It’s necessary to get a list of the colors of all the paragraphs in the text and delete the paragraphs corresponding to the unwanted results. The paragraphs which come after the deleted ones in the document will be renumbered each time, so it’s best to work backwards through the list.

set colourToDelete to {64633, 466, 1797} -- "Maraschino"

tell application "Pages"
	activate
	tell the front document
		tell the body text
			-- Get a list of the colors of the text's paragraphs.
			set paragraphColours to color of paragraphs
			-- Work through the list in reversed order.
			repeat with i from (count paragraphColours) to 1 by -1
				if (item i of paragraphColours is colourToDelete) then
					delete paragraph i
				end if
			end repeat
		end tell
	end tell
end tell

@Nigel Garvey: Thank you very much for your answer. It works!

But, now I have an other problem: my AppleScript works with a 16-bit definition of a colour and my pages with a 8-bit definition.

I found some where on the internet, that I should multiply RGB(0,0,255) with 257 to get the 16-bit. So, I though that it would be (0,0,655635). But, I experimentally noticed that it must be {7, 115, 65418}. (Or, to be correct, that is the conversion my computer mad of it, may be it is a setting of my computer)

Hi Amelie.

It does seem to be a problem. No matter how the colors of Pages text are set — even by script! — it’s impossible to predict exactly what the ‘color’ lists will contain when a script asks for them.

The 8-bit settings in the “sliders” colour picker seem to be interpreted according to a profile which can be selected after clicking the cogwheel next to the pop-up window in the sliders picker. But that’s all the sense I’m able to make of the situation. :confused:

Once set, the colors don’t seem to change over time, so one approach might be to test your documents with a script to see which numbers correspond to which of your colours and use those numbers in subsequent scripts. But this is obviously far from foolproof!

If you’re using unsubtle colours like red, you could perhaps test each component individually and comparatively:

tell application "Pages"
	activate
	tell the front document
		tell the body text
			-- Get a list of the colors of the text's paragraphs.
			set paragraphColours to color of paragraphs
			-- Work through the list in reversed order.
			repeat with i from (count paragraphColours) to 1 by -1
				set thisColour to item i of paragraphColours
				-- Test if this colour could be considered to be red:
				-- The red component must be above some highish figure and both the green and blue below lowish ones.
				if ((item 1 of thisColour > 62000) and (item 2 of thisColour < 3000) and (item 3 of thisColour < 3000)) then
					delete paragraph i
				end if
			end repeat
		end tell
	end tell
end tell