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

如何通过AppleScript删除文件名末尾固定长度字符用于Automator批量重命名

修改后可直接使用的AppleScript代码

替换你原有Automator动作里的代码即可,该代码会直接删除文件名(不含后缀部分)末尾固定24个字符,同时增加了长度校验避免短文件名报错:

on run {input, parameters}
    
    repeat with thisFile in input
        tell application "Finder"
            set {theName, theExtension} to {name, name extension} of thisFile
            if theExtension is in {missing value, ""} then
                set theExtension to ""
            else
                set theExtension to "." & theExtension
            end if
            -- 提取不含后缀的主文件名
            set baseName to text 1 thru -((count theExtension) + 1) of theName
            -- 校验主文件名长度,避免过短报错
            if (length of baseName) > 24 then
                -- 截取除了最后24位之外的部分,需要调整删除长度修改这里的数值即可
                set trimmedName to text 1 thru -25 of baseName
            else
                -- 长度不足24则保留原文件名,可根据需求调整此处逻辑
                set trimmedName to baseName
            end if
            set name of thisFile to trimmedName & theExtension
        end tell
    end repeat
    
    return input
end run

使用说明

  • 如需调整删除的字符数量,仅需修改两处数值即可:将> 24的24改为你要删除的字符数,将thru -25的25改为「要删除的字符数+1」即可
  • 如果需要严格匹配「以下划线开头的后缀再删除」,避免文件名长度不足时误删,可以将截取逻辑替换为查找最后一个下划线的位置后截断,适配性更强
  • 代码可直接放入Automator的「运行AppleScript」动作中,和其他工作流节点串联使用

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 14:54:05