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

AppleScript调用find命令生成的文件路径含双斜杠的问题及替代方案咨询

AppleScript调用find命令生成的文件路径含双斜杠的问题及替代方案咨询

嗨,我来帮你搞定这个双斜杠的问题~ 其实这个小坑很常见,咱们分两种思路解决:要么修复find命令的输出,要么换用AppleScript原生的方式生成文件列表,都能完美解决Excel对比的问题。

先说说双斜杠的原因

你用choose folder获取的POSIX路径,默认结尾会带一个斜杠(比如/Root_Directory/Directory_to_search/),而find命令在处理这个路径时,会直接把它和找到的文件名拼接,就变成了/Root_Directory/Directory_to_search//filename.txt。虽然系统能识别,但确实会干扰文本对比。


方法1:给find命令“修个路”——去掉路径末尾的斜杠

在把路径传给find之前,先移除最后的斜杠,这样拼接出来的路径就只会有单斜杠了。修改你脚本里的这部分代码:

set source_drive to choose folder with prompt "Please select hard drive."
set source_drive to the POSIX path of source_drive
-- 新增:移除路径末尾的斜杠
if source_drive ends with "/" then
    set source_drive to text 1 thru -2 of source_drive
end if
set drive_name to name of (info for source_drive)
-- 后续代码不变...
set source_drive to the quoted form of source_drive
set theText to do shell script "find " & source_drive & " -type f"

方法2:用sed命令“一键修正”输出

如果你不想修改路径处理逻辑,也可以在find命令后加个sed替换,把所有连续的斜杠换成单个:

set theText to do shell script "find " & source_drive & " -type f | sed 's//\\{2,\\}/\\//g'"

这个命令会把路径里所有//甚至更多连续斜杠都替换成单个/,彻底解决问题。


方法3:换用AppleScript原生递归遍历(完全避开shell命令)

如果你想彻底摆脱shell相关的小问题,用AppleScript原生的方式遍历文件是更稳妥的选择。我给你修改了你的脚本,用递归handler收集所有文件的POSIX路径,生成的结果完全不会有双斜杠:

on writeTextToFile(theText, theFile, overwriteExistingContent)
    try
        set theFile to theFile as string
        set theOpenedFile to open for access file theFile with write permission
        if overwriteExistingContent is true then set eof of theOpenedFile to 0
        write theText to theOpenedFile starting at eof
        close access theOpenedFile
        return true
    on error
        try
            close access file theFile
        end try
        return false
    end try
end writeTextToFile

-- 递归收集所有文件的POSIX路径
on collectFiles(folderObj)
    set fileList to {}
    tell application "Finder"
        set allItems to every item of folderObj
        repeat with itemRef in allItems
            if kind of itemRef is not "Folder" then
                set end of fileList to POSIX path of itemRef
            else
                -- 递归处理子文件夹
                set fileList to fileList & collectFiles(itemRef)
            end if
        end repeat
    end tell
    return fileList
end collectFiles

set source_drive to choose folder with prompt "Please select hard drive."
set drive_name to name of source_drive
set theUser to long user name of (system info)

-- 生成yyyymmdd格式的日期戳
set dateObj to (current date)
set theMonth to text -1 thru -2 of ("0" & (month of dateObj as number))
set theDay to text -1 thru -2 of ("0" & day of dateObj)
set theYear to year of dateObj
set dateStr to theYear & theMonth & theDay as string

-- 用原生方法收集所有文件路径
set filePaths to collectFiles(source_drive)
-- 把列表转成换行分隔的文本
set theText to ""
repeat with filePath in filePaths
    set theText to theText & filePath & linefeed
end repeat
-- 移除最后多余的换行符
if theText is not "" then set theText to text 1 thru -2 of theText

set theFile to (((path to desktop folder) as string) & drive_name & "_on_" & dateStr & ".txt")
writeTextToFile(theText, theFile, true)

这个方法直接调用Finder的API,遍历逻辑更直观,也不会有shell命令带来的路径拼接问题。

总结一下

  • 如果你习惯用find命令,方法1和方法2都能快速解决双斜杠问题;
  • 如果你想要更原生、更稳妥的方案,方法3的递归遍历是更好的选择。

备注:内容来源于stack exchange,提问作者chrisnhamp

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.23 07:29:09