messages - send file

I’m trying to figure this out.

I have this script that works beautifully in macOS 10.11 It sends the file “test.txt” to phone number 15555555555 (obviously not this but a real number) through messages which sends instantly and received as instantly.

set attached to POSIX file "/Users/<userAccount>/Desktop/test.txt"
	tell application "Messages"
	send attached to buddy "+15555555555" of service 1
end tell

I bring the script to macOS 12. A few things one the script changes from the above when I compile it. Some of the reserved words change.

set attached to POSIX file "/Users/<userAccount>/Desktop/test.txt"
tell application "Messages"
	send attached to participant "+15555555555" of account 1
end tell

Issue:
it seems to work and attempts to send the file I can see the process bar. But it takes more than 5 minutes and eventually times out and says “Message Failed to Send” > The message to “+15555555555” failed to send. I tried it on 2 different macOS 12 machines with the same result.

Question:
Does anyone know how to fix the issue?

thanks

It seems you have to use the send command to a chat, not a participant.

Try this:

set attached to POSIX file "/Users/<userAccount>/Desktop/test.txt"
tell application "Messages"
	--send attached to participant "+15555555555" of account 1
	set acs to accounts whose service type is iMessage
	repeat with ac in acs
		set myChats to every chat of ac
		if (count myChats) > 0 then exit repeat -- find the account that has the chats
	end repeat
	set ac to contents of ac
	repeat with myChat in myChats
		set ParticipantNames to full name of participants of myChat
		if (count ParticipantNames) = 1 then -- find the chat with only one participant
			try
				-- of these chats with 1 participant, find the one with the participant we are looking for
				if (item 1 of ParticipantNames) = "Robert Fernandez" then 
					set myParticipant to item 1 of participants of myChat
					exit repeat
				end if
			end try
		end if
	end repeat
	send attached to myChat
end tell