Extract the text available in a Pages 5.2 table

Hello

The dictionary of Pages 5.2 claims :

table‚n [inh. iWork item] : A table
elements
contains cells, rows, columns, ranges; contained by documents.
properties
name (text) : The item’s name.
cell range (range, r/o) : The range describing every cell in the table.
selection range (range) : The cells currently selected in the table.
row count (integer) : The number of rows in the table.
column count (integer) : The number of columns in the table.
header row count (integer) : The number of header rows in the table.
header column count (integer) : The number of header columns in the table.
footer row count (integer) : The number of footer rows in the table.
responds to
sort.

Alas, when the document has a body text, trying to grab some infos from such object we get nothing.

For instance, count of every table returns always 0

Alas, I have a script which need to extract the text embedded in the unique table of a Pages 5.2 document which may have a body text.
I am pig headed so I finally got a working code but it is awful.
I post it here hoping that somebody will have a better scheme.

set {extractFromTable, leTexte} to my extractTags()

# =====

on extractTags()
	local processName, oldPosition, xArea, yArea, sWidth, sHeight, mt, mi, ms
	local extract_FromTable, tt, le_Texte
	(*
I want to extract the text datas, from Pages documents.
	Alas in Pages 5.2 there is no AppleScript support to do that if the datas
	are stored in a table of a document with a body text.
	A lot of trickery is required. Happily, the table is large enough to fill the entire page 1.
Je dois extraire le texte contenu dans des documents Pages.
	Hélas, Pages 5.2 n'offre pas de support Applescript pour ce faire lorsque les données
	sont dans une table d'un document doté d'un corps de texte.
	Il faut bidouiller. Heureusement, la table remplit la page 1.
*)
	tell application "Pages"
		activate
		set hasBodyText to document body of document 1
	end tell
	
	if hasBodyText then
		tell application "System Events"
			set theProcess to (first process whose frontmost is true)
			tell theProcess
				tell window 1
					(*
Get position of document's window and move it topLeft
Lis la position de la fenêtre du document et la pousse en haut à gauche
*)
					set {oldPosition, position} to {its position, {0, 20}}
					(*
Get measures of the splitter group which is made of : thumbnails area, main page, inspector
Extrait les mesures du « splitter group » composé de : bloc des vignettes, page principale, inspecteur	
*)
					tell first splitter group
						set {xArea, yArea} to position
						set {sWidth, sHeight} to size
					end tell # first splitter
				end tell # window.
				(*
Click at center of the splitter group using Shane STANLEY's ASObjC Runner.app which is already used for other tasks in the full script.
Clique au centre du « splitter group » en utlilisant l'application ASObjC Runner de Shane STANLEY
http://www.macosxautomation.com/applescript/apps/runner.html
*)
				tell application "ASObjC Runner"
					--repeat 1 times
					--delay 0.1
					click button once at {xArea + (sWidth div 2), yArea + (sHeight div 2)}
					delay 0.2
					--end repeat
				end tell
				(*
If the menu item "Insert a row above" of menu item "Table" of menu "Format" is enabled, the selection is a table => labels
	If this item is disabled, it's not a table = personalize a document	
Si l'article « Insère un rang au-dessus » de l'article « Tableau » du menu « Format » est activé, la sélection est une table => étiquettes
	Si cet article est désactivé, ce n'est pas une table => personaliser un document
*)
				set {mt, mi, ms} to {6, 4, 1} # Format > Table > Insert a row above
				tell menu bar 1 to tell menu bar item mt to tell menu 1 to tell menu item mi to tell menu 1
					set extract_FromTable to enabled of menu item ms
				end tell # menu bar.
			end tell # process
		end tell # System Events
		
		if extract_FromTable then #==============================
			
			tell application "System Events" to tell theProcess
				set frontmost to true
				(*
We are in a cell which is not what we need.
	Press the combo command + up arrow to move to top of cell.
	Press up arrow to move to select the cell above without entering it.
Nous sommes dans une cellule ce qui ne convient pas.
	Presse le duo command + flêche vers le haut pour aller en haut de la cellule
	Presse flêche vers le haut pour sélectionner la cellule supérieure sans y entrer.
*)
				# # 123 = left arrow, 124 = right ., 125 = down ., 126 = up .
				key code 126 using {command down} # move to top of cell
				key code 126 # move to cell above
				(*
Select All
Tout Sélectionner
*)
				keystroke "a" using {command down}
				(*
Fill the clipboard with a fake string
Remplir le presse-papiers avec une chaîne improbable
*)
				set tt to "All The Things You Could Be By Now If Sigmund Freud's Wife Was Your Mother, © Charles Mingus"
				tell me to set the clipboard to tt
				(*
Copy
Copier
*)
				keystroke "c" using {command down}
				
			end tell # System Events	
			(*
Wait for the achievement of the Copy task and extract the text from the clipboard
Attend la fin de la copie et extrait le texte du presse-papiers
*)
			tell me
				repeat 20 times
					delay 0.1
					try
						set le_Texte to (the clipboard as text)
						if le_Texte is not tt then exit repeat
					end try
				end repeat
			end tell
			(*
Here, le_Texte contains every tags available in the target table.
Ici, le_Texte contient toutes les balises présentes dans la table à remplir.
*)
		else # ==============================
			(*
Here extract_FromTable is false, extract all available text knowing that a body text is available.
Ici, extract_FromTable est « false », extrait tout le texte présent dans le document sachant qu'un corps de texte est disponible.
*)
			tell application "Pages" to tell document 1
				set le_Texte to body text & object text of every iWork item
			end tell
			(*
Here, le_Texte contains every tags available in the document to customize.
Ici, le_Texte contient toutes les balises présentes dans le document à personaliser.
*)
		end if # extract_FromTable ==============================
		
		(*
Move the window back to its original position
Remet la fenêtre à sa position initiale
*)
		tell application "System Events" to tell theProcess
			set position of window 1 to oldPosition
		end tell
	else
		(*
The document has no body text
Il n'y a pas de couche « corps de texte »
*)
		tell application "Pages" to tell document 1
			if (count tables) < 1 then error "There is nothing usable in this document"
			tell table 1
				set le_Texte to value of every cell whose value is not missing value
				# the main code is able to treat a list as well as a true piece of text 
			end tell
		end tell
		set extract_FromTable to true
	end if
	return {extract_FromTable, le_Texte}
end extractTags

Yvan KOENIG (VALLAURIS, France) mardi 17 juin 2014 18:46:39

Really nobody to clean that ?

Yvan KOENIG (VALLAURIS, France) samedi 21 juin 2014 10:54:57