有没有更好的方法用 applescript 递归解析菜单栏?

Is there a better way to recursively parse the menu bar with applescript?

我正在尝试解析菜单栏以查找所有菜单项。我运行宁这一次

tell application "System Events" to tell (process 1 where frontmost is true)
    tell menu bar 1
        get every menu item of every menu of (every menu bar item whose name is not "Apple")
    end tell
end tell

获取第一级菜单项。然后下面得到二级菜单项。

tell application "System Events" to tell (process 1 where frontmost is true)
    tell menu bar 1
        get every menu item of every menu of every menu item of every menu of (every menu bar item whose name is not "Apple")
    end tell
end tell

这需要我 运行 2 个 applescript 并且只能得到 2 级菜单。如果可能的话,最好只运行一个也是递归的脚本。

一个 UI 元素有一个 entire contents 属性,它将为您完成所有工作,例如:

tell application "System Events" to set appList to name of processes where background only is false
set choice to choose from list appList
if choice is not false then
   set appName to choice as text
   tell application appName to activate
   tell application "System Events" to tell process appName to return entire contents of menu bar items of menu bar 1 whose name is not "Apple"
end if

请注意,包括分隔符和禁用的菜单项等项目。