
Automate iMessages with AppleScript
If you’re having a party, there are plenty of tools out there for creating great invitations. But you probably don’t have emails for most of your friends. If you’re in the US, you probably have their phone numbers though. But if your guest list is very long, texting invites to everyone can be a pain. And if you’re a developer like me, sending programmatically seems like the way to go.
Surge is the easiest way to add text messaging capabilities to your application. We make carrier registration easier and faster, and then provide a delightful developer experience through our APIs, SDKs, and embeddable components to help you get up and running faster. But we are set up for A2P (application to person) messaging, not P2P (person to person) messaging. And that’s how Twilio and all the other text messaging APIs work too.
If you have an iPhone and a Mac though, there’s a better option: AppleScript.
AppleScript
Apple created AppleScript over 30 years ago to allow users to automate tasks on their Macs. It’s a powerful tool that can be used to control many of your Mac’s applications, including Messages.
So if you want to send iMessages programmatically, you can use AppleScript to
automate the process. First, you’ll need to create a new file. In this case
we’ve gone with sendMessage.applescript
but you can use a different filename.
In that file, add the following contents:
property buddyList : { "8015551234", "8015554567" }property targetMessage : "Hello from AppleScript!"
tell application "Messages" set targetService to 1st service whose service type = iMessage
repeat with aBuddy in buddyList set targetBuddy to buddy aBuddy of targetService send targetMessage to targetBuddy end repeatend tell
log "Message sent"
You’ll notice that AppleScript uses a natural language syntax, which makes it
easy to read and write. You’ll want to make sure to update the buddyList
and
targetMessage
properties on the first two lines to match your needs. The
buddyList
property should be a list of phone numbers (or email addresses) that
you want to send the message to. The targetMessage
property should be the
message you want to send.
Run the script
Once you’ve saved the script, you can run it from your terminal with this command (assuming you used the same filename as I did):
osascript sendMessage.applescript
Then the script will loop over each of the phone numbers in the buddyList
and send the targetMessage
to each of them. It may take a minute to send all
the messages, but you should see a message in your terminal when it’s done. And
if you have your sound turned on, you should hear the sound of a message being
sent from the Messages app.
While this probably won’t scale to meet the needs of a business, it can be great for personal use. Easily send out party invites, reminders, or just a quick message to a group of friends. If you want to learn more about AppleScript, you can check out the official AppleScript documentation, and if you have any questions about text messaging, feel free to reach out to me on Twitter/X at @dnsbty.