Sending an iMessage via Osascript to Multiple Recipients

Hi,

I’m not a macscripter yet, but I’m learning. I’m currently sending a text message to myself via the following code successfully, but want to be able to send it to two numbers. Can anyone assist? Code below:

#Embedded in my script which calls a seperate file called SendMessage.scpt

os.system(“osascript /Users/Username/SendMessage.scpt 5555551234 ‘Hi There!!’”)

#SendMessage.scpt file contents below
on run {targetBuddyPhone, targetMessage}
tell application “Messages”
set targetService to 1st account whose service type = iMessage
set targetBuddy to participant targetBuddyPhone of targetService
send targetMessage to targetBuddy
end tell
end run

Thank you!!

Hi.

This is largely untested as I don’t use osascript or Messages myself, I’m not familiar with your method of invoking osascript, and the Messages terminology in your AppleScript code doesn’t match that in the Messages dictionary on my Mojave machine! But taking you at your word that the AS code does work, you could possibly modify it like this:

on run argv
	-- Treat all but the last parameter as a buddy phone number …
	set phoneNumbers to items 1 thru -2 of argv
	-- … and the last parameter as the message.
	set targetMessage to item -1 of argv
	repeat with targetBuddyPhone in phoneNumbers
		tell application "Messages"
			set targetService to 1st account whose service type = iMessage
			set targetBuddy to participant targetBuddyPhone of targetService
			send targetMessage to targetBuddy
		end tell
	end repeat
end run

Then the osascript call would simply contain all and as many numbers as you want followed by the message:

os.system("osascript /Users/Username/SendMessage.scpt 5555551234 5556664321 5556667890 'Hi There!!'")

@Nigel_Garvey
I tested your code on my Mac, running macOS Ventura 13.4.1. When I ran it on two phone numbers, it properly sent a message to each of those numbers.
I don’t know if the original poster, @CaptainNewb, wanted separate messages to each, or for it to do them as a group. But, if separate, then the code you gave works on Ventura, at least.

Hi @Krioni.

Thanks for trying the code. It’s good to know it does actually work! :slight_smile: Looking at it again, I could perhaps have put the repeat after the line which gets the account, but the small increase in efficiency probably wouldn’t make a lot of difference to the user experience.

on run argv
	-- Treat all but the last parameter as a buddy phone number …
	set phoneNumbers to items 1 thru -2 of argv
	-- … and the last parameter as the message.
	set targetMessage to item -1 of argv
	tell application "Messages"
		set targetService to 1st account whose service type = iMessage
		repeat with targetBuddyPhone in phoneNumbers
			set targetBuddy to participant targetBuddyPhone of targetService
			send targetMessage to targetBuddy
		end repeat
	end tell
end run
1 Like