How to delay a keymap script without delaying blender's startup?

Some quick context:
I made a script that creates some custom keymaps. If I place it in the startup folder so it runs automatically then this line throws an error saying the property: name doesn’t exist:

kmi = km.keymap_items.new("wm.tool_set_by_id", "Q", "PRESS").properties.name="builtin.select_box"

HOWEVER

If I run my keymap script from the text editor AFTER blender starts then everything works fine, so I assume that specific tool I’m trying to keymap doesn’t exist until AFTER blender starts up.

So I’m wondering if there’s a way I can delay the script or keep trying to register that keymap without halting Blender’s normal startup.

Or am I missing a different way of avoiding this error?

It’s likely the keymap is being defined before the operator (properties) is ready. In this case you can delay using a timer. Here it’s set to 0.1 seconds.

bpy.app.timers.register(
    lambda: setattr(
        km.keymap_items.new("wm.tool_set_by_id", "Q", "PRESS").properties,
        "name",
        "builtin.select_box"),
    first_interval=0.1)
1 Like

That did the trick, it works perfectly. Thank you.