Home > Language Reference > Windows Management Functions > ChooseMenu

ChooseMenu

The ChooseMenu function selects the speficied menu option.

Usage

Used to automate a menu selection in an application. ChooseMenu works only with native Windows menus. If ChooseMenu cannot be used to select a menu option, you can automate the keyboard shortcut using SendKeys function (see the example below). If the application under automation supports the UI Automation framework and if you use the 64 bit automation software, you can automate a menu selection with the ClickUIA function.

Syntax

ret=ChooseMenu(normal|system|context, <menu_item> [, on|off])

Parameters

normal|system|context, keyword, type of the menu.
Normal indicates a menu inside an application,
System indicates a system menu,
Context indicates a context menu (generally displayed by a right click).

<menu_item>, string, text of the menu option to be selected. For example:
"&Edit|&Copy Ctrl+C"
selects the Edit main menu then clicks Copy option. The & character before a letter means that this letter is the keyboard shortcut for the menu option. The main menu option and the submenu option are separated by the | character. Use Recording mode to generate automatically the ChooseMenu syntax.

on|off, optional keyword for checking/unchecking the menu option. This optional parameter is not generated by Recording mode, add it manually if you want to force a tick/untick for the menu option.

Return value

ret, optional numeric return value. If the specified menu item has been successfully selected within 30 seconds (this default value can be changed using #ActionTimeout), the function returns 0. If the function fails, the return value is a nonzero value (see Window Functions Error Codes).

Remarks

ChooseMenu only works with standard Windows menus. If the menu is a custom built menu, you can still automate a menu selection using keyboard shorcuts, see the example below.

If the application under automation supports the UI Automation framework and if you use the 64 bit automation software, you can automate a menu selection with the ClickUIA function.

See also

SendKeys
ClickUIA

Examples

'ChooseMenu different uses in Notepad.
'Launch notepad
shell("notepad.exe")
'Type some text in Notepad window
UseWindow("NOTEPAD.EXE|Edit|Untitled - Notepad|1",1)
 SendKeys("Test ChooseMenu")
'ChooseMenu(Normal,...), select Format/Font menu
UseWindow("NOTEPAD.EXE|Notepad|Untitled - Notepad",1)
 ChooseMenu(Normal,"F&ormat|&Font...")
'Cancel
UseWindow("NOTEPAD.EXE|#32770|Font",1)
 Click(Button,"Cancel")

'ChooseMenu(Context, ), right click within text and select Select All in the context menu
UseWindow("NOTEPAD.EXE|Edit|Untitled - Notepad|1",1)
 ClickMouse(Right,Down,169,12)
 ClickMouse(Right,Up,169,12)
 ChooseMenu(Context,"Select &All")

'Keyboard shortcut use instead of ChooseMenu
Shell("notepad",1)
UseWindow("NOTEPAD.EXE|Notepad|Untitled - Notepad",1)
'With ChooseMenu, the File/Exit selection is: ChooseMenu(Normal,"&File|E&xit")
'Using shortcuts, the same action can be done with the lines below
SendKeys("<Alt f>")
'a small pause to give the system some relief
pause 3 ticks
Sendkeys("x")


'Use of ClickUIA when ChooseMenu does not apply - example on Wordpad Exit option
'Launch Wordpad
Shell("wordpad",3)
'Select Wordpad button and Exit menu option
ClickUIA("[APP='wordpad.exe',CLASS='NetUIHWND'][NAME='Application menu',ROLE='button']")
ClickUIA("[APP='wordpad.exe',CLASS='NetUIHWND'][NAME='Exit',ROLE='menu item']")