Automatically Clearing the Clipboard in MacOS Monterey

Your clipboard contains a lot of sensitive info – passwords from password managers being the most obvious.

Any app or website can read from the clipboard, so having this data just hanging around is a bit of a problem – a password could be read by some unintended app or website and surreptitiously recorded.

New MacOS features like handoff sync your clipboard across devices adding to the amount of potential exposure.

Clearing the Clipboard Periodically on MacOS

The solution to this is to have the clipboard be automatically cleared of data periodically. You shouldn’t be relying on the clipboard as a place to store data, for most users copy and paste actions occur within a minute of each other.

So, clearing the clipboard after 30 seconds should give plenty of time for you to paste the data, and then it can be wiped so that it can’t be leaked to another application later on.

Existing Apps Which Automatically Clear the Clipboard

While there are existing apps which can clear the clipboard periodically, it doesn’t really solve the trust issue.

If I don’t trust random developers to behave responsibly with my clipboard data, why introduce another app from a random developer and grant it access to manage my clipboard data?

Of course, sometimes you don’t have much choice – you need a tool to do a job and you can’t write your own bespoke software to do everything – but in this case you can write a simple tool to do the job using MacOS’ built in automation tools.

Automatically Clearing the Clipboard with Apple Automator

Apple’s Automator can be used to build a simple app which will periodically clear your clipboard. Here’s how to do it.

The first step is to create a directory for your clipboard clearing App from the terminal with the following command:

mkdir -p ~/Applications/clearClipboard

While still in the terminal, create a Bash script which will do the actual clearing. The below command will create the file using the nano text editor and open it for editing:

nano ~/Applications/clearClipboard/clearClipboard.sh

Paste the following script into the nano text editor:

#!/bin/bash

# An automator app should be used to call this script which will then execute infinitely
# The automator app can then be set to launch automatically

echo "Running clearClipboard"

# Init vars
currentrun=$(date +%s)
hash=""

# Infinite loop
while : 
do
    # Repeat every 5 seconds
    sleep 5

    # It would be nice to catch the shutdown signal here to quit the script when shutting down, but apparently it's not possible
    
    # Read the clipboard on MacOS
    clipboardcontents="$(pbpaste)" 

    # $() assigns result of command to variable
    lasthash=$hash
    hash="$(echo "$clipboardcontents" | shasum)" 
    # lastchange=$currentrun
    currentrun=$(date +%s)

    # If clipboard populated
    if [ -n "$clipboardcontents" ]; then
        echo "Clipboard populated"
        # If clipboard contents have not changed 
        if [ "$hash" = "$lasthash" ]; then
            echo "Clipboard contents have not changed"
            timesincelastchange=$(( $currentrun - $lastchange ))
            # and it's been 30s since they changed
            if [ "$timesincelastchange" -gt 30 ]; then
                echo "...and it's been 30s since they changed"
                echo "Clearing clipboard"
                # Clear clipboard
                pbcopy </dev/null
            fi
        else 
            echo "Clipboard contents changed"
            # If contents have changed do not clear it, wait for the next round
            lastchange=$(date +%s)
        fi
    fi

done

The comments should explain what the above script is doing. The script is set to clear the clipboard 30 seconds (give or take) after something has been copied (the contents of the clipboard have changed)

Press CRTL + X to close the nano text editor and press Y to save.

Open the Automator App (It’s got a little robot icon) and create a new Application. Configure it to match the following screenshot:

You can search the list of actions using the search field at the top – in this case simply typing shell should reveal the Run Shell Script action. Drag it into the main work area and copy and paste the below text into it:

sh ~/Applications/clearClipboard/clearClipboard.sh

This line simply tells automator to launch the script we created previously when the application is run.

Export the Automator Application by going to File -> Export. Name the Application clearClipboard and save it to the same directory the above Bash script was saved in.

The final step is to make your newly created Automator App launch automatically when you start your computer or log in.

System Preferences -> Users & Groups -> Click your Username from the list -> Login Items

…and add the clearClipboard Automator application to the list. Once it’s added to the list check the Hide option next to it.

While the app is running your clipboard will be automatically cleared 30 seconds after you’ve cut or copied something.

Remember to be careful from now on that you don’t cut anything important as you might lose it when the clipboard is automatically cleared.

You’ll now the app is running because Automator will show a spinning cog icon in the menu bar:

Clicking on it will show which Automator actions are running:

As the Bash script the App uses is an infinite loop which never resolves, it will always show 0% completed – and that’s OK.

And that’s it! Your clipboard data will now be blown away after you’re done with it and less likely to leak to programs and websites you might not want to expose that info to – all done with the built in MacOS automation tools.

Leave a Reply