Applescript to replace message sent and received in Messages applicati

I found this script to change uppercase and lowercase of characters in messages but i want to modify this code to replace some string in text, for ex: message received is “i want to eat”, this message will be change to “i like to eat” or “:-))” change to “:-)”.

property lowercaseCharacters : "abcdefghijklmnopqrstuvwxyz"
property uppercaseCharacters : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

on intercaps(str)
	set theCharacters to characters of str
	set theCount to 1
	repeat with aChar in theCharacters
		if (aChar is in uppercaseCharacters or aChar is in lowercaseCharacters) then
			if (theCount mod 2) is equal to 1 then
				set contents of aChar to character (offset of aChar in lowercaseCharacters) of uppercaseCharacters
			else
				set contents of aChar to character (offset of aChar in uppercaseCharacters) of lowercaseCharacters
			end if
		end if
		set theCount to theCount + 1
	end repeat
	return theCharacters as string
end intercaps

using terms from application "Messages"
	
	on message sent theMessage for theChat
		return intercaps(theMessage)
	end message sent
	
	on message received theMessage from theBuddy for theChat
		return intercaps(theMessage)
	end message received
	
	on chat room message received theMessage from theBuddy for theChat
		return intercaps(theMessage)
	end chat room message received
	
end using terms from

Hi,

try this


property stringsToFind : {"want", ":-))"}
property stringsToReplace : {"like", ":-)"}

on replaceStrings(str)
	repeat with i from 1 to count stringsToFind
		if str contains item i of stringsToFind then
			set {TID, text item delimiters} to {text item delimiters, item i of stringsToFind}
			set str to text items of str
			set text item delimiters to item i of stringsToReplace
			set str to str as text
			set text item delimiters to TID
		end if
	end repeat
	return str
end replaceStrings

using terms from application "Messages"
	
	on message sent theMessage for theChat
		return replaceStrings(theMessage)
	end message sent
	
	on message received theMessage from theBuddy for theChat
		return replaceStrings(theMessage)
	end message received
	
	on chat room message received theMessage from theBuddy for theChat
		return replaceStrings(theMessage)
	end chat room message received
	
end using terms from

thanks but i was used this method

property searchList : {":))", ":-))", ":((", ":-((", ":)]", ":O)", ":o)", ":-??", ":@)", ":)>-", ";))", ":(|)"}
property replaceList : {".:))", ".:-))", ".:((", ".:-((", ".:)]", ".:O)", ".:o)", ".:-??", ".:@)", ".:)>-", ".;))", ".:(|)"}
to switchText of t from s to r
	set text item delimiters to s
	set t to t's text items
	set text item delimiters to r
	tell t to set t to beginning & ({""} & rest)
	t
end switchText
to convertText(t)
	set d to text item delimiters
	considering case
		repeat with n from 1 to count searchList
			set t to switchText of t from my searchList's item n to my replaceList's item n
		end repeat
	end considering
	set text item delimiters to d
	t
end convertText
using terms from application "Messages"
	
	on message sent theMessage for theChat
		try
			return convertText(theMessage)
		end try
	end message sent
	
	on message received theMessage from theBuddy for theChat
		return convertText(theMessage)
	end message received
	
	on chat room message received theMessage from theBuddy for theChat
		return convertText(theMessage)
	end chat room message received
	
end using terms from

This code worked fine for Yahoo account, but it dont work when i received messages from iMessage and Jabber account. I tried to look into Alerts event and i found “Message received in active chat” ,maybe iMessage and Jabber account use this event to alert received message, but the problem is this object not defined in applescript. How can i define it, or make this code work with iMessage and Jabber account.

after long long times for google and test myseft, i found yahoo account have problem too, because active chat :mad: why is apple dont define this object "“Message received in active chat”’ :mad: