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

求助:AppleScript无法从§分隔文本文件创建列表

AppleScript 按§分隔符生成列表的解决方案

问题分析

你的代码无法生成预期列表的核心原因有两个:

  1. 路径处理冗余且不稳定,POSIX file转字符串容易引入路径解析问题
  2. §是Unicode特殊字符,未指定文件编码会导致分隔符识别失败

修正代码

-- 直接使用原生POSIX路径
set definitionsPath to "/Users/toto/Desktop/Definitions.txt"
-- 转换为alias确保文件引用稳定
set defsFile to POSIX file definitionsPath as alias

-- 指定UTF-8编码读取,避免特殊字符解析错误
set rawList to read defsFile using delimiter "§" as text

-- 过滤空元素和无效空格(处理末尾§或分隔符前后的空格)
set finalList to every item of rawList where it is not "" and it is not " "

针对带引号的文本格式处理

如果你的文本是"text1"§"text2"§"text3"格式,可额外添加代码去除首尾引号:

set cleanedQuotedList to {}
repeat with itemText in finalList
    if itemText starts with "\"" and itemText ends with "\"" then
        set end of cleanedQuotedList to text 2 thru -2 of itemText
    else
        set end of cleanedQuotedList to itemText
    end if
end repeat

关键注意事项

  • 确保文本文件以UTF-8编码保存(用TextEdit存储时,先在“格式”菜单选“制作纯文本”,存储选项中编码选UTF-8)
  • 路径中无需使用转义符\,直接用原生POSIX路径格式/Users/toto/...即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 08:15:36