Newbie Here...Would Appreciate Some Help WIth A Messages Script

I have done some – but much more to do – reading on AppleScript to do the following:

  1. Open Messages

  2. Delete the SMS / iMessage conversations that are listed in Messages

  3. Close Messages

I have searched the forum to see whether I could find some code to get me started but came up empty.

Would appreciate either being pointed in the right direction or some code to get me started.

Thanks.

Hi.

What have you got so far? The best way of learning AppleScript is to fumble through your first few.

Just because the service/service type properties may be a bit confusing for someone learning, this should point you in the right direction…

tell application "Messages"
	if not (exists (windows whose visible = true)) then reopen
	
	set myChats to chats
	repeat with aChat in myChats
		if service type of (aChat's service) = iMessage then
			-- insert your code to delete aChat ;)
		end if
	end repeat
	
	-- insert your code to quit ;)
end tell

Appreciate the response and will most definitely post where I land / struggle through the process.

The key difficulty – as you note – is learning how to reference the different components of an application / window…a good reference would be most helpful, any suggestions.

Thanks.

In AppleScript Editor, command + shift + o to inspect an Application’s dictionary.

AppleScript 1-2-3 & AppleScript: The Definitive Guide are good place to start.

There are also some very good tutorials here:
http://macscripter.net/viewforum.php?id=31

Much appreciated…looking forward to sending you back what I craft for this AppleScript hopefully in the next day or two depending on work…

I decided to give this a try on my own – without referring you code – and below is what I got.

I know it is a sad / weak attempt but please recall I have only been doing this for a few days.

Please pay attention to the comments that I have added.

** START OF SCRIPT

tell application “Messages”

repeat with i from 1 to 2 -- the index "2" needs to be be replaced with code that i) determines the actual number of messages and ii) termnates the script in the event that there are no messages to delete, PLEASE HELP /PROVIDE CODE!
	
	get chat i
	
	display dialog "The" & " " & i & " " & "iMessage has telephone number " & chat i id buttons {"OK"} default button 1 -- display dialog added to confirm the repeat command is looping properly and this will be removed from the final script, PLEASE HELP / PROVIDE CODE in how to display / reference the chat i's telephone number as the current code is not working
	
	close chat i -- PLEASE HELP / PROVIDE CODE in that the syntax close / delete / quit are not the right commands as none of them are black when compiled...this illustrates one of the biggest problems I am having namely how does one find the applcable command
	
	
end repeat

display dialog "All Messages have been successfully deleted" buttons {"OK"} default button 1

end tell

Great. This is much better than asking for code!

Loop Syntax:
To get the count of messages

set myChats to chats
repeat with i from 1 to (count myChats)
	set aChat to item i of myChats
end repeat

However, If you want to iterate through every item in a list, use this:

 set myChats to chats
repeat with aChat in myChats
end repeat

Using your newly learned loop which has aChat variable:

display dialog "The iMessage has telephone number " & id of aChat buttons {"OK"} default button 1

Yes they are.

if service type of (aChat's service) = iMessage then delete aChat

Putting it together

tell application "Messages"
	-- Make sure the window is open
	if not (exists (windows whose visible = true)) then reopen
	set myChats to chats -- set my variable
	
	set deletedChats to {} -- create an empty list 
	repeat with aChat in myChats -- use the correct form of the loop
		display dialog "The iMessage has telephone number " & id of aChat buttons {"OK"} default button 1
		if service type of (aChat's service) = iMessage then
			delete aChat -- delete the chat
			set end of deletedChats to aChat -- add to deletedChats list to indicate chats were deleted
		end if
	end repeat
	
	if deletedChats = {} then return -- terminate if not chats were deleted
	
	display dialog "All Messages have been successfully deleted" buttons {"OK"} default button 1
	quit -- quit
end tell

I will tackle my second script tomorrow night…I am determined to learn this!..am grateful for all the help!

Sorry for what follows but I am asking in the interest of learning!

A few follow ups:

  1. Am unclear as to where I could even find the identifier / word “chats” to reference all chats…how / where do I go to learn that this exists?

  2. Like the elegance of the second loop as there is one less step / variable…presumably this is a feature of AppleScripts repeat loops in that aChat will successively loop through each chat in myChats?

  3. Why could do you need to define myChats? Why could you not have written “repeat with aChat in chats”?

This is encouraging in the I had the right idea [i.e. chat i id] but did not have the right syntax [i.e. id of chat i].

While I appreciate your revision I have further revised this line of code to provide more detail as follows:


display dialog "The" & " " & number of aChat & " " & "iMessage has an e-mail address or telephone number of " & id of aChat buttons {"OK"} default button 1

  1. Am unclear as to where I could even find that id of aChat needs to be referenced as “id of aChat” rather than aChat id…is this just a convention that I will eventually get used to?

  2. Is there a way to translate the number of aChat from 1 2 3 4 etc. (as it loops through) to first, second, third, fourth, etc.?

  3. Why is the dialogue box contain “iMessage;-;” the “text portion” of the dialog box and id of aChat [presumably this is output by / from “if of aChat”]? And, how do I remove it to increase readability?

A few follow ups:

  1. As the messages application can contain both iMessages and SMS messages do I need to change the “iMessage” to “iMessage or SMS Message” or whatever the correct syntax is for SMS Messages? And, if yes, where do I find the syntax for SMS Messages?

  2. Can you please explain the need / use of service type as I want to delete everything…iMessage, SMS Message, video Messages, etc.?

And if you did not think I am learning check this out [not much for you but a moment of accomplishment for me :slight_smile: ]…i changed the last part of the code to read:


if (number of myChats > 0) then -- Display a dialog box depending / confirming action
		display dialog "All Messages have been successfully deleted" buttons {"OK"} default button 1
	else
		display dialog "No Messages to delete" buttons {"OK"} default button 1
	end if

In the above i recognized that myChats is essentially all of the chats and that I have to reference one of its characteristics / properties…is there an easy way to keep straight whether a variable has / does not have properties that need to be referenced?

AGAIN, TRULY, THANKS FOR ALL THE HELP…I WILL WRITE ANOTHER SCRIPT TOMORROW AND CAN INLY HOPE THAT YO WILL BE EQUALLY HELPFUL…

EDIT…THE FOLLOWING TEXT WAS ADDED ON NOVEMBER 19 AT 9:00 AM EST…HERE GOES…

I am continuing to try to learn so have taken the bulls by the horns and have written additional code to return the Messages window to the same sate it was in before the script was run. The resulting apple script is as follows with the new code appearing at the beginning and at the end [note: I tried to highlight the new test in green but it did not work within AppleScript though it should be easy for you to identify]:



tell application "Messages"
	
	if (exists (windows whose minimized = true)) then -- Windows can be in one of three states, test whether minimized to return to minimized when the script exits
		set AppStatus to "Open"
		set AppWindow to "Minimized"
		set AppIndex to 0
	end if
	
	if (exists (windows whose visible = true)) then -- Window can be in one of three states, test whether maximized / visible to return to maximized / visible when script exits
		set AppStatus to "Open"
		set AppWindow to "Visble"
		set AppIndex to get the index of windows
	end if
	
	if not ((exists (windows whose minimized = true)) or (exists (windows whose visible = true))) then -- Window can be in one of three states, test wether closed to return to closed when script exits
		set AppStatus to "Closed"
		set AppWindow to "Closed"
		set AppIndex to 0
	end if
	
	display dialog AppStatus & "     " & AppWindow & "   " & AppIndex buttons {"OK"} default button 1 -- Display dialog to test window states, will be removed in final compilation	

	
	set myChats to chats -- Set the myChats variable to the "list of all chats"
	
	
	
	repeat with aChat in myChats -- Loop through each aChat in myChats to identify and delete each chat
		display dialog "The" & " " & number of aChat & " " & "message has an e-mail address or telephone number of " & id of aChat buttons {"OK"} default button 1 -- Dialog to identify each chat to confirm looping
		if service type of (aChat's service) = iMessage then delete aChat -- Delete each chat
	end repeat
	
	
	
	if (number of myChats > 0) then -- Dialog to display the number of deleted chats and actions taken
		display dialog "All messages have been successfully deleted..." buttons {"OK"} default button 1
	else
		display dialog "No messages to delete..." buttons {"OK"} default button 1
	end if
	
	
	
	if ((AppStatus = "Open") and (AppWindow = "Minimized")) then set windows to minimized -- Return to minimized state
	
	if ((AppStatus = "Open") and (AppWindow = "Visible")) then -- Return to visisble state with the same index
		set windows to visible
		set index of windows to AppIndex
	end if
	
	if (AppStatus = "Closed") then quit -- Return to closed sate
	
	
	
end tell


There are a few problem that I need help with in the above code:

  1. If the window’s initial state is minimized then the state IS being properly identified as minimized but IS NOT being return to minimized as the code “if ((AppStatus = “Open”) and (AppWindow = “Minimized”)) then set windows to minimized” does not work [i.e. it balks at set windows to minimized". Please help me fix this.

  2. If the window’s initial state is visible then the state IS being properly identified as visible and the index is being determined is IS NOT being returned to the same indexed position among the open / visible window. Please help me fix this.

  3. Related point, is it possible to run this script without opening the Messages window.

  4. Unrelated point, once this script is finalized, what is the best way to run it…is it simply to save the script as an application and assign it a keystroke or through spotlight?

I have worked on this further today and have amended the bottom portion of the script as follows:



if ((AppStatus = "Open") and (AppWindow = "Minimized")) then -- Return to minimized state	
		try
			keystroke "cmd-m"
		end try
	end if
	
	if ((AppStatus = "Open") and (AppWindow = "Visible")) then -- Return to visisble state with the same index
		reopen
		set index of windows to WindowIndex
	end if
	
	if (AppStatus = "Closed") then quit -- Return to closed state

I think that’s solves problem 1 (above) [i.e. minimize now works] and part of 2. (above) [i.e. maximize / visible now works BUT window index is still not working]…please help me to get indexing working and, to the extent there is a more elegant way, how to control the window’s state.

Thanks for all you help!!!

I have continued to work on and evolve my script…I have two key issues that are not resolved namely:

  1. I am trying to access / parse out the buddy fulsome [i.e. John Doe] from the chats but cannot get the code to work…would appreciate assistance with this; and

  2. I cannot get the Messages window to return to the same relative position AMONG ALL OTHER WINDOWS and, again would appreciate assistance with this.

Thanks,

Joel


-- AppleScript to delete the delete the chats in Messages where chats = {iMessages, SMS}

-- Items to be resolved include 
--   i) the list for the service types in addition to iMessage [NOTE: a) Parsing the id of aChat is one work-around and b) the service classes = {AIM, Bonjour, Jabber, andŒiMessage}
--  ii) the window index determination and handling which is not working.

tell application "Messages"
	
	-- Determine the Messages window state at script start up	
	if (exists (windows whose minimized = true)) then -- Messages window can be in one of three states, test whether it is minimized to return to minimized when the script exits
		set AppStatus to "Open"
		set AppWindowStatus to "Minimized"
		set AppWindowIndex to {}
	end if
	
	if (exists (windows whose visible = true)) then -- Messages window can be in one of three states, test whether it is maximized / visible to return to maximized / visible when script exits
		set AppStatus to "Open"
		set AppWindowStatus to "Visble"
		set AppWindowIndex to get the index of windows
	end if
	
	if not ((exists (windows whose minimized = true)) or (exists (windows whose visible = true))) then -- Messages window can be in one of three states, test wether it is closed to return to closed when script exits
		set AppStatus to "Closed"
		set AppWindowStatus to "Closed"
		set AppWindowIndex to {}
	end if
	
	display dialog AppStatus & "     " & AppWindowStatus & "   " & AppWindowIndex buttons {"OK"} default button 1 -- Display dialog to test application status, application window status and application window index, will be removed in final compilation	
	
	-- Initialize the variables myChats and deletedChats
	set myChats to chats -- Set the myChats variable to the list of all chats
	set deletedChats to 0 -- Set / initialize the deletedChats variable to 0
	
	-- Count, identify and delete the chats
	repeat with aChat in myChats -- Loop through with aChat taking on the value of each chat in myChats to count, identify and remove chats			
		
		set deletedChats to deletedChats + 1 -- Increase the number of deleted chats by 1
		set contactDetails to id of aChat -- Set / initialize the chat e-mail address or telephone number
		set serviceType to id of aChat -- Set / initialize the type of chat
		
		-- set myBuddies to buddies
		-- repeat with aBuddy in myBuddies
		-- set id of aBuddy to aBuddyString as text
		-- display dialog aBuddyString buttons {"OK"} default button 1
		-- end repeat
		set buddyDetails to full name of buddy
		-- display dialog buddyDetails buttons {"OK"} default button 1
		
		
		if service type of (aChat's service) = iMessage then -- Parse values for iMessages, differs from SMS because "iMessages" is longer than "SMS"
			set contactDetails to (text 12 through -1 of contactDetails)
			set serviceType to (text 1 through 8 of serviceType)
		end if
		
		if service type of (aChat's service) is not iMessage then -- Parse values for SMS, differs from iMessage because "iMessages" is longer than "SMS"
			set contactDetails to (text 7 through -1 of contactDetails)
			set serviceType to (text 1 through 3 of serviceType)
		end if
		
		display dialog "The" & " " & deletedChats & " " & "message is an " & serviceType & " and has an e-mail address or telephone number of " & contactDetails buttons {"OK"} default button 1 -- Dialog to identify each chat to confirm looping
		-- if service type of (aChat's service) = iMessage then delete aChat -- Delete each chat, limited to iMessages
		-- delete aChat -- Delete each chat {iMessages, SMS, ect.}
	end repeat
	
	-- Return the Messages window state to its script start up state
	if (number of myChats > 0) then -- Dialog to display the number of deleted chats and related action
		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
	
	if ((AppStatus = "Open") and (AppWindowStatus = "Minimized")) then -- Return to minimized state	
		try
			keystroke "cmd-m"
		end try
	end if
	
	if ((AppStatus = "Open") and (AppWindowStatus = "Visible")) then -- Return to visisble state with the same index
		reopen
		set the index of windows to AppWindowIndex
	end if
	
	if (AppStatus = "Closed") then quit -- Return to closed state
	
	
end tell

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

  1. 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].

  2. 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.

  3. 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]?

Hi JoelC.

I can’t help much with Messages, since I don’t use it, don’t have it set up, and therefore can’t test anything with it.

One thing I have noticed in your script is that although you’re sorting a list (myChatsOrdered) of selected data from your chats (sorted on the number of seconds since the chats were updated), you’re cycling through the original, unsorted list of chats (myChats) in the loop which determines which messages to delete. You’re displaying information from the sorted list, but deleting chats from the unsorted list.

A way round this may be to append each chat reference to its corresponding data list before the sort, ie. change .

set end of myChatsOrdered to {aChatTime, aChatParticipantName, aChatTelephone, aChatType} -- Set list of data items to be sorted

. to:

set end of myChatsOrdered to {aChatTime, aChatParticipantName, aChatTelephone, aChatType, aChat} -- Set list of data items to be sorted

Then change the deletion section further down to something like this:

-- Determine which Messages to delete and then delete them	
if ((count of myChats) > 0) then
	repeat with deletedChats from 1 to (count myChatsOrdered)
		set deletedChatsString to item deletedChats of chatCountList
		set chatData to item deletedChats of myChatsOrdered
		display dialog "The" & " " & deletedChatsString & " " & "message is an " & item 4 of chatData & " from " & item 2 of chatData & " " & " and has an e-mail address or telephone number of " & item 3 of chatData buttons {"OK"} default button 1 -- Dialog to identify each chat to confirm looping
		set aChat to item 5 of chatData -- Retrieve the chat reference from this list in the sorted list.
		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

There are things which can be done to tidy up the code, but this is just to get the chats deleted in the sorted order.

Hi JoelC,
Just a little update. Rather than minimizing the window using:

try
           keystroke "cmd-m"
       end try

… you can use:

set minimized of window thisReference to true

To get the properties of a window you can use:

tell application "Messages"
	get properties of window 1 -- change reference to window as required
end tell

This returned the information below in the ‘Result’ window of the Script Editor.

HTH

Exactly and, per my previous posting, I identified this as being the issue but did not know how to fix it.

Brilliant, truly brilliant…I never would have thought of this on my own.

I am happy to report that I tested these changes on a short list of messages – 3 in total – and it worked perfectly…I will continue testing it but believe [and hope that] you have provided the insight and solution…Much thanks!!

I am sure that there are many things in the code that can be tidied up a GREAT DEAL…I will work on this in the coming days but for now am just thrilled to have a nearly completed scripts largely thanks to you…perhaps I took on a little to much for my first scripting effort?

TecNik:

As I still have a great deal to learn the provided information is both appreciated and helpful.

Thanks,

Joel

Just a brief update for everyone following this thread…here goes:

  1. A big thank-you to everyone who helped me along the way with my understanding and use of AppleScript.

  2. I have now finished the first script which I wrote – albeit with your help – on my own. The code is likely not elegant or compact but i) the script works and ii) this, the clean up, is something I will work on as my skills improve.

  3. That said, for those interested, the script is posted here for all to enjoy / provide input on on / use / etc.

  4. The easiest / preferred way of using the script – at least for me – is to compile and save it as an application and then from Spotlight call / execute the application…I have name the application “messages delete” for obvious reasons.

Once again, a huge thank you to all!


-- 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 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)
	end tell
	-- 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
			
		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
				
				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
			
		end if
		
	else
		display dialog "No chats 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

PS. I have to go, now working on my second Applescript and enjoying the learning!