Thursday, February 15, 2024

Automating WhatsApp Using AppleScript

Edit: added a windows version, and all source code is available here:

I was looking for a way to send a message to a distribution list. This is a feature that in its simplest form is part of the WhatsApp mobile app, but its a bit cumbersome, and what's more - it does not allow to customize messages for each recipient.

There are third party tools, and WhatsApp business API, but I am just a simple guy who needs to send his friends some personal messages (think about invitations to a party or greetings for the holidays), so I was trying all kind of ways, and then found a reasonable solution using JavaScript in Mac Script Editor.

The idea is to create a data file with a list of recipients, at the minimum it is something like:

"recipients": [
{"name": "John", "phone": "12123334444"},
{"name": "Marry", "phone": "12123335555"},
{"name": "Anna", "phone": "12123336666"},

With a first name and phone number.

Save the list to a file named "data.json" in a new empty folder.

Create a message template file, like this:

Hello FNAME,
I would love to invite you to my birthday party on Saturday night at 8PM.
Lots of food and beer - don't eat too much before the party...
see ya!

Save this to a file called "template.txt" in that same folder as the data.json file.

Then create this script (since I use Hebrew, I needed to read the file as UTF-8):


const readFileEnc = function (path, encoding) {

    !encoding && (encoding = $.NSUTF8StringEncoding)

    const fm = $.NSFileManager.defaultManager

    const data = fm.contentsAtPath(path)

    const str = $.NSString.alloc.initWithDataEncoding(data, encoding)

    return ObjC.unwrap(str)


const sendOneMessage = function (chromeapp, window, url) {

 var ic = window.tabs.length;

 window.tabs.push(new chrome.Tab());

  window.tabs[1].url = url;

  while (window.tabs.length > ic) {




var app = Application("Script Editor");

app.includeStandardAdditions = true;

var folder = app.chooseFolder({ withPrompt: 'Please select the data folder' })

var file = folder + "/data.json";

var dataStr = readFileEnc(file)

var data = JSON.parse(dataStr);

var chrome = Application('Google Chrome');

var window = chrome.Window().make();

var countDoc = 0;

for (var i = 0 ; i < data.recipients.length ; i++) {

var tfile = folder + "/template.txt";

var text = readFileEnc(tfile);

text = encodeURIComponent(text.replace(/FNAME/g, data.recipients[i].name));

var dest_url = "" + data.recipients[i].phone + "&text=" + text + "&type=phone_number";

sendOneMessage(chrome, window, dest_url);





Before you run this script, make sure you logged in to WhatsApp Web in Chrome. The script will open a new window in Chrome, and then a tab with WhatsApp Web setup with the phone number and text ready for you, click the send button manually, and close the tab to get the next record opened. 

If you only have 30-40 recipients, this will be quite a smooth and fulfilling experience.


No comments:

Post a Comment