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

如何用AppleScript捕获Apple Mail拖拽事件并导出EML至Web应用

问题背景与需求

在Sonoma系统下,Apple Mail不再支持插件,而且原生功能无法直接把EML文件拖拽到Web应用中。我想开发一个AppleScript作为Automator服务,捕获Apple Mail里的拖拽事件,把真实的EML文件拖拽到Web应用。最初尝试的代码没法获取拖拽对象,于是转而写了一个把选中邮件导出到隐藏文件夹的脚本,但现在需要让这些导出的文件能用于拖拽事件。

初始尝试代码

tell application "Mail"
    set items to open draggedItems
    set theDialogText to items
    display dialog theDialogText
end tell

已实现的导出邮件脚本

tell application "Mail"
    set emls to (get selection)
    set theFolder to "/Users/%username%/.tmpDragFiles"
    
    set DatumsTrenner to "-"
    set maxSubjectLen to 64
    set maxSenderNameLength to 15
    
    repeat with eml in emls
        
        set emlDate to date received of eml
        
        set theYear to year of emlDate
        set theMonth to month of emlDate as integer
        set theMonth to my addZero(theMonth as string)
        set theDay to my addZero(day of emlDate as rich text)
        set hrs to my addZero(hours of emlDate as rich text)
        set mins to my addZero(minutes of emlDate as rich text)
        set secs to my addZero(seconds of emlDate as rich text)
        
        set emlDate to theYear & DatumsTrenner & theMonth & DatumsTrenner & theDay & "_" & hrs & "." & mins & "." & secs
        
        set comparison_string to "||*:/[]<>?\""
        set replacement_string to "--x_-()()__'"
        
        set emlSubject to ""
        
        repeat with c in ((extract name from (sender of eml)) as string)
            set x to the offset of c in comparison_string
            if x is not 0 then
                set emlSubject to (emlSubject & character x of replacement_string) as string
            else
                set emlSubject to (emlSubject & c) as string
            end if
        end repeat
        
        if the length of emlSubject > maxSenderNameLength then
            set emlSubject to characters 1 thru maxSenderNameLength of emlSubject
        end if
        set emlSubject to my changecaseoftext(emlSubject, "upper") & "-"
        
        repeat with c in (subject of eml as string)
            set x to the offset of c in comparison_string
            if x is not 0 then
                set emlSubject to (emlSubject & character x of replacement_string) as string
            else
                set emlSubject to (emlSubject & c) as string
            end if
        end repeat
        set emlSubject to my replaceText("Re- ", "", emlSubject)
        set emlSubject to my replaceText("Re-", "", emlSubject)
        
        if the length of emlSubject > maxSubjectLen then
            set emlSubject to characters 1 thru maxSubjectLen of emlSubject
        end if
        
        set newFileName to (emlDate & "_" & emlSubject & ".eml") as string
        
        try
            set target_file to theFolder & "/" & newFileName
            set this_data to (get source of eml)
            
            try
                set open_target_file to open for access target_file with write permission
                set eof of open_target_file to 0
                write this_data to open_target_file starting at eof
                close access open_target_file
                return true
            on error errorMessage number errorNumber
                display dialog errorMessage & " Error #" & errorNumber & " " & target_file as string buttons {"Oops"}
                try
                    close access target_file
                end try
                return false
            end try
            
        on error errorMessage number errorNumber
            display dialog errorMessage & " Error #" & errorNumber as string buttons {"Oops"}
        end try
        
    end repeat
end tell

on addZero(v)
    if length of v < 2 then
        return "0" & v
    end if
    return v
end addZero

on replaceText(find, replace, subject)
    set prevTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to find
    set subject to text items of subject
    
    set text item delimiters of AppleScript to replace
    set subject to "" & subject
    set text item delimiters of AppleScript to prevTIDs
    
    return subject
end replaceText

on changecaseoftext(QuellText, theCaseToSwitchTo)
    if theCaseToSwitchTo contains "lower" then
        set theComparisonCharacters to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        set theSourceCharacters to "abcdefghijklmnopqrstuvwxyz"
    else if theCaseToSwitchTo contains "upper" then
        set theComparisonCharacters to "abcdefghijklmnopqrstuvwxyz"
        set theSourceCharacters to "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    else
        return QuellText
    end if
    set ZielText to ""
    repeat with aCharacter in QuellText
        set theOffset to offset of aCharacter in theComparisonCharacters
        if theOffset is not 0 then
            set ZielText to (ZielText & character theOffset of theSourceCharacters) as string
        else
            set ZielText to (ZielText & aCharacter) as string
        end if
    end repeat
    return ZielText
end changecaseoftext

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 11:43:30