Read the text in a menu item

Is it possible to read the text in a menu item by using AppleScript and save that text as a variable?

For context:
I want to create a single shortcut for Logic Pro's export option, instead of clicking File and then moving the mouse to the Export option and then picking one option (especially because some of those options I rarely use and I just want to have in front of me the options I tend to use the most).

Some of those export options change their text based on how many audio/MIDI regions (or tracks) are selected as you can see below

So I was wondering if KM can read the text available at that particular time for both of those menu options, then save them as variables so when the Prompt... prompts... I can see exactly how many regions/tracks are selected

I don't have Logic Pro, but found this an intriguing question, though it's well beyond my AppleScript knowledge. So I thought I'd see how ChatGPT faired. I used the Safari File > Edit menu as a test, wanting to capture its sub-menu into an AppleScript variable:

image

After some back and forth, here's what it came up with:

tell application "Safari"
	activate
	tell application "System Events"
		tell process "Safari"
			set editMenu to menu "Edit" of menu bar 1
			set findMenuItem to menu item "Find" of editMenu
			set findSubmenu to menu 1 of menu item "Find" of editMenu
			
			set menuItemsList to {}
			repeat with menuItem in menu items of findSubmenu
				set end of menuItemsList to name of menuItem
			end repeat
			
			return menuItemsList
		end tell
	end tell
end tell

When run, it returns these values:

{"DuckDuckGo Search…", missing value, "Find…", "Find Next", "Find Previous", missing value, "Hide Find Banner", missing value, "Use Selection for Find", "Jump to Selection"}

You could modify the above to match the Logic Pro menu structure, then return the results to a Keyboard Maestro variable. You could then process the list to find the menu items you're interested in.

Those whose AppleScript skills dwarf mine (most everyone :)) can probably come up with a simpler solution, but this seems to work as a starting point.

-rob.

In case you missed it: "Export Regions as Audio Files" and "Export Tracks as Audio Files", along with other export options, can be triggered by Logic Pro key commands.

Not sure if this is relevant to what you are doing: