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

将指定扩展名文件移动至计算所得子目录的AppleScript技术问询

AppleScript 批量文件分类移动解决方案

作为AppleScript新手,太懂你想靠脚本简化工作流程的心情了!下面这个脚本完美匹配你的三个需求,我会把关键部分拆解开讲,方便你理解和按需修改。


完整可运行脚本

-- 👇 这里是可自定义的配置区,按需修改
set targetExtensions to {"pdf", "docx", "jpg", "png"} -- 要筛选的文件扩展名(统一小写)
set subdirMapping to {¬
    "办公文档": {"pdf", "docx"}, ¬
    "图片素材": {"jpg", "png"} ¬
} -- 预设子目录与对应扩展名的映射关系

-- 获取Finder中选中的文件夹
tell application "Finder"
    set selectedFolders to selection
    if count of selectedFolders is 0 then
        display alert "提示" message "请先在Finder中选中至少一个文件夹再运行脚本!"
        return
    end if
end tell

-- 逐个处理选中的文件夹
repeat with sourceFolder in selectedFolders
    tell application "Finder"
        set sourceFolderName to name of sourceFolder
        -- 在源文件夹同级创建分类根目录
        set targetRoot to make new folder at container of sourceFolder with properties {name:"分类归档_" & sourceFolderName}
        
        -- 先批量创建预设的子目录
        repeat with subdirName in keys of subdirMapping
            make new folder at targetRoot with properties {name:subdirName}
        end repeat
        
        -- 按扩展名筛选文件并移动
        repeat with ext in targetExtensions
            -- 查找当前扩展名的所有文件(默认仅源文件夹根目录,不含子文件夹)
            set matchedFiles to every file of sourceFolder whose name extension is ext
            if count of matchedFiles > 0 then
                -- 找到对应目标子目录
                repeat with subdirName in keys of subdirMapping
                    if ext is in (item subdirName of subdirMapping) then
                        set targetFolder to folder subdirName of targetRoot
                        move matchedFiles to targetFolder
                        exit repeat -- 找到对应目录就停止循环
                    end if
                end repeat
            end if
        end repeat
        
        display alert "处理完成!" message "已将文件分类到「" & name of targetRoot & "」中"
    end tell
end repeat

关键功能拆解

1. 自定义配置区

你可以直接修改最上方的两个变量:

  • targetExtensions:列出所有需要筛选的文件扩展名,记得统一用小写
  • subdirMapping:设置子目录名称和对应的扩展名,比如把「办公文档」对应pdf、docx,「图片素材」对应jpg、png

2. 选中文件夹检测

脚本会先检查你在Finder里有没有选中文件夹,如果没选会弹出提示,避免空运行出错。

3. 目标文件夹生成

脚本会在源文件夹的同级目录创建一个名为「分类归档_原文件夹名」的根目录,然后自动生成你预设的所有子目录,不用手动创建结构。

4. 文件筛选与移动

  • 默认只筛选源文件夹根目录的文件(不含子文件夹里的),如果需要包含子文件夹,可以把every file of sourceFolder改成every file of entire contents of sourceFolder
  • 自动根据你设置的映射关系,把对应扩展名的文件移动到指定子目录

新手友好提示

  • 测试脚本前,请先备份好文件!虽然移动操作是安全的,但新手建议先用测试文件试手
  • 如果遇到大小写混合的扩展名(比如PDF和pdf),可以把name extension is ext改成lowercase name extension is ext,实现大小写不敏感匹配

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:44:02