I'm poor with AppleScript, so I'd like to know if anyone can confirm that this GPT-generated AppleScript will delete all my iMessages that contains a particular string. (Naturally, replacing the specified phrases with real data). I'm concerned that I might delete too much, but I won't blame anyone else on this forum if you say this script will work, but then it deletes too much. I'm just looking for a better opinion than my own.
If it works, I might put a KM shell around it so I can run it more easily. My iMessages (stored on iCloud) has grown out of control, particularly since I send myself error messages when a script needs attention.
-- Replace "Your Specific Word" with the word you want to search for
set specificWord to "Your Specific Word"
tell application "Messages"
set targetChat to first chat whose name contains "Contact Name" -- Replace "Contact Name" with the name of the conversation
set messagesToDelete to {}
-- Iterate through each message in the conversation
repeat with aMessage in messages of targetChat
if content of aMessage contains specificWord then
set end of messagesToDelete to aMessage
end if
end repeat
-- Delete the messages that contain the specific word
repeat with aMessage in messagesToDelete
delete aMessage
end repeat
end tell
display dialog "Deletion completed for messages containing '" & specificWord & "'."
Bonus question: is there a way to tell how much data is stored in a specific iMessage conversation? ChatGPT doesn't know of an answer.
If it works (I don't use Messages much, but none of my chats have "name" values) then the main cause of deleting "too much" will be contains specificWord -- if that word is "phone" then you'd also delete messages in that chat that contained "phonetic" and "answerphone".
You can see how many messages will be deleted by replacing the deletion repeat block with return count of messagesToDelete.
I'm betting that, like mine, your chats' "name"s are all missing value. So that first whose clause is returning an empty list -- and you can't get the first item of an empty list.
You'll have to work through all the chats, testing the participants of each:
set theName to "John Smith"
set idList to {}
tell application "Messages"
repeat with eachChat in (get every chat)
if name of every participant of eachChat contains theName then copy id of eachChat to end of idList
end repeat
set targetChat to first item of idList
...
But I don't think even that will help. If you look at Message's AppleScript dictionary you'll see that chat contains only participants, there's no mention of messages -- indeed, there's no mention of messageanywhere in the dictionary.
I reckon the AI's given you false hope (or might, to be charitable, be referencing an earlier version of Messages where you could do this). Shortcuts is no help either. I don't like to give up so easily, but I can't see a way of doing what you want.
Thanks for trying. I can probably write a KM macro that can use Find Image or OCR combined with mouse clicks to manually delete messages one at a time. It will be slow, but it should work. Sigh.