UI scripting within Shortcut stalls out when UI takes too long to load

Hi all,

I’ve created a Shortcut for MacOS that incorporates Applescript to navigate the UI of System Settings. In a few places throughout where I need to wait for a UI element to load, I run a version of:

repeat until UI element 1 exists
    delay 0
end repeat

Most of the time this works fine, and the Shortcut completes running without a problem. Occasionally though, the UI takes longer than usual to load (I’m navigating into the iCloud+ Hide My Email service submenu, which seems to load from a database online and the length of time to do this varies) and the Shortcut aborts and throws an error: System Events got an error: AppleEvent handler failed. Is there an alternative method, without adding unnecessary length to the delay, to achieve a delay that can handle an unknown / indefinite length?

Thanks!