replace every groups of spaces by single one and remove leading and trailing spaces in the text t
on nettoieEspaces(t)
local oTIDs, l, tt, i
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, space}
set l to text items of t
set AppleScript's text item delimiters to ""
set tt to "" & l
set nbSpaces to (count t) - (count (tt))
set AppleScript's text item delimiters to oTIDs
if nbSpaces > 0 then
set l to {}
repeat with i from nbSpaces to 2 by -1
set tt to ""
repeat i times
set tt to tt & space
end repeat
copy tt to end of l
end repeat
set AppleScript's text item delimiters to l
set l to text items of t
set AppleScript's text item delimiters to space
set t to "" & l
if t is space then return t
if t starts with space then set t to text 2 thru -1 of t
if t ends with space then set t to text 1 thru -2 of t
end if # nbSpaces > 0
return t
end nettoieEspaces