Automate sending images via text with Mac OS Messages
Photo by Firmbee.com 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!
1#!/bin/zsh23# Path to the file containing phone numbers4PHONE_NUMBERS_FILE="phone_numbers.txt"56# Path to the image to be attached7IMAGE_PATH="invite.jpg"89# Log file10LOG_FILE="send_messages.log"1112# Function to log messages to both the screen and the log file13log_message() {14 echo "$1"15 echo "$1" >> "$LOG_FILE"16}1718# Start logging19log_message "Script started at $(date)"2021# Check if the phone numbers file exists22if [[ ! -f "$PHONE_NUMBERS_FILE" ]]; then23 log_message "Phone numbers file $PHONE_NUMBERS_FILE does not exist."24 exit 125fi2627# Check if the image file exists28if [[ ! -f "$IMAGE_PATH" ]]; then29 log_message "Image file \"$IMAGE_PATH\" does not exist."30 exit 131fi3233# Log the contents of the phone numbers file34log_message "Contents of $PHONE_NUMBERS_FILE:"35cat "$PHONE_NUMBERS_FILE" | while read -r line; do36 log_message "$line"37done3839# Read the phone numbers from the file and send the image40while read -r PHONE_NUMBER; do41 if [[ -n "$PHONE_NUMBER" ]]; then42 log_message "Processing phone number: $PHONE_NUMBER"4344 # Copy image to clipboard using AppleScript45 osascript <<EOF46 set the clipboard to (read (POSIX file "$(realpath "$IMAGE_PATH")") as JPEG picture)47EOF4849 # Send image using GUI scripting50 osascript <<EOF51 tell application "Messages" to activate52 delay 153 tell application "System Events"54 keystroke "n" using {command down}55 delay 156 keystroke "$PHONE_NUMBER"57 keystroke return58 delay 159 repeat 5 times60 keystroke tab61 delay 0.262 end repeat63 repeat 6 times64 key code 5165 delay 0.266 end repeat67 keystroke "v" using {command down}68 delay 169 keystroke return70 end tell71EOF72 log_message "Image sent to $PHONE_NUMBER"73 sleep 37475 else76 log_message "Empty or invalid phone number line encountered"77 fi78done < "$PHONE_NUMBERS_FILE"7980log_message "Script ended at $(date)"
Instructions for creating and running the script:
- Create a new file with a .sh extension (e.g. send_image_invites.sh)
- Copy the above code into the file
- Replace
$IMAGE_PATH
with the full path to the image file you want to send - Create a file containing the phone numbers you want to send the image to, one per line (e.g. phone_numbers.txt)
- Replace
$PHONE_NUMBERS_FILE
with the full path to the phone numbers file - Save the script file
- Open Terminal and navigate to the directory containing the script file
- Make the script executable with the command:
chmod +x send_image_invites.sh
- Run the script with the command:
./send_image_invites.sh
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.