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

AppleScript批量处理文件报错排查:展开子文件夹时执行失败

问题描述

日常使用的PSD模板文件名规则:包含ITEM_A/ITEM_B(区分项目)、DRAFT/FINAL(区分状态),其中X是需要替换的项目专属内容。编写的AppleScript脚本功能如下:

  • 获取Finder中选中的文件/文件夹
  • 删除名称包含ITEM_A、FINAL、assets的文件/文件夹
  • 将ITEM_B类文件名中两个下划线之间的X替换为输入的文本

遇到的问题:当子文件夹未展开直接选中时脚本运行正常,但展开子文件夹并选中其内部内容时会报错。需要实现自上而下的处理逻辑:符合删除条件的子文件夹直接删除(包含其内部所有内容),无需处理已选中的该子文件夹内部内容。

问题原因

当前脚本直接遍历所有选中项,若同时选中了待删除的子文件夹和它内部的文件,脚本会先删除子文件夹,后续遍历到内部文件时,这些文件已经随父文件夹被删除,Finder会因找不到对象而抛出错误。此外,脚本没有处理优先级——应该先识别待删除的父级文件夹,跳过对其内部选中项的处理。

修复后的脚本
tell application "Finder"
    set selectedItems to selection
    set replacementText to text returned of (display dialog "Event#_AssetName" default answer "")
    
    -- 先收集所有待删除项的路径,避免遍历过程中对象失效
    set pathsToDelete to {}
    repeat with itemRef in selectedItems
        set itemName to name of itemRef
        if itemName contains "ITEM_A" or itemName contains "FINAL" or itemName contains "assets" then
            set end of pathsToDelete to POSIX path of itemRef
        end if
    end repeat
    
    -- 处理选中项:先过滤待删除文件夹的子项,再执行删除/重命名
    repeat with itemRef in selectedItems
        set itemPath to POSIX path of itemRef
        set isChildOfDeletedFolder to false
        
        -- 检查当前项是否属于待删除的文件夹
        repeat with deletePath in pathsToDelete
            if itemPath starts with deletePath then
                set isChildOfDeletedFolder to true
                exit repeat
            end if
        end repeat
        
        if not isChildOfDeletedFolder then
            set itemName to name of itemRef
            if itemName contains "ITEM_A" or itemName contains "FINAL" or itemName contains "assets" then
                delete itemRef
            else if itemName contains "ITEM_B" then
                set modifiedName to my replaceFirstX(itemName, replacementText)
                set name of itemRef to modifiedName
            end if
        end if
    end repeat
end tell


-- 替换文件名中两个下划线间的第一个X
on replaceFirstX(fileName, newText)
    -- 检查文件名是否有扩展名
    set fileExtensionPos to offset of "." in fileName
    if fileExtensionPos > 0 then
        set fileExtension to text (fileExtensionPos + 1) through end of fileName
        set fileNameWithoutExtension to text 1 through (fileExtensionPos - 1) of fileName
    else
        set fileNameWithoutExtension to fileName
        set fileExtension to ""
    end if
    
    -- 定位第一个下划线
    set firstUnderscorePos to offset of "_" in fileNameWithoutExtension
    -- 定位最后一个下划线(从字符串末尾搜索)
    set lastUnderscorePos to offset of "_" in (reverse of characters of fileNameWithoutExtension as string)
    -- 转换为正向位置
    set lastUnderscorePos to (length of fileNameWithoutExtension) - lastUnderscorePos + 1
    
    -- 确认两个下划线存在且有效
    if firstUnderscorePos ≠ 0 and lastUnderscorePos ≠ 0 then
        set beforeUnderscore to text 1 through firstUnderscorePos of fileNameWithoutExtension
        set middlePart to text (firstUnderscorePos + 1) through (lastUnderscorePos - 1) of fileNameWithoutExtension
        
        -- 替换中间的X
        set newMiddle to my replaceText(middlePart, "X", newText)
        
        set afterLastUnderscore to text (lastUnderscorePos + 1) through end of fileNameWithoutExtension
        -- 拼接新文件名
        return beforeUnderscore & newMiddle & "_" & afterLastUnderscore & "." & fileExtension
    else
        -- 没有找到符合条件的X,返回原文件名
        return fileNameWithoutExtension & "." & fileExtension
    end if
end replaceFirstX

-- 通用文本替换函数
on replaceText(theText, searchText, replaceText)
    set AppleScript's text item delimiters to searchText
    set theTextItems to text items of theText
    set AppleScript's text item delimiters to replaceText
    set theText to theTextItems as string
    set AppleScript's text item delimiters to {""}
    return theText
end replaceText
修复说明
  1. 预收集待删除路径:先遍历所有选中项,把符合删除条件的文件/文件夹路径存起来,避免后续处理中因对象被删除导致的失效问题。
  2. 过滤子项:处理每个选中项时,先检查它是否属于某个待删除的文件夹路径下,如果是则直接跳过——因为父文件夹会被删除,子项会被一并移除,无需单独处理。
  3. 保留原有重命名逻辑:原有的replaceFirstX和replaceText函数保持不变,确保ITEM_B类文件的重命名功能正常运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 15:55:20