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

Mac Ventura系统下同名异扩展名文件归类的Folder Action脚本需求

自动归类同名异扩展名文件的Folder Action脚本(Mac Ventura)

需求说明

监控指定文件夹,实现:

  • 自动识别文件夹内2个及以上同名不同扩展名的文件
  • 为每组同名文件创建对应子文件夹
  • 将这些文件移入子文件夹;单个文件保留在原目录

原文件示例

Example Print.ai
Example Print.mkv
Example Print.jpg
Example Print.txt
Weird Logo 3454.psd
Weird Logo 3454.ai
Weird Logo 3454.png
Weird Logo 3454.gif
Green Bird.png
Green Bird.jpg
Lizard Jumping.mkv

期望结果

/users/blaine/work/Example Print/
Example Print.ai
Example Print.mkv
Example Print.jpg
Example Print.txt

/users/blaine/work/Weird Logo 3454/
Weird Logo 3454.psd
Weird Logo 3454.ai
Weird Logo 3454.png
Weird Logo 3454.gif

/users/blaine/work/Green Bird/
Green Bird.png
Green Bird.jpg

/users/blaine/work/Lizard Jumping.mkv

现有脚本问题

手动选择文件执行,处理数千至两万级文件效率极低,无法自动触发归类操作。

解决方案脚本

以下AppleScript可直接作为Folder Action使用,自动完成归类:

on adding folder items to this_folder after receiving added_items
    tell application "Finder"
        -- 获取文件夹内所有文件(跳过已存在的子文件夹)
        set all_files to every file of this_folder whose kind is not "Folder"
        -- 按文件名(不含扩展名)分组
        set file_groups to {}
        repeat with a_file in all_files
            set file_name to name of a_file
            set base_name to text 1 thru ((offset of "." in file_name) - 1) of file_name
            -- 检查是否已有该分组
            set group_exists to false
            repeat with a_group in file_groups
                if item 1 of a_group is base_name then
                    set end of a_group to a_file
                    set group_exists to true
                    exit repeat
                end if
            end repeat
            if not group_exists then
                set end of file_groups to {base_name, a_file}
            end if
        end repeat

        -- 处理每个分组
        repeat with a_group in file_groups
            set base_name to item 1 of a_group
            set group_files to items 2 thru end of a_group
            -- 仅处理数量≥2的分组
            if count of group_files ≥ 2 then
                -- 检查子文件夹是否已存在
                if not (exists folder base_name of this_folder) then
                    make new folder at this_folder with properties {name:base_name}
                end if
                -- 移动文件到子文件夹
                move group_files to folder base_name of this_folder
            end if
        end repeat
    end tell
end adding folder items to

使用说明

  1. 打开「自动操作」应用,新建「Folder Action」
  2. 在顶部下拉菜单选择需要监控的目标文件夹(如Work)
  3. 左侧动作库中搜索「运行AppleScript」,拖入右侧编辑区
  4. 替换默认脚本为上述代码,保存即可

注意事项

  • 首次使用前请备份目标文件夹内的文件,避免意外
  • 处理大量文件时,Finder可能需要数分钟完成,请勿中断操作
  • 脚本会自动跳过已归类的文件,重复触发不会重复移动

内容的提问来源于stack exchange,提问作者Blaine Nadeau Johansen

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 02:12:12