You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在AppleScript中将嵌套列表合并为单个列表?

合并AppleScript嵌套列表为单列表的最简方法

你从.plist文件获取到的是两层嵌套的列表,要转成扁平的单列表,用AppleScript原生的循环拼接就能搞定,这是最简的实现方式:

方法一:获取数据后单独处理

在现有代码基础上,添加几行代码合并列表:

on getData()
    tell application "System Events"
        set thePropertyListFilePath to FilePad
        tell property list file thePropertyListFilePath
            try
                return value of every property list item
            on error errMsg
                display dialog "ERROR: " & errMsg
            end try
        end tell
    end tell
end getData

set allData to getData()
-- 开始合并嵌套列表
set flattenedList to {}
repeat with subList in allData
    set flattenedList to flattenedList & subList
end repeat
-- flattenedList 就是你要的结果

方法二:直接在获取数据的handler里返回扁平列表

如果不想额外处理,也可以把合并逻辑写进getData函数里,直接返回最终结果:

on getData()
    tell application "System Events"
        set thePropertyListFilePath to FilePad
        tell property list file thePropertyListFilePath
            try
                set nestedData to value of every property list item
                set flattenedList to {}
                repeat with subList in nestedData
                    set flattenedList to flattenedList & subList
                end repeat
                return flattenedList
            on error errMsg
                display dialog "ERROR: " & errMsg
            end try
        end tell
    end tell
end getData

set allData to getData()
-- allData 直接是扁平后的列表

运行后,得到的结果就是你期望的:

{1, 2, 3, 4, "a", "b", 5, 6, 7, "c", "d", 8, 9}

注:如果你的.plist返回的是更深层的嵌套列表(比如三层及以上),可以写一个递归handler来处理,但从你的示例来看,两层嵌套用上面的循环就足够了。

内容的提问来源于stack exchange,提问作者Cardoso Filono

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 07:50:35