Messages – send multiple images

I’m trying to send multiple images in a single Message using AppleScript.

I can do it manually in iOS Messages by selecting images in Photos and sending them all in one message to a recipient. Otherwise on the Mac I can drag multiple images into the message pane of the Messages app. It seems to create multiple paragraphs, with each image on its own paragraph. Using AppleScript on the Mac however, I can only see how to send one image at a time. This results in a new notification for each image which is not ideal.

Looking at the Messages AppleScript dictionary, it seems multiple attachments can be added to rich text but I can’t work out how to create a rich text object or a message object that can accept attachments or images. Any ideas?

Here is the code that can send images as multiple messages.


	tell application "Messages"
	set targetService to (1st service whose service type = iMessage)

	--This is the person's contact as text
	set MessengerContact to "1234 123 123"

	--This is the buddy that corresponds to this text
	set targetBuddy to buddy MessengerContact of targetService

	--The variable FileList is a list of aliases
	repeat with i from 1 to (count FileList)
		set targetAttach to item i of FileList
		send targetAttach to targetBuddy
	end repeat

	end


To create a script that binds multiple files to a single message is a useless task. This is because the total size of files should not exceed 1-10 MB. Simply the server will refuse to send a huge message. Instead, you just need to build a text that includes a list of links to these files. Building similar text with AppleScript is simple task. You create empty string, then add in loop each link to end of this string.

Maybe Messages allow adding links like nextLoopLinkString as URL. I did not try, I do not know. If not, add them as plain text. Use & return at the end of each added link.

I don’t have an answer on how to attach the files, but in reply to KniazidisR’s response - a lot of texted image jpg’s are 100k or less. You could put 100 of them on a max 10 MB text message. For all we know, his goal is to attach 2 ≈30k images to each message. Doing so is not a “useless task.” I text multiple images in one message to people via UI on my phone all the time and it’s never caused a problem I’m aware of.

As he noted, he can accomplish what he wants via the UI, so I don’t think dismissing what he wants as impractical is likely an accurate criticism.

Okay. Once you insist …


set theImagesAliasList to (choose file of type {"public.image"} with multiple selections allowed)

set theImagesPosixList to {}
repeat with i from 1 to (count of theImagesAliasList)
	set theItem to item i of theImagesAliasList
	tell application "System Events" to set theItem to (POSIX path of theItem)
	set end of theImagesPosixList to POSIX file theItem
end repeat

tell application "Messages" to activate
tell application "System Events" to tell application process "Messages"
	
	click menu item "New Message" of menu 1 of menu bar item "File" of menu bar 1
	click text field "To:" of scroll area 3 of splitter group 1 of window "Messages"
	keystroke "1234 123 123" & return
	
	click text area 1 of scroll area 4 of splitter group 1 of window "Messages"
	keystroke "hi, have new images!"
	
	repeat with theItem in theImagesPosixList
		set the clipboard to theItem
		click menu item "Paste" of menu 1 of menu bar item "Edit" of menu bar 1
		delay 1
	end repeat
	
end tell

To send message, you can add before the end tell this:

keystroke return

I have this solution working on my machine running Catalina and Messages version 13, but when I tried to run this on a machine with Big Sur and Messages version 14, it failed.

Why can’t the scripting interface dictionary allow multiple images in one message! This is ridiculous! Then I wouldn’t have to futz with GUI issues.

Has anyone developed the GUI solution for this problem with Big Sur and Messages 14?

Thanks!