Apple Script实现重复文件自动添加后缀的需求及脚本优化
AppleScript自动整理Downloads文件夹:优化重复文件重命名功能
我写了个AppleScript用来维护Downloads文件夹整洁,学习工作中用着挺实用——能把文件移到指定文件夹,方便后续在应用里做链接之类的操作。目前脚本已经实现这些功能:
- 目标文件夹无同名文件时,直接把文件移到用户选择的文件夹(会弹出文件夹选择窗口)
- 碰到同名文件时,提供将新文件移至回收站的选项
但现在想新增一个功能:碰到重复文件时,不用手动改名,自动给新文件加后缀——第二个文件加_2,第三个加_3,以此类推。当前手动改文件名的方式不仅运行不正常,效率还很低,下面是我当前的脚本:
(* Version 1.2b Move to Bin prompt added Fixed activate Finder add ability to keep a duplicate *) on run {input, parameters} tell application "Finder" set fileToMove to item 1 of input set fileName to name of fileToMove set fileExtension to name extension of fileToMove ¬ --gets extension incase file needs to be renamed activate set chosenFolder to POSIX file choose folder with prompt "Select a folder to move " & fileName & " " log chosenFolder try move fileToMove to folder chosenFolder -- Make sound play only on success on error -- Display notification with title "File Not Moved" subtitle "File with that name already exists at that location" display alert "Duplicate File Found" message "Would you like to move " & fileToMove & " to the Bin or Keep & Rename?" buttons {"Move Duplicate to Bin", "Keep & Rename", "Cancel"} default button "Keep Both" cancel button "Cancel" set buttonReturned to button returned of result if buttonReturned is "Keep Both" then display dialog "Set New File Name for: " & fileName & " " default answer fileName with icon note set newName to result set the name of fileToMove to newName set the extension of fileToMove to fileExtension move fileToMove to folder chosenFolder do shell script "afplay /System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/Volume Mount.aif'" end if if buttonReturned is "Move Duplicate to Bin" then -- Move the file to the trash move fileToMove to trash end if end try end tell end run
改进后的脚本(实现自动递增重命名)
(* Version 1.3 - Added automatic sequential renaming for duplicate files - Fixed button label mismatch - Corrected file renaming logic - Fixed afplay command syntax - Improved error handling - Added batch file processing support *) on run {input, parameters} tell application "Finder" -- 支持批量处理选中的多个文件 repeat with fileToMove in input set fileName to name of fileToMove set fileExtension to name extension of fileToMove -- 提取文件名(不含扩展名) set baseName to text 1 through ((length of fileName) - (length of fileExtension) - 1) of fileName activate set chosenFolder to POSIX file (choose folder with prompt "Select a folder to move " & fileName & " ") as alias try move fileToMove to chosenFolder -- 移动成功播放提示音 do shell script "afplay /System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/Volume\\ Mount.aif" on error errMsg number errNum -- 仅处理文件已存在的错误(错误码-48) if errNum is -48 then display alert "Duplicate File Found" message "Would you like to move " & fileName & " to the Bin or Keep & Rename?" buttons {"Move Duplicate to Bin", "Keep & Rename", "Cancel"} default button "Keep & Rename" cancel button "Cancel" set buttonReturned to button returned of result if buttonReturned is "Keep & Rename" then -- 自动生成递增后缀的文件名 set newFileName to getSequentialFileName(baseName, fileExtension, chosenFolder) set name of fileToMove to newFileName move fileToMove to chosenFolder do shell script "afplay /System/Library/Components/CoreAudio.component/Contents/SharedSupport/SystemSounds/system/Volume\\ Mount.aif" else if buttonReturned is "Move Duplicate to Bin" then move fileToMove to trash end if else -- 处理其他类型错误 display alert "Error Moving File" message errMsg buttons {"OK"} default button "OK" end if end try end repeat end tell end run -- 辅助函数:生成递增后缀的文件名 on getSequentialFileName(baseName, fileExtension, targetFolder) tell application "Finder" set counter to 2 -- 拼接初始新文件名 set newName to baseName & "_" & counter if fileExtension is not "" then set newName to newName & "." & fileExtension end if -- 循环检查文件名是否已存在,直到找到可用名称 while exists file newName in targetFolder set counter to counter + 1 set newName to baseName & "_" & counter if fileExtension is not "" then set newName to newName & "." & fileExtension end if end while return newName end tell end getSequentialFileName
关键改进点
- 自动递增重命名:新增
getSequentialFileName辅助函数,自动扫描目标文件夹,生成_2、_3格式的后缀,直到找到未被使用的文件名 - 修复原脚本错误:
- 修正按钮文字不匹配问题(原脚本默认按钮标注与选项不一致)
- 修复
afplay命令的路径格式(空格需转义,移除多余单引号) - 优化重命名逻辑,不再需要手动输入文件名
- 批量处理支持:添加循环逻辑,可同时处理Finder中选中的多个文件
- 精准错误判断:仅在触发文件已存在的错误(错误码-48)时执行重复文件处理逻辑,其他错误单独提示
其他优化建议
- 预设常用文件夹:可以把Documents、Pictures等常用文件夹路径存为变量,让用户选择时快速切换,减少弹窗操作
- 添加通知提示:用
display notification替代部分弹窗,文件移动成功后弹出桌面通知,不打断当前工作流程 - 绑定快捷键:将脚本存为Automator服务,绑定自定义快捷键,选中文件后一键触发整理操作
- 日志记录:添加日志写入逻辑,把每次操作记录到本地文件,方便后续排查问题
内容的提问来源于stack exchange,提问作者devoted_dampest
相关产品推荐
相关产品推荐

