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

如何用Automator与AppleScript基于日历事件批量归类房产摄影文件夹

AppleScript实现房产摄影文件夹自动归类方案

核心逻辑

  • 从预先生成的地址-经纪人映射文件中构建地址与经纪人的对应关系
  • 监听指定触发文件夹,对新移入的房产文件夹(按地址命名)进行地址匹配
  • 通过指定分隔符(for 和()解析出经纪人名称
  • 自动将文件夹移动至Dropbox对应的经纪人目录;匹配失败或目录不存在时,弹出交互窗口让用户选择创建新目录或指定现有目录

完整代码实现

-- 配置全局路径(根据你的实际路径修改)
set triggerFolder to POSIX path of (path to desktop folder as text) & "房产摄影触发文件夹/"
set mappingFilePath to POSIX path of (path to documents folder as text) & "地址-经纪人映射.txt"
set dropboxRoot to POSIX path of (path to home folder as text) & "Dropbox/经纪人房产摄影/"

-- 构建地址→经纪人的映射字典
set addressBrokerMap to {}
try
    set mappingContent to read (POSIX file mappingFilePath) as text
    set allLines to paragraphs of mappingContent
    repeat with singleLine in allLines
        if singleLine ≠ "" then
            -- 假设映射文件每行格式为「地址 | for 经纪人姓名 (其他附属信息)」,可根据实际格式调整分隔符
            set AppleScript's text item delimiters to " | "
            set {targetAddress, rawBrokerInfo} to text items of singleLine
            
            -- 解析经纪人姓名:先以"for "分割,再以" ( "截断
            set AppleScript's text item delimiters to "for "
            set splitByFor to text items of rawBrokerInfo
            if count of splitByFor > 1 then
                set postForText to item 2 of splitByFor
                set AppleScript's text item delimiters to " ( "
                set brokerName to item 1 of text items of postForText
                set end of addressBrokerMap to {addr:targetAddress, broker:brokerName}
            end if
        end if
    end repeat
on error errMsg
    display dialog "读取映射文件出错:" & errMsg buttons {"确认"} default button 1 with icon stop
    return
end try

-- Automator Folder Action 触发逻辑:处理移入的文件夹
on run {input, parameters}
    repeat with folderObj in input
        set folderPosixPath to POSIX path of folderObj
        set folderAddrName to name of folderObj
        
        -- 匹配对应的经纪人
        set matchedBroker to missing value
        repeat with entry in addressBrokerMap
            -- 用前缀匹配避免完全一致导致的匹配失败(可根据需求改为精确匹配)
            if folderAddrName starts with addr of entry then
                set matchedBroker to broker of entry
                exit repeat
            end if
        end repeat
        
        if matchedBroker ≠ missing value then
            set targetDir to dropboxRoot & matchedBroker & "/"
            try
                -- 自动创建目录(不存在则创建,存在则无操作)
                do shell script "mkdir -p " & quoted form of targetDir
                -- 移动文件夹
                do shell script "mv " & quoted form of folderPosixPath & " " & quoted form of targetDir
                display dialog "文件夹「" & folderAddrName & "」已移至「" & matchedBroker & "」目录" buttons {"确认"} default button 1 with icon note
            on error moveErr
                display dialog "移动失败:" & moveErr buttons {"确认"} default button 1 with icon stop
            end try
        else
            -- 无匹配结果,弹出用户交互窗口
            set userOption to display dialog "未找到「" & folderAddrName & "」对应的经纪人信息,请选择操作:" buttons {"新建经纪人目录", "指定已有目录", "取消"} default button 1 with icon caution
            set selectedBtn to button returned of userOption
            
            if selectedBtn is "新建经纪人目录" then
                set newBrokerName to text returned of (display dialog "输入新经纪人名称:" default answer "" buttons {"确认", "取消"} default button 1)
                if newBrokerName ≠ "" then
                    set newDir to dropboxRoot & newBrokerName & "/"
                    do shell script "mkdir -p " & quoted form of newDir
                    do shell script "mv " & quoted form of folderPosixPath & " " & quoted form of newDir
                    display dialog "已创建「" & newBrokerName & "」目录并完成移动" buttons {"确认"} default button 1 with icon note
                end if
            else if selectedBtn is "指定已有目录" then
                set chosenFolder to choose folder with prompt "选择目标经纪人目录:"
                set chosenDirPath to POSIX path of chosenFolder
                do shell script "mv " & quoted form of folderPosixPath & " " & quoted form of chosenDirPath
                display dialog "文件夹已移至指定目录" buttons {"确认"} default button 1 with icon note
            end if
        end if
    end repeat
end run

使用说明

  1. 配置路径:修改代码开头的triggerFolder、mappingFilePath、dropboxRoot三个变量为你实际的文件/文件夹路径
  2. 映射文件格式:确保地址-经纪人映射.txt每行格式为房产地址 | for 经纪人姓名 (其他信息),如果Calendar导出的格式不同,调整代码中分割文本的逻辑
  3. Automator设置:
    • 打开Automator,新建「Folder Action」工作流
    • 在顶部选择触发的文件夹(即triggerFolder对应的文件夹)
    • 左侧搜索「Run AppleScript」,拖拽到工作流区域,替换默认代码为上述脚本
    • 保存工作流
  4. 权限设置:在系统设置→隐私与安全性中,给Automator添加访问触发文件夹、Documents文件夹和Dropbox的权限

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 02:54:36