This site runs best with JavaScript enabled.

Automate sending images via text with Mac OS Messages

Photo by on Unsplash

How I sent out images to 80 phone numbers in 12 minutes with Mac OS Messages

My wife has been a bit overwhelmed this summer with going back to school and planning a wedding for our daughter. Last night after doing some yard work we sat down and she said, "I need your help sending out all these invites." She had a spreadsheet with 80 phone numbers. I imagined manually copying and pasting that image into Messages 80 times and it didn't sound appealing, so I said, "Let me see if I can script it".

It took longer than I had hoped because I struggled getting the image to attach and display properly and work on both iOS and Android. I don't have an Android phone to test with, so I was using my Google Voice account to send tests to. Eventually I got it to work. In the process, I added a lot of debugging statements and logging so this script may be unnecessarily complex. But it works!

3# Path to the file containing phone numbers
6# Path to the image to be attached
9# Log file
12# Function to log messages to both the screen and the log file
13log_message() {
14 echo "$1"
15 echo "$1" >> "$LOG_FILE"
18# Start logging
19log_message "Script started at $(date)"
21# Check if the phone numbers file exists
22if [[ ! -f "$PHONE_NUMBERS_FILE" ]]; then
23 log_message "Phone numbers file $PHONE_NUMBERS_FILE does not exist."
24 exit 1
27# Check if the image file exists
28if [[ ! -f "$IMAGE_PATH" ]]; then
29 log_message "Image file \"$IMAGE_PATH\" does not exist."
30 exit 1
33# Log the contents of the phone numbers file
34log_message "Contents of $PHONE_NUMBERS_FILE:"
35cat "$PHONE_NUMBERS_FILE" | while read -r line; do
36 log_message "$line"
39# Read the phone numbers from the file and send the image
40while read -r PHONE_NUMBER; do
41 if [[ -n "$PHONE_NUMBER" ]]; then
42 log_message "Processing phone number: $PHONE_NUMBER"
44 # Copy image to clipboard using AppleScript
45 osascript <<EOF
46 set the clipboard to (read (POSIX file "$(realpath "$IMAGE_PATH")") as JPEG picture)
49 # Send image using GUI scripting
50 osascript <<EOF
51 tell application "Messages" to activate
52 delay 1
53 tell application "System Events"
54 keystroke "n" using {command down}
55 delay 1
56 keystroke "$PHONE_NUMBER"
57 keystroke return
58 delay 1
59 repeat 5 times
60 keystroke tab
61 delay 0.2
62 end repeat
63 repeat 6 times
64 key code 51
65 delay 0.2
66 end repeat
67 keystroke "v" using {command down}
68 delay 1
69 keystroke return
70 end tell
72 log_message "Image sent to $PHONE_NUMBER"
73 sleep 3
75 else
76 log_message "Empty or invalid phone number line encountered"
77 fi
80log_message "Script ended at $(date)"

Instructions for creating and running the script:

  1. Create a new file with a .sh extension (e.g.
  2. Copy the above code into the file
  3. Replace $IMAGE_PATH with the full path to the image file you want to send
  4. Create a file containing the phone numbers you want to send the image to, one per line (e.g. phone_numbers.txt)
  5. Replace $PHONE_NUMBERS_FILE with the full path to the phone numbers file
  6. Save the script file
  7. Open Terminal and navigate to the directory containing the script file
  8. Make the script executable with the command: chmod +x
  9. Run the script with the command: ./

The script will log its progress to the console and send the specified image to each phone number in the phone numbers file using the Messages app on macOS. The great part about this method was that I could watch it doing all the work.

Discuss on TwitterEdit post on GitHub

Share article
Dustin Davis

Dustin Davis is a software engineer, people manager, hacker, and entreprenuer. He loves to develop systems and automation. He lives with his wife and five kids in Utah.

Join the Newsletter

Dustin Davis