Thank you again, Viking OS X. Your code worked perfectly. But for my larger purpose, there is a "bug". Please allow me to first explain what I am trying to do:
I am a teacher at a public school. I communicate with caregivers (parents, grandparents) of my students via text messages. One of them may tell me "XYZ will be out today. Dentist appointment." Or I may say "XYZ did great to day, he ...". But once a week, I give a general update to every caregiver. I keep it personal - address them by their name, address their child by name and proper gender etc. Also, many of my parents do not speak or read English, and so I first visit a website like google translate to translate into their preferred language.
My motivation: A lot of bad behavior by students goes away, and motivating them to do their homework and study harder becomes easier when caregivers and teachers simply communicate and coordinate more. Non-English speakers are so relieved I communicate in their language - because it finally allows them to be aware of and participate in a key part of their child's development.
It is not sustainable though, because it takes way too much time to send individual messages to each caregiver of each class.
For it to be sustainable, I am automating things. Hence this effort. Please see "Additional Text" for the script. It works just fine - and you can see the part where you helped me. It addresses key hurdles - save time, overcome the language barrier, yet keep it personal.
If it were just me who were going to use it, I am done. However, several other teachers and administrators in my district and in other school districts want to use it, but I don't want them to be editing anything in the script to use it. Worst case I will give them the script as is and tell them to be careful, but I'd like to make it nicer.
Here are the challenges that perhaps you can guide me:
- Currently, I am composing the message to send inside the script. You will notice that I mix regular text and variables so that the message is personal. I want them to compose in an app like pages. I used your code, but then it does not recognize that it contains variables. Also, I want them to choose the pages document just once - before that repeat loop - and then just update the messageToSend with the personalized message to send to each caregiver. I tried to by-pass the choose and open with my own code, but it does not work, so I commented it out.
- The Caregiver's contact info, and student info is in a numbers spreadsheet. Currently, I have the filename hardwired into the code. I tried to do a choose, but then I was getting errors in referencing the document. Each class has its own set of caregivers, so hardwiring the name means the teachers/administrators have to manually manage which file gets called "Contacts.numbers". Ideally, I don't want to hard wire the name of the sheet or table either - there will be only one sheet in the spreadsheet and one table (can there even be more than one table).
- As an aside, how do I reference the folder that the script is located in? Path to me gives me the script's filename as well, and I want just path to the folder. Path to container of me and other variations I tried gives me errors.
- I want to send an iMessage to caregivers who have an iPhone, and SMS to others. Because, if I send to all via SMS, then for those with an iPhone, messages that I manually send and messages that I send via this script are treated as different senders even when the sending phone number is the same. Besides the confusion, it splits apart the communication history under different senders. Ideally, the Messages application would take care of choosing iMessage to SMS - because, how do I know whether the recipient has an iPhone or not? I came up with the code in the script, but I am told it will fail to send to non-iPhone users because the on error part never gets executed. Everyone I am close enough to test with uses iPhone, and I don't want to test this with the teachers - some of whom do use Android.
Other notes:
- I could not upload contacts.numbers, so here is a screenshot with the mobile numbers and email addresses changed.
- So you can execute the code, including translation, I have kept the actual key in the translate API call. I will change it when we are done.
- Mr. Banana is what my students call me.
Thank you in advance. I opened up to you because I can tell from the detail in your code that you are fully vested in helping people, and for that, I am grateful.

See "Additional Text" for Actual Script
[Image Edited by Moderator to Remove Personal Information]