Get all unique messages list (plain AppleScript version)

set {uniqueMessages, IDs} to {{}, {}}

tell application "Mail"
	repeat with anAccount in (accounts)
		repeat with aMailbox in (mailboxes of anAccount)
				set aMessages to messages of aMailbox
				repeat with k from 1 to count aMessages
					set aMessage to item k of aMessages
					set aMessageID to id of aMessage
					if (IDs contains aMessageID) then
						-- do nothing
						set end of IDs to aMessageID
						set end of uniqueMessages to aMessage
					end if
				end repeat
			end try
		end repeat
	end repeat
end tell

return uniqueMessages