Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save solarmicrobe/a99a72759f9fdac1f8e35fd1957291df to your computer and use it in GitHub Desktop.
Save solarmicrobe/a99a72759f9fdac1f8e35fd1957291df to your computer and use it in GitHub Desktop.
Delete all from sender - an Applescript for the Mail.app in Mac OS X that will search for all messages set by the sender of the email message currently open and allow you to delete them all at once.
-- Delete all from sender - https://gist.github.com/xunker/44fa4404c882c27e0ad25857e9470552
-- For the Mail.app in Mac OS X. It will search for all messages set by the sender of the email message currently open,
-- highlight them all and ask if you want to delete them. If you choose "yes" it will delete them for you and then clear
-- the search.
--
-- Important notes:
-- You can load this in Script Editor and then save it as an application so you can run it without Script Editor being open.
-- This does not work when Mail.app is in fullscreen mode; it must be in windowed mode.
-- You will need to enable "Assistive Devices" support for Script Editor (if run there) or for the .app you saved if you converted it to a standalone app.
tell application "Mail"
set theSenderList to {}
set theMessages to the selected messages of message viewer 0
repeat with aMessage in theMessages
set end of theSenderList to sender of aMessage
end repeat
--set the clipboard to (theSenderList as rich text)
-- beep
set senderEmail to (theSenderList as rich text)
--display alert "alert" message senderEmail
set senderEmail to do shell script "echo '" & senderEmail & "' | grep -o '[[:alnum:]+\\.\\_\\-]*@[[:alnum:]+\\.\\_\\-]*'"
--display alert "alert" message senderEmail
if senderEmail is equal to "" then
error "Broken"
end if
activate application "Mail"
tell application "System Events"
keystroke "f" using {option down, command down}
tell process "Mail"
tell window 1
set search_text_field to text field 1 of the last group of toolbar 1
tell search_text_field
set the value of attribute "AXValue" to senderEmail
--keystroke senderEmail
end tell
end tell
end tell
--tell application "System Events" to keystroke "f" using {option down, command down}
key code 36 -- press RETURN key
end tell
delay 0.5
set msgs to {}
tell application "Mail"
set message_list to message viewer 1
set msgs to every message of message_list
tell application "System Events" to tell process "Mail" to keystroke "1" using command down
tell application "System Events" to tell process "Mail" to keystroke "a" using command down
delay 0.5
end tell
set the message_count to the count of msgs
set userCanceled to false
try
set dialogResult to display dialog "Delete these " & message_count & " messages?"
on error number -128
set userCanceled to true
end try
set clearSearch to false
if userCanceled then
-- display alert "alert" message "OK, not deleting nothin'!"
try
set clearDialog to display dialog "Clear search?"
if button returned of clearDialog is "OK" then
set clearSearch to true
end if
on error number -128
set clearSearch to false
end try
else
if button returned of dialogResult is "OK" then
tell application "Mail"
-- --display alert "alert" message "your message"
-- --activate application "Mail"
tell application "System Events" to key code 51
delay 3
-- repeat with i from 1 to the message_count
-- set this_message to item i of msgs
-- delete this_message
-- delay 0.5
-- end repeat
set clearSearch to true
end tell
end if
end if
if clearSearch then
tell application "Mail"
tell application "System Events"
keystroke "f" using {option down, command down}
key code 53 -- press ESC key
keystroke "1" using command down
end tell
end tell
end if
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment