osascript:从命令行输出时如何保持嵌套列表结构

osascript: how to keep nested lists structure when output from command-line

我正在尝试编写一个能够提取给定应用程序菜单结构的脚本。这是一个简化的工作示例:

tell application "System Events"
    tell process "Terminal"
        tell menu bar 1
            set listA to name of every menu bar item
            set listB to name of every menu of every menu bar item
            set listC to name of every menu item of every menu of every menu bar item
            set listD to name of every menu of every menu item of every menu of every menu bar item
            set listE to name of every menu item of every menu of every menu item of every menu of every menu bar item
        end tell
    end tell
end tell
return {listA, listB, listC, listD, listE}

当我在脚本编辑器上运行这个脚本时,结果是一组嵌套列表,像这样(实际结果太长,所以我给出了一个示例):

{{{"Option1", "Option2", "Option3"}, {{"subOption1.1", "subOption1.2"}, {"subOption2.1", subOption2.2", "subOption2.3"}, {"subOption3.1"}}}

这样就很容易知道菜单Option1里面有两个项目等等...

但是当我 运行 来自 python 的相同脚本时,使用“osascript -e”,列表结构和大括号消失了,就像这样

{{"Option1", "Option2", "Option3"}, {"subOption1.1", "subOption1.2", "subOption2.1", subOption2.2", "subOption2.3", "subOption3.1"}}

所以无法知道对应的是哪个子列表

有没有办法保留这些大括号或将它们转换成您以后可以管理的不同内容,或者将其写入一种保留嵌套结构的“原始”数据?

提前致谢!

按照@red_menace的建议,我终于搞定了所有的应用程序菜单结构:

    itemList = []

    def findit():

        level = 0

        while True:
            part = ""
            for lev in range(level):
                if lev % 2 == 0:
                    part = " of every menu" + part
                else:
                    part = " of every menu item" + part
            subCmd = "set itemList to name" + part + " of every menu bar item"

            if level % 2 == 0:  # Grabbing items only (menus will have non-empty lists on the next level)

                cmd = """
                        on run arg1
                        set procName to arg1 as string
                        tell application "System Events"
                            tell process procName
                                tell menu bar 1
                                    %s
                                end tell
                            end tell
                        end tell
                        return itemList as list
                        end run
                        """ % subCmd
                # 
                # Didn't find a way to get the "injected code" working when passed as argument
                proc = subprocess.Popen(['osascript', '-s', 's', '-', str(self._parent._app.localizedName())],
                                        stdin=subprocess.PIPE, stdout=subprocess.PIPE, encoding='utf8')
                ret, err = proc.communicate(cmd)
                ret = ret.replace("\n", "").replace('missing value', '"separator"').replace("{", "[").replace("}", "]")
                item = ast.literal_eval(ret)

                if err is None and not self._isListEmpty(item):
                    itemList.append(item)
                else:
                    break
            level += 1

        return itemList != []

    def fillit(subList):
       # Pending: build hierarchy structure (dict format) from list


    if findit():
        print(itemList)
        fillit(itemList)