Try this. It should avoid the need to ungroup:
tell application id "com.adobe.illustrator" -- Adobe Illustrator
tell document 1
set theSel to selection
my addVariablesFor:theSel
end tell
end tell
on addVariablesFor:listOfItems
tell application id "com.adobe.illustrator" -- Adobe Illustrator
tell document 1
repeat with anItem in listOfItems
set theClass to class of anItem
if theClass is text frame then
set theVar to make new variable at end with properties {kind:textual}
set anItem's content variable to theVar
else if theClass is group item then
(my addVariablesFor:(page items of anItem))
end if
end repeat
end tell
end tell
end addVariablesFor: