A script that gets all the components inside the select glyphs?

I want to transform some glyphs, these glyphs have components, is there a script already written that can return all the components within selected glyphs, as if it went through each glyph and “doubl-clicked” each component, to add it to the currently displayed string?

I wouldn’t know about such a script. Do you need help writing it?

I managed to make this one but I don’t know how to make it recursive so it checks all the nested components:

What’s the python way of doing a recursive function until it stops finding components inside?

You could have a variable continue = True, a while loop while continue: bla and a setter if foundComponent: continue = False inside the while loop.