At least in the current Pages’ ( v.14.2 ) ‘selection’ is a reference to the current document. Which itself contains, obviously, a ‘selection’ property which is a reference the current document. Recursively, ad infinitum. Well done Pages team!
I would give up on this approach. You could GUI script this but it’s not supportive of this at all.
Make your selection, and this code will open the “Text Color” color-picker window…
tell application "System Events"
tell its application process "Pages"
tell its window "Untitled 20"
tell its splitter group 1
tell its scroll area 2
tell color well 1
perform action "AXPress" -->( "Text Color" window opens )
end tell
end tell
end tell
end tell
end tell
end tell
Unfortunately, the color-picker window is only present when the Pages app is frontmost. Making exploring it tedious with Script Debugger. This will give you the elements and you can extend the return statement to get sub-elements.
tell application "Pages" to activate
tell application "System Events"
tell its application process "Pages"
tell its window "Text Color"
return entire contents
-->{splitter group 1 of window "Text Color" of application process "Pages", slider 1 of splitter group 1 of window "Text Color" of application process "Pages", value indicator 1 of slider 1 of splitter group 1 of window "Text Color" of application process "Pages", slider "Opacity" of splitter group 1 of window "Text Color" of application process "Pages", value indicator 1 of slider "Opacity" of splitter group 1 of window "Text Color" of application process "Pages", static text "Opacity" of splitter group 1 of window "Text Color" of application process "Pages", text field "Opacity" of splitter group 1 of window "Text Color" of application process "Pages", splitter 1 of splitter group 1 of window "Text Color" of application process "Pages", color well 1 of splitter group 1 of window "Text Color" of application process "Pages", checkbox 1 of splitter group 1 of window "Text Color" of application process "Pages", scroll area 1 of splitter group 1 of window "Text Color" of application process "Pages", list "favorite swatches" of scroll area 1 of splitter group 1 of window "Text Color" of application process "Pages", toolbar 1 of window "Text Color" of application process "Pages", button 1 of toolbar 1 of window "Text Color" of application process "Pages", button 2 of toolbar 1 of window "Text Color" of application process "Pages", button 3 of toolbar 1 of window "Text Color" of application process "Pages", button 4 of toolbar 1 of window "Text Color" of application process "Pages", button 5 of toolbar 1 of window "Text Color" of application process "Pages", button 1 of window "Text Color" of application process "Pages", button 2 of window "Text Color" of application process "Pages", group 1 of button 2 of window "Text Color" of application process "Pages", group 1 of group 1 of button 2 of window "Text Color" of application process "Pages", button 3 of window "Text Color" of application process "Pages", static text "Text Color" of window "Text Color" of application process "Pages")
end tell
end tell
end tell
I cannot seem to SET the value of the color well, but
tell application "Pages" to activate
tell application "System Events"
tell its application process "Pages"
tell its window "Text Color"
return the value of every color well of item 1 of UI elements -->"rgb 0 0 0 1"
end tell
end tell
end tell