Send iMessage AND Text (SMS) from the Command Line

This code sends a text message from the command line


on run {targetBuddyPhone, targetMessage}
  tell application "Messages"
    set targetService to 1st service whose service type = SMS 
    set targetBuddy to buddy targetBuddyPhone of targetService
    send targetMessage to targetBuddy
  end tell
end run 

And this code sends an iMessage from the command line


on run {targetBuddyPhone, targetMessage}
  tell application "Messages"
    set targetService to 1st service whose service type = iMessage
    set targetBuddy to buddy targetBuddyPhone of targetService
    send targetMessage to targetBuddy
  end tell
end run 


I’m looking for a solution to combine this code into a single snippet that will try to send an iMessage if the recipient has iMessages, but if they do not, then the code will send a SMS message.

Replies


I am having the same exact issue!!!

iMessage isn't throwing an error even though the messages are not delivered to SMS users. (so the on error block isn't ever running...

I wish that the send function returned something once it confirms the message was sent.

Does anyone have any ideas???? - I really need that on error catch block to run!



Code Block
tell application "Messages"
set phoneNumber to "+15555555555"
set messageToSend to "This is a test!"
try
set iMessageService to (1st account whose service type = iMessage)
set iMessageBuddy to participant phoneNumber of iMessageService
send messageToSend to iMessageBuddy
log ("sent as iMessage to: " & phoneNumber)
on error
try
set iMessageService to (1st account whose service type = SMS)
set iMessageBuddy to participant phoneNumber of iMessageService
send messageToSend to iMessageBuddy
log ("sent as SMS to: " & phoneNumber)
on error
log ("ERROR: COULD NOT SEND TO: " & phoneNumber)
end try
end try
end tell

I too have the same exact need: If the recipient has an iPhone, send an iMessage. Else send SMS.

Did you resolve the issue? What was your solution?