Skip to content

Instantly share code, notes, and snippets.

@hepcat72
Last active December 8, 2023 22:36
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hepcat72/6b7abd9000e8b108ecdb76e12da1257e to your computer and use it in GitHub Desktop.
Save hepcat72/6b7abd9000e8b108ecdb76e12da1257e to your computer and use it in GitHub Desktop.
Send SMS or iMessages via AppleScript
#!/usr/bin/env osascript
-- Version 2, now with attachments!
-- Note, the first couple times you run this script, the Messages app may prompt you to approve a couple things, one of which will be the ability to send SMS messages through your phone
-- Run via osascript on the command line like this:
-- osascript sendText.scpt --to ########## "this is" "a text message" --attachment "/path/to/image.png" "sent via applescript"
-- Where ########## is the phone number to send to
-- If you leave out `--to ##########`, it defaults to the value of an environment variable named MYPHONE in your .bashrc file (add the line "export MYPHONE=##########" to ~/.bashrc, entering your default phone number in place of ##########)
-- The 3 quoted strings will appear on separate lines and the image will be inserted between the lines "a text message" and "sent via applescript"
on run argv
set defaultMessage to "applescript test"
set errors to {}
if (count of argv) is equal to 0 then
set phoneNumber to my getMyPhoneNumber()
set argv to {"--to", phoneNumber, defaultMessage, "--attachment", "/Users/robleach/Pictures/Icons/rob_face.jpg"}
end if
if (item 1 of argv) is equal to "--to" then
if (count of argv) is less than 2 then
return "ERROR: --to requires an argument"
else
set phoneNumber to item 2 of argv
if (count of argv) is equal to 2 then
set messageParts to {defaultMessage}
else
set messageParts to my getMessageParts((items 3 thru (count of argv) of argv))
end if
end if
else
set phoneNumber to my getMyPhoneNumber()
set messageParts to my getMessageParts(argv)
end if
tell application "Messages"
set smsMessageType to id of 1st account whose service type = SMS
set smsRecipient to participant phoneNumber of account id smsMessageType
set iMessageType to (id of 1st account whose service type = iMessage)
set iMessageRecipient to participant phoneNumber of account id iMessageType
repeat with messagePart in messageParts
try
send messagePart to smsRecipient
on error
try
send messagePart to iMessageRecipient
on error errmsg
set the end of errors to errmsg
end try
end try
end repeat
end tell
if (count of errors) is greater than 0 then
set the beginning of errors to (count of errors) & " of " & (count of messageParts) & " message parts failed to send with the following error(s):"
return my join(linefeed, errors)
end if
return "Message sent"
end run
on join(myDelimiter, myList)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to myDelimiter
set joinedString to myList as text
set AppleScript's text item delimiters to astid
return joinedString
end join
on getMyPhoneNumber()
set myphone to (do shell script ". ~/.bashrc && echo $MYPHONE")
if myphone is equal to "" then
error "You must add the line:" & linefeed & "export MYPHONE=##########" & linefeed & "to your .bashrc file."
end if
return myphone
end getMyPhoneNumber
on getMessageParts(args)
set messageParts to {}
set getAttachment to false
set lastWasText to false
repeat with anArg in args
set stringArg to anArg as string
if getAttachment then
set the end of messageParts to POSIX file stringArg
set getAttachment to false
set lastWasText to false
else
if stringArg is equal to "--attachment" then
set getAttachment to true
set lastWasText to false
else
if lastWasText then
set lastText to item (count of messageParts) of messageParts as string
set newLast to my join(linefeed, {lastText, stringArg}) as string
set item (count of messageParts) of messageParts to newLast
else
set the end of messageParts to stringArg
end if
set lastWasText to true
end if
end if
end repeat
return messageParts
end getMessageParts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment