Send messages inside a loop


#1

I have a situation where I’d like to send a message to multiple people without it being a “group” MMS message. Scriptable seems to be the ideal solution for this. I have an array with numbers and a for loop that goes through the array, creating a new message with each loop.

It works for the first one, and brings up the message draft with the correct contact for me to send. Then the loop seems to break after that. This may be an issue with promises, etc. that I don’t understand. Any ideas?

for (let n of numbers) {
    let msg = new Message();
    msg.recipients = [n]
    msg.body = "Test message";
    msg.send();
}

I don’t mind hitting “send” for each one, I’d just like to save the work of creating all the messages.

Thanks!


#2

This does not work as I would expect, either. send is documented to run synchronously, but seems to be asynchronous, so your loop runs to completion before the UI for the first message is displayed. @simonbs?


#3

This is actually by design but I acknowledge that it is not a desirable design so I have changed it for the next update. In the next update, Mail.send() and Message.send() will return a promise, letting you halt execution of the script until the message or mail have been sent.


#4

Unless you specifically want to use Scriptable to accomplish this, Shortcuts can do this no problem. And if it’s text-only, you won’t even have to interact with the message or send button. Adding pictures or other media will nessecitate your hitting the send button.


#5

I tried this initially but kept running into an issue where Shortcuts was attaching a text file with the contents of the message rather than putting it in the body. I’ll have to try again, if what you say is true, that sounds good.

I could then use Scriptable to extract the correct phone numbers from the contacts (unless that can be done in Shortcuts too).


#6

The message action is very finicky, if you need to do some things before the message block, use a nothing action just before hand. I’ve included a picture of one of my Shortcuts that does the exact same thing you’re describing.


#7

That’s cool! Now I just have to figure out how to filter contacts based on organizationName, and then send the text to only the “mobile” number. That was easy using Scriptable.


#8

Message.send now returns a Promise, so something like the below can be used to send multiple messages sequentially to an array of “To” addresses:

const tos = ["123", "456"];
tos.reduce((acc, cur) => {
  return acc.then(() => {
    const msg = new Message();
    msg.recipients = [cur];
    msg.body = "...";
    return msg.send();
  });
}, Promise.resolve());

#9

Hello Greggy, thanks for mentioning the nothing block, that worked for me. I have several message blocks in a row to send the same message to different people and found that txt item it added annoying, that Shortcuts inserted for some reason. This fixed it. Now that it works I’ll have to see about making a loop. Haven’t figured that out as far as how to get the contacts into the loop.