Capture Drag and Drop Event with AppleScript

79 views Asked by At

since Apple Mail doesn´t use Plugins in Sonoma anymore and "never" was able to DND eml-files to webapps, I want to develope an AppleScript that captures a Drag Event in Apple Mail. This scpt should acts as a service (with Automator) and drop the real EML to web-apps. Now I have a problem to start, because I don´t know how to capture the dragged eml objects in Apple Script. My first step would be to display the dragged objects with filename or id with this code:

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

Does anyone know a way to get this object, thx.

Edit: I have a new approach. When I´m not able to get the DragEvent then I want to move those Emails to a hidden folder in first step! This now works ...

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

The upper script exports selected mail as eml files to a hidden folder. Now I want them to be available in a drop Event...

1

There are 1 answers

0
meDom On

I solved this issue now with the following script, but I am not satisfied with some parts of this script - maybe you can help. This script exports selected mails from AppleMail to a hidden folder and copies them into the clipboard:

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

The part that I am not happy with, is the following:

#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

I´d prefer to use the #-commented part for putting the files into clipboard, but at the moment this does not work for multiple files?

So I used another way by opening the folder, selecting all files with CMD+A and copying to clipboard with CMD+C. This works, but only if I have an active listView in Finder. Is there a way to force listView on tell application Finder? Btw. would it be possible to do this process in background?

Thx for any help