Search code examples
applescriptmessage

How to use Applescript to delete Conversations whose senders contain email addresses from the Messages app


I want to use applescript to delete conversations from people who sent me messages using email addresses. I want to keep all conversations from mobile numbers, but delete all conversations, in which my correspondent is an email address.

I've tried:

tell application "Messages"
    tell every item
        delete (every item whose sender contains ".com")
    end tell
    save
end tell

Solution

  • All scriptable applications have an AppleScript Dictionary that can be accessed from Script Editor via the Window --> Library menu. Reading through an application's dictionary can help you obtain the proper terms for generating a script to do your bidding.

    The script below will successfully create a list of chat IDs of all chats (conversations) wherein at least one participant is using an email address. I have not tested the delete section, since I am not interested in deleting anything. I strongly recommend that you run Time Machine or another backup service BEFORE executing that portion of the script, just in case you get unexpected results.

    set chatsToKill to {}
    tell application "Messages"
        set allChats to every chat
        repeat with eachChat in allChats
            set thePeeps to participants of eachChat
            repeat with oneParticipant in thePeeps
                if oneParticipant's handle contains "@" then
                    if chatsToKill does not contain eachChat's id then set end of chatsToKill to eachChat's id
                end if
            end repeat
        end repeat
        repeat with deathChat in chatsToKill
            delete item 1 of (get every chat whose id = deathChat)
        end repeat
    end tell