Skip to content

Instantly share code, notes, and snippets.

@jmdelafe
Last active April 14, 2022 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmdelafe/47adb02018297a13891bd6f52b753937 to your computer and use it in GitHub Desktop.
Save jmdelafe/47adb02018297a13891bd6f52b753937 to your computer and use it in GitHub Desktop.
Script to sort mails in my Exchange account (imported in Apple Mail)
if application "Mail" is running then
tell application "Mail"
set casesFolder to mailbox "Cases" of mailbox "Inbox" of account "Exchange"
set caseMessages to (every message of (casesFolder))
set createFolders to {}
repeat with theMessage in caseMessages
set msgSubject to subject of theMessage as Unicode text
if (msgSubject contains "Support Survey") then
set theContent to content of theMessage
set theLines to paragraphs of theContent
repeat with aLine in theLines
set isMatch to do shell script "echo " & quoted form of aLine & " | grep -E '^[0-9]{8}$' > /dev/null; printf $?"
if isMatch = "0" then
set caseNumber to aLine
end if
end repeat
else
set caseNumber to do shell script "echo " & quoted form of msgSubject & " | sed s/[a-zA-Z[:punct:]\\]//g | awk '{ print $1 }'"
end if
repeat 1 times -- # fake loop
set msgFolder to (every mailbox of account "Exchange" whose name contains caseNumber)
if (msgFolder is {}) then
if caseNumber is not in createFolders then
copy caseNumber to the end of createFolders
end if
exit repeat
#tell account "Exchange"
# set newMailbox to make new mailbox with properties {name:(caseNumber), container:(casesFolder)}
#end tell
end if
set msgFolder to (every mailbox of account "Exchange" whose name contains caseNumber)
set theMessage's mailbox to item 1 of msgFolder
end repeat
end repeat
if createFolders is not {} then
set folders to ""
repeat with folder in createFolders
if folders is "" then
set folders to folder
else
set folders to folders & ", " & folder
end if
end repeat
display dialog "Please create folder for case " & folders buttons {"ok"} default button 1
end if
end tell
end if
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment