The specific implementation code of the Get Context Menu function?

I want to get the specific code to call the context menu, I want to automate the script, I think this function is amazing.
I would like to automate the script, and I would like to implement the following line of command,

I have consulted countless materials but I have not found the perfect solution, I can only get the list without submenus, which does not solve my problem, if there are submenus can not get and execute
I am open to any kind of language and script implementation, and would appreciate any help.

Translated with DeepL Translate: The world's most accurate translator (free version)

I used dopusrt.exe to basically achieve what I wanted