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

如何让AppleScript仅处理新增文件而忽略重命名文件?

问题:如何让Folder Action脚本仅响应新增文件而非重命名文件?

我编写了一个Folder Action的AppleScript,可自动打开添加到特定文件夹中的新文件,运行效果良好。但问题是,当我重命名新文件时,Automator会再次运行并重新打开该文档。扫描器生成的文件命名格式为:

  • 示例:2022-12-21-191532.pdf
    1. 年-月-日 + “-”
    2. 随机数字(时间戳)
    3. .pdf 后缀

是否有办法让我的脚本仅针对新增文件运行,而忽略重命名的文件?

原脚本代码如下:

on adding folder items to this_folder after receiving added_items
    delay 30
    
    try
        tell application "Finder"
            --get the name of the folder
            set the folder_name to the name of this_folder
            --go to the desktop 
            activate
            open this_folder
            open added_items
            
            reveal the added_items
        end tell
    end try
end adding folder items to

解决方案

方案一:通过文件创建时间过滤

重命名操作不会修改文件的创建时间,而新增文件的创建时间与脚本触发时间非常接近。利用这一点,我们可以在脚本中添加时间判断,只处理创建时间在最近窗口内的文件。

修改后的脚本:

on adding folder items to this_folder after receiving added_items
    delay 30
    set currentTime to current date
    
    try
        tell application "Finder"
            set folder_name to name of this_folder
            activate
            open this_folder
            
            set validItems to {}
            repeat with itemRef in added_items
                -- 获取文件创建时间,计算与当前时间的差值(秒)
                set creationTime to creation date of itemRef
                set timeDiff to currentTime - creationTime
                -- 留5秒缓冲,确保新增文件能被捕获
                if timeDiff ≤ 35 then
                    copy itemRef to end of validItems
                end if
            end repeat
            
            if validItems is not {} then
                open validItems
                reveal validItems
            end if
        end tell
    end try
end adding folder items to

方案二:记录已处理文件的唯一UUID

每个文件都有一个唯一且不会随重命名改变的UUID,我们可以维护一个列表记录已处理过的文件UUID,每次触发时检查文件是否已处理过。

修改后的脚本:

on adding folder items to this_folder after receiving added_items
    delay 30
    -- 存储已处理文件UUID的plist路径,可自行调整位置
    set plistPath to POSIX path of (path to library folder from user domain) & "Preferences/com.your.processed.files.plist"
    
    try
        -- 读取已处理UUID列表,文件不存在则初始化空列表
        set processedUUIDs to {}
        if file plistPath exists then
            set processedUUIDs to read plist file plistPath
        end if
        
        tell application "Finder"
            set folder_name to name of this_folder
            activate
            open this_folder
            
            set validItems to {}
            repeat with itemRef in added_items
                set fileUUID to UUID of itemRef
                if fileUUID is not in processedUUIDs then
                    copy itemRef to end of validItems
                    copy fileUUID to end of processedUUIDs
                end if
            end repeat
            
            if validItems is not {} then
                open validItems
                reveal validItems
                -- 更新plist文件,记录新处理的UUID
                write processedUUIDs to plist file plistPath
            end if
        end tell
    end try
end adding folder items to

注意:方案二的plist文件会随着时间积累越来越大,建议定期清理该文件,或者在脚本中添加逻辑自动移除过旧的记录。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 06:05:55