How To Get Notified On macOS When A Website Changes

October 16, 2020 · 2 min read
Man typing on MacBook Pro
Photo by Bram Naus on Unsplash

As software developers, we have powerful tools we sometimes don‘t think of.

A few days ago I was waiting for a press release on local government‘s website. I knew the day, but not the time that the update should come.

So I wrote a BASH script.

At the end of this article, you‘ll know how to create a script that monitors a website and notifies you with a desktop notification.

Checking A Website In A Regular Interval

For this article, we will crawl a random string API every 5 minutes and look for the occurrence of the letter “M”

We'll start with a standard Bash file with an infinite loop:

#!/bin/bash

while [ 1 ];
do
    echo "Last checked on $(date)"
    sleep 300
done

Save this file as monitor.sh and run chmod +x ./monitor.sh to make that script executable. This script will now print the current date and time, every five minutes, until you cancel it.

The next step is to find out if the website changed in a specific way.

For this, we'll count how often the letter “M” is present in the response of the random string api with grep -c:

count=`curl -s "http://www.randomnumberapi.com/api/v1.0/randomstring?min=20&max=20" | grep -c "M"`

We can check if the count is not 0 with an if clause:

if [ "$count" != "0" ] then echo 'Random string contained "M"' exit 0 fi

Putting this together we get:

#!/bin/bash

while [ 1 ];
do
    count=`curl -s "http://www.randomnumberapi.com/api/v1.0/randomstring?min=20&max=20" | grep -c "M"`

    if [ "$count" != "0" ]
    then
       echo 'Random string contained "M"'
       exit 0   
    fi
    echo "Last checked on $(date)"
    sleep 300
done

Getting Desktop Notifications From BASH Scripts on macOS

If we want to get a real macOS notification from our script, we can use AppleScript. AppleScript commands can be executed with the osascript utility from the terminal.

For example, an AppleScript command to display a notification is osascript -e 'display notification "Contained an M" with title "Random String" sound name "Blasso"'. It contains the following components:

  • osascript -e this triggers the osascript utility to evaluate and execute the passed string as AppleScript
  • display notification "Contained an M" with title "Random String" triggers macOS to display a notification with title and body texts.
  • sound name "Blasso" plays the "Blasso" Notification sound. You can find the available sounds in ~/Library/Sounds or /System/Library/Sounds.

Finally, we have the full functioning BASH script:

#!/bin/bash

while [ 1 ];
do
    count=`curl -s "http://www.randomnumberapi.com/api/v1.0/randomstring?min=20&max=20" | grep -c "M"`

    if [ "$count" != "0" ]
    then
        # display notification when string contains letter "M", then sleep for 60 seconds
        echo 'Random string contained an "M", sending notification'
        osascript -e 'display notification "Contained an M" with title "Random String" sound name "Blasso"'
        sleep 60
        # display notification again in case I missed the first one, then exit
        echo 'Sending second notification'
        osascript -e 'display notification "Contained an M" with title "Random String" sound name "Blasso"'
        exit 0   
    fi
    echo "Last checked on $(date)"
    sleep 300
done