使用 AppleScript 捕获拖放事件

问题描述 投票:0回答:1

由于 Apple Mail 不再在 Sonoma 中使用插件,并且“永远”无法将 eml 文件发送到 Web 应用程序,因此我想开发一个 AppleScript 来捕获 Apple Mail 中的拖动事件。此 scpt 应该充当服务(使用 Automator)并将真正的 EML 放到 Web 应用程序中。现在我有一个问题要开始,因为我不知道如何在Apple Script中捕获拖动的eml对象。我的第一步是使用以下代码显示带有文件名或 ID 的拖动对象:

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

有谁知道如何获得这个对象,谢谢。

编辑: 我有一个新方法。当我无法获取 DragEvent 时,我想第一步将这些电子邮件移动到隐藏文件夹!现在可以了...

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

上面的脚本将选定的邮件作为 eml 文件导出到隐藏文件夹。现在我希望它们可以在掉落事件中使用...

applescript automator
1个回答
0
投票

我现在用以下脚本解决了这个问题,但我对此脚本的某些部分不满意 - 也许你可以提供帮助。此脚本将选定的邮件从 AppleMail 导出到隐藏文件夹并将它们复制到剪贴板中:

tell application "Mail"
    set emls to (get selection)
    set theFolder to "/Users/admin/.test"
    
    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
            on error errorMessage number errorNumber
                display dialog errorMessage & " Error #" & errorNumber & " " & target_file as string buttons {"Oops"}
                try
                    close access target_file
                end try
            end try
            
        on error errorMessage number errorNumber
            display dialog errorMessage & " Error #" & errorNumber as string buttons {"Oops"}
        end try
        
    end repeat
    
    #tell application "System Events"
    #   set fileList to POSIX path of disk items of folder theFolder
    #   set the clipboard to {fileList} as «class furl»
    #end tell
    
    tell application "Finder"
        activate
        do shell script "open " & theFolder & "/"
        tell application "System Events" to keystroke "a" using command down
        tell application "System Events" to keystroke "c" using command down
    end tell
    
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

我不满意的部分如下:

#tell application "System Events"
#   set fileList to POSIX path of disk items of folder theFolder
#   set the clipboard to {fileList} as «class furl»
#end tell
    
tell application "Finder"
    activate
    do shell script "open " & theFolder & "/"
    tell application "System Events" to keystroke "a" using command down
    tell application "System Events" to keystroke "c" using command down
end tell

我更喜欢使用 # 注释部分将文件放入剪贴板,但目前这不适用于多个文件?

所以我使用了另一种方法,打开文件夹,用CMD+A选择所有文件,然后用CMD+C复制到剪贴板。这有效,但前提是我在 Finder 中有一个活动的 listView。有没有办法强制 listView 告诉应用程序 Finder?顺便提一句。可以在后台执行此过程吗?

谢谢您的帮助

© www.soinside.com 2019 - 2024. All rights reserved.