# Dynamic menu creation : how to get the selection?

I think it's a stupid question and the answer may be right in front of my nose. I create a menu populated by the text_blocks of the Text Editor, asking which text I want.

Then.... how do I process the selection ?

class Choose_Text(bpy.types.Menu):
bl_idname = "OBJECT_MT_select_file"
bl_label = "Where is my text ?"

def draw(self, context):
layout = self.layout

for i in bpy.data.texts:
layout.operator("something", text=i.name)


I guess, all the menu items should lead to the same operator and transmit a variable, is that right ?

Thanks for any help !

The menu invokes an operator and this operator would need to receive the filename as a parameter.

I assembled a few lines (sorry for not renaming the classes) I'm sure this can be improved.

AnimMenu - iterates over all loaded text files and add them to a manu in the main header. The files name is handed over by a StringProperty.

AnimOperator - Is invoked when a menu item has been selected it reads the value of the property and shows how the filename is received and how to retrieve files content.

import bpy

text=""

class AnimOperator(bpy.types.Operator):
bl_idname = "object.anim_operator"
bl_label = "Pose Anim Operator"

text_name = bpy.props.StringProperty(name="anim_text_name")
@classmethod
def poll(cls, context):
return context.active_object is not None

def execute(self, context):
print("invoked script %s" % text )
print("invoked script name=%s" % self.text_name )
txt = bpy.data.texts[ self.text_name ].as_string()
print("do something with script")
return {'FINISHED'}

def draw(self, context):
layout = self.layout
for txt in bpy.data.texts:
props = layout.operator("object.anim_operator", text=txt.name)
props.text_name = txt.name

def draw_item(self, context):
layout = self.layout

def register():
bpy.utils.register_class(AnimOperator)