Menu Bar Items

Hi!
Is there a way to do the following with ASObjC, so that it performs better?

tell application "System Events" to tell (process 1 whose frontmost is true)
	tell menu bar 1
		return entire contents of menu bar items
	end tell
end tell

Hello,

I tried to get the main menu items as an array (using |items|() property), but it didn’t work for me. So I did a little trick - I used the numberOfItems() property. The following script returns a list of lists of names for menu items fairly quickly. Of course, this only works with Cocoa applications.


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set menuBAR1 to ((current application's NSApplication)'s sharedApplication())'s mainMenu()

set menus to {}
repeat with i from 0 to (menuBAR1's numberOfItems()) - 1
	
	set m to (menuBAR1's itemAtIndex:i)
	set end of menus to (m's title()) as text
	
	set tempList to {}
	set sub_Menu to m's submenu()
	repeat with j from 0 to (sub_Menu's numberOfItems()) - 1
		set t to ((sub_Menu's itemAtIndex:j)'s title()) as text
		-- checking the "" is for submenu separator lines " ----- "
		if t is not "" then set end of tempList to t
	end repeat
	
	set end of menus to tempList
end repeat
return menus

NOTE: my script lists only 2 levels of menu items. To get entire contents, someone can make it recursive.

Thank you!