I wrote the following script to delete messages within iMessage…
-- AppleScript to delete Message's Chats
tell application "Messages"
-- Determine the Message's window state at script start up so it can be returned said state at end of sccript
-- NOTE: It was determined through testing that Message has only one visible window
-- NOTE: It was determined through testing that Message's window's index property only applies to the Message's window as opposed to all windows
if (exists (windows whose visible = true)) then
set AppWindowStatus to "Visible"
else if (exists (windows whose minimized = true)) then
set AppWindowStatus to "Minimized"
else
set AppWindowStatus to "Closed"
end if
-- display dialog "Message's window status is" & " " & "\"" & AppWindowStatus & "\"" buttons {"OK"} default button 1-- NOTE: Comment out / delete once final. Used to test AppWindowStatus
-- Initialize the list chatCountList which is used to communicate the message number to the user
-- Initialize the counter/variable deletedChats which counts the number of Chats which are deleted
-- Initialize the list myChats which contains information on all Chats
-- Initialize the list myChatsOrdered which contains the list of dispalyed information sorted by update time
set chatCountList to {"1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th", "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th", "26th", "27th", "28th", "29th", "30th", "31st", "32nd", "33rd", "34th", "35th", "36th", "37th", "38th", "39th", "40th", "41st", "42nd", "43rd", "44th", "45th", "46th", "47th", "48th", "49th", "50th"}
set deletedChats to 0
set myChats to chats
set myChatsOrdered to {}
-- Determine / extract the information to be displayed
repeat with aChat in myChats
set aChatParticipant to participants of aChat -- Get the Chat's participant's name
set aChatParticipantName to first name of item 1 of aChatParticipant & " " & last name of item 1 of aChatParticipant
set aChatService to id of aChat -- Get the Chat's telepehone number and type [i.e. iMessage or SMS]
if (character 1 of aChatService is "i") then
set aChatTelephone to (text 12 through -1 of aChatService)
set aChatType to "iMessage"
else if (character 1 of aChatService is not "i") then
set aChatTelephone to (text 7 through -1 of aChatService)
set aChatType to "SMS"
end if
set aChatTime to updated of aChat -- Get the Chat's updated time
-- log (current date) -- NOTE: Comment out / delete once final. Used to test "current date"
-- log aChatTime -- NOTE: Comment out / delete once final. Used to test aChatTime
set aChatTime to (current date) - aChatTime
set end of myChatsOrdered to {aChatTime, aChatParticipantName, aChatTelephone, aChatType, aChat} -- Set list of data items to be sorted
-- log "myChatsOrdered Before Sort" -- NOTE: Comment out / delete once final. Used to test myChatsOrdered before sort
-- log myChatsOrdered -- NOTE: Comment out / delete once final. Used to test myChatsOrdered before sort
end repeat
-- Sort myChatsOrdered based on aChatTime [i.e. newest to oldest]
-- Set callSort to load a compiled sorting script
set sortFunction to load script "Macintosh HD:Users:JoelC:Documents:Apple:Scripts:Utilities:20141125_script to sort a list of items_specific.scpt" as alias
set myChatsOrdered to simple_sort(myChatsOrdered) of sortFunction
-- log "myChatsOrdered After Sort" -- NOTE: Comment out / delete once final. Used to test myChatsOrdered after sort
-- log myChatsOrdered -- NOTE: Comment out / delete once final. Used to test myChatsOrdered after sort
-- Determine which Chats [i.e. all Chats or specifc Chats] to delete and then delete them
if ((count of myChats) > 0) then
display dialog "Do you want to delete all chats or specific chats?" buttons {"All chats", "Specifc chats"} default button 2 with icon note
set chatDeletePreference to button returned of result
set chatDeleteSelected to 0
if ((chatDeletePreference = "All chats") is true) then
repeat with deletedChats from 1 to (count myChatsOrdered) -- Branch when user has elected to delete all Chats
set chatToDeleteData to item deletedChats of myChatsOrdered
set aChat to item 5 of chatToDeleteData
delete aChat
end repeat
display dialog "As elected, all " & deletedChats & " chats have been deleted..." buttons {"OK"} default button 1 with icon note
else
repeat with deletedChats from 1 to (count myChatsOrdered) -- Branch when user has elected to delete specific Chats
set deletedChatsString to item deletedChats of chatCountList
display dialog "Do you want to delete the" & " " & deletedChatsString & " " & "chat which is an " & item 4 of item deletedChats of myChatsOrdered & " from " & item 2 of item deletedChats of myChatsOrdered & " " & " which has an e-mail address or telephone number of " & item 3 of item deletedChats of myChatsOrdered & "?" buttons {"Yes", "No"} default button 2 with icon note
set chatDeleteSpecific to button returned of result
if ((chatDeleteSpecific = "Yes") is true) then
set chatDeleteSelected to chatDeleteSelected + 1
set chatToDeleteData to item deletedChats of myChatsOrdered
set aChat to item 5 of chatToDeleteData
delete aChat
end if
end repeat
display dialog "As elected, " & chatDeleteSelected & " specifc chats out of " & deletedChats & " total chats have been deleted..." buttons {"OK"} default button 1 with icon note
end if
else
display dialog "No chats to delete..." buttons {"OK"} default button 1 with icon note
end if
-- Return the Messages window state to the same state at script start up state
if (AppWindowStatus = "Visible") then
reopen windows
else if (AppWindowStatus = "Minimized") then
try
keystroke "cmd-m"
end try
else
quit
end if
end tell
The script worked perfectly under Yosemite (i.e. the only thing that I am aware of that changed is the upgrade from Yosemite to El Capitain)…the script is failing at the line that reads:
set aChatParticipant to participants of aChat -- Get the Chat's participant's name
with the error message "Can’t get participants of text chat id "iMessage;-;+14163005811"." number -1728 from participants of text chat id “iMessage;-;+14163005811”.
Please explain the problem and provide a fix.
TIA,
Joel