I have continued to work on my script which has evolved with both learning and time…the current script is as follows:
-- AppleScript to delete Messages
tell application "Messages"
-- Determine the Messages window state at script start up so it can be returned later
-- NOTE: It was determined through testing that Messages only has one visible window
-- NOTE: It was determined through testing that Messages window's index property only applies to the Messages' window as opposed to all windows
if (exists (windows whose visible = true)) then
set AppWindowStatus to "Visisble"
else if (exists (windows whose minimized = true)) then
set AppWindowStatus to "Minimized"
else
set AppWindowStatus to "Closed"
end if
-- display dialog "Messages' 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 Message'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 Message'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 Message's updated time
set aChatTime to (current date) - aChatTime
set end of myChatsOrdered to {aChatTime, aChatParticipantName, aChatTelephone, aChatType} -- Set list of data items to be sorted
log myChatsOrdered -- NOTE: Comment out / delete once final. Used to test myChatsOrdered
end repeat
-- Sort myChatsOrdered based on aChatTime [i.e. newest to oldest]
-- Set callSort to load a compiled sorting script
set callSort to (load script "/Users/JoelC/Documents/Apple/Scripts/Sort/20141125_script to sort a list of items_specific_complete.scpt")
-- Tell callSort to sort myChatsOrderd
tell callSort
set myChatsOrdered to simple_sort(myChatsOrdered)
log myChatsOrdered -- NOTE: Comment out / delete once final. Used to test myChatsOrdered
end tell
-- Determine which Messages to delete and then delete them
if ((count of myChats) > 0) then
repeat with aChat in myChats
set deletedChats to deletedChats + 1
set deletedChatsString to item deletedChats of chatCountList
display dialog "The" & " " & deletedChatsString & " " & "message is an " & item 4 of item deletedChats of myChatsOrdered & " from " & item 2 of item deletedChats of myChatsOrdered & " " & " and has an e-mail address or telephone number of " & item 3 of item deletedChats of myChatsOrdered buttons {"OK"} default button 1 -- Dialog to identify each chat to confirm looping
-- delete aChat -- Delete each chat {iMessages, SMS, ect.}
end repeat
display dialog "All " & deletedChats & " messages have been successfully deleted..." buttons {"OK"} default button 1
else
display dialog "No messages to delete..." buttons {"OK"} default button 1
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
A few follow ups for everyone
-
As far as the coding was concerned it is not yet complete in that I need to add some functionality [see 2 below] and I need to fix a bug [see 3 below].
-
I want to add functionality to i) delete all of the chats [without a dialog for each one] and ii) delete selected chats [provide the user with a delete / keep option]. I will work on this tomorrow.
-
There are two bugs I would like you input on:
a) I have added code to sort the chats by their updated property [i.e. defined per the dictionary as the date the last chat was updated] but am running into a problem in that for the most recent chat the date returned is incorrect [i.e. the actual date as it appears in the messages app was November 30 at 9:30 PM whereas the script returned time was November 21 at 8:30 AM]. Any ideas?
b) Ignoring the issue problem in 3a, the chats are being deleted in a random [at least one that I cannot determine] order. The issue / problem is that while I have successfully collected and sorted each chats’ “parameters” [i.e. name, telephone number, service type] I am not re-ordering the actual chats themselves. How can I fix / solve this as the dictionary indicates that chat’s properties are essentially all read only so I have no ability to resort the chats themselves.
While I hope that this is note case, is it possible that this is just one of those cases where the chats are deleted based on some order [possibly when the chat was started] and that there is no way to address this problem [i.e. I just need to live with the fact that the chats will be deleted in a different order than they appear in the Messages window and, because of that, remove all of the sort logic that is built into the script]?