The friction to open Messages and respond to text messages I receive is enough that I often forget to respond. Normally this isn’t a big deal, but it’s been a frustration to my wife because I regularly forgot to respond to her texts (and I don’t blame her!).

Get the AppleScript

I wrote up a quick AppleScript today that I embedded in a quick Raycast Script. It requires text content for the message and then takes that input and makes it the textMessage.

on run argv
  tell application "Messages"
    set targetBuddy to "(XXX) XXX-XXXX"
    set targetService to id of 1st account whose service type = iMessage
    set textMessage to ( item 1 of argv )
    set theBuddy to participant targetBuddy of account id targetService
    send textMessage to theBuddy
  end tell
  log "Message sent"
end run

It took me a big to figure out how to pass an argument from Raycast to AppleScript. I’m definitely not a proficient AppleScript user, so if you know more about AppleScript, I’m sure this is fairly pedestrian. Here is what each line does if you’re interested:

  • Line 1. ‘on run argv’ processes arguments passed into the script.
  • Lines 2-8. Talks to the Messages app and sets the indended recipient’s phone number to a variable called ‘targetBuddy’. Because Messages can be wonky with AppleScript sometimes, line 4 ensures we’re talking about the right application. Line 5 sets the content of the text message to the argument passed into the script. Line 6 sets the recipient of the message to the variable ‘targetBuddy’. Line 7 sends the message and line 8 closes out the Messages tell.
  • Line 9. I log out a message to confirm the message was sent.
  • Line 10. Closes the argv process.

Because it is a simple AppleScript, you can use it in Automator, Shortcuts, Alfred, etc. I use it with the free Spotlight replacement, Raycast.

Get the Raycast Script

If you want the full Raycast Script, you can find it below. You’ll obviously need to replace the (XXX) XXX-XXXX with your desired phone number.

Note: There are scripts around that show you how to use AppleScript to dynamically search for names in the Contacts app. You could pass that into Raycast and then process that as another variable.

#!/usr/bin/osascript

# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title Text Person
# @raycast.mode silent

# Optional parameters:
# @raycast.icon 💬
# @raycast.argument1 { "type": "text", "placeholder": "Enter text message" }

# Documentation:
# @raycast.description Send Person a Text Message
# @raycast.author Chris Pennington
# @raycast.authorURL @cpenned on Twitter

on run argv
  tell application "Messages"
    set targetBuddy to "(XXX) XXX-XXXX"
    set targetService to id of 1st account whose service type = iMessage
    set textMessage to ( item 1 of argv )
    set theBuddy to participant targetBuddy of account id targetService
    send textMessage to theBuddy
  end tell
  log "Message sent"
end run

To use this in Raycast, save the text above to a file and open Raycast Preferences. Click on Extensions and select Script Commands. Click Add Directories on the right and open the directory where you saved the script. I recorded a hotkey in the Raycast Preferences pane to make running the script easy. Hope this helps!