Application.GetMenu

table Application.GetMenu (

)

Example 1

-- Output all top level menu items:
local strMenuData = "";

-- Store the contents of the menu in a table
tblMenu = Application.GetMenu();

-- Check that the table exists
if(tblMenu)then
local nNumItems = Table.Count(tblMenu);

-- Step through the table row by row
for index = 1, nNumItems do
local tblItemInfo = tblMenu[index];
if(tblItemInfo)then
-- Add the text to the string
strMenuData = strMenuData..tblItemInfo.Text.."\r\n";
end
end

-- Output the created string to the user
Dialog.Message("Top Level Menu Items", strMenuData);
end

Outputs all the top level menu items to the user in a dialog message.

Example 2

--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
--% Name: OutputMenu (tMenuToOutput, nLevelCount = 0, sMenuString = ""); %
--% Values: tMenuToOutput: REQUIRED - the menu table generated from %
--% Application.GetMenu %
--% nLevelCount: Optional (default to 0), the indent the current item %
--% should have in the output. %
--% sMenuString: Optional (default to ""), a string containing %
--% the menu items. %
--% Returns: A string containing the menu items ready for output. %
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function OutputMenu (tMenuToOutput, nLevelCount, sMenuString)
-- Since we don't want to make nLevelCount required (for the first call),
-- we have to initialize it if it is not passed to the function
if not nLevelCount then
nLevelCount = 0;
end

-- sMenuString is not a required variable (for the first call),
-- therefore we must initialize it if it was not passed to the function.
if not sMenuString then
sMenuString = "";
end

-- Menu table is numerically indexed, step through for each node
for nIndex, tMenuItem in pairs(tMenuToOutput) do
-- Add the current menu item to the string to return, whether it has a sub menu or not
sMenuString = sMenuString .. String.Repeat(" ", nLevelCount*4) .. tMenuItem.Text .. " (ID: " .. tMenuItem.ID .. ")".. "\r\n";
-- Check if a sub menu exists at the current menu's location
if type(tMenuItem.SubMenu) == 'table' and tMenuItem.ID == -1 then
-- A sub menu exists. Recursively call this function to include the sub menu.
-- Increasing the level count by 1 controls the indent that the item will have in the output.
sMenuString = OutputMenu (tMenuItem.SubMenu, nLevelCount + 1, sMenuString);
end
end

-- Return the menu string
return sMenuString;
end





-- Get the menu structure and store in a table
tMenu = Application.GetMenu();

-- Set the title and message for the Dialog.Message box
sTitle = "Current Menu Structure";
sMessage = OutputMenu(tMenu);

-- Display a Dialog.Message box to the user
Dialog.Message(sTitle, sMessage);

Outputs the entire menu structure to the user through a dialog message. This example uses an advanced implementation of a function to recursively step through the menu.

See also: Related Actions