Losing the Human touch…

With the explosion of social media, many of us would have more than a handful of people to which we have to (at least) occasionally “Stay in touch” with. to make matters worse, there are individuals in everybody’s friend lists who have a really strong fear of rejections. We are constantly seeking way out of the mundane task of keeping in touch with everyone on Facebook (at least for me). So the best thing to do… IMHO… have an annual routine to wish them a “Happy Birthday”.

But unfortunately due to a hectic working schedule, occasionally I have missed out on wishing some friends (actually a lot) on their birthday, So the next best thing to do. Automate the process…

Once again, I am going to show you Apple script’s prowess in the mac scripting environment. In the past, I have relied solely on Curl to authenticate the shell script into the secure Facebook site, retrieve the list of birthday individuals and ultimately wish them on their special day. But unfortunately, Facebook has added additional security measure to prevent scripts from delegating the task off their human counterpart. So this call for a rework of the script.

Instead of trying to circumvent the security measure every time Facebook decides to change the security measure to prevent the bot, I have decided to instead to let the bot control the actual Safari browser using my existing authenticated credential (cookie/token).

Before we start, the Safari bundled with El Capitan has remote Apple Script’s Javascript capability turned off. Simply click on “Develop” from the menu bar and turn on “Allow Javascript from Apple Events”.

Screen Shot 2016-07-26 at 11.39.13 PM

Next up, we have a couple of task we need to automate.

1. Open Safari and Navigate to “https://www.facebook.com/events/birthdays”, of course I am assuming your Facebook has been logged in (Why shouldn’t it be ?).

2. Count the number of Birthday Wishes recipients. Loop through the list of recipients to extract the “Name” and append and “Happy Birthday” text to it, ultimately we are going to populate the Textboxes.

3. After checking through the Facebook code, I got lazy of tracing how the inline submit actually works on the Facebook page, so instead of mimicking the submit code. The Script/bot is going to use “Tab” and “Return” keystrokes to submit the Birthday Wishes.

To realize the 3 steps above in code, it would look as follows. ( Please follow through the comments)Screen Shot 2016-07-27 at 2.53.14 AM

Of course there are a couple of helper functions that I have utilize here not in the code snippets, they are pretty simple & straight forward.

1) goToWebPage will be opening a new Window on Safari and navigate to the desire URL. I have employ a “Hack” method to detect if the webpage has been fully loaded by checking on the closing <HTML> tag.

Screen Shot 2016-07-27 at 2.44.55 AM

2) divCount will count the number of element inside a given class, which correspond to the number of Birthday boy/girl of the day.

Screen Shot 2016-07-27 at 2.45.14 AM

3) getInputByClass will extract the source text of the given class, ultimately we are going to extract the person’s name by looking for the <a href> using extractText.

Screen Shot 2016-07-27 at 2.45.46 AM

4) Lastly we have inputByName which will allow us to change the value of the Element (textField).

Screen Shot 2016-07-27 at 2.46.25 AM

The last step would be to add this to your daily cron job !!

Screen Shot 2016-07-27 at 3.06.51 AM

The Final working script could be downloaded from >>Code<<

Leave A Comment?