Window Indexing / Ordering...

A Basic – or at least what I hope is – question noting that I did a Search and found no answer.

I have written a script to clear out the messages in the messages app which more or less works. The MAIN problem that I am having is that I want to “return” the Messages window to the same “desktop position index” it was in before I ran the script. I cant get this part of the script to work and would appreciate assistance in fixing this.

While I am new to AppleScript I am doing my best to learn so please be gentle but feel free to suggest improvements to the code.


-- AppleScript to 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 and ii) the window index determination and handling which is not working.

tell application "Messages"
	
	
	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	
	
	
	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
	
	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
		
		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
	
	
	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



Problem solved / understood…no need to respond…

I don’t know if it’s related to your original problem, but, at the beginning of the script you defined :
set AppWindowStatus to “Visble”
while, near the end you have the test
if ((AppStatus = “Open”) and (AppWindowStatus = “Visible”)) then

I doubt that the condition may be verified :wink:

Yvan KOENIG (VALLAURIS, France) vendredi 5 décembre 2014 16:29:31