8

I tab a lot between tasks and would like to make sure that I do not accidentally paste something where it does not belong. Thus, is there a feature or an app that allows me to auto-clear my clipboard after n seconds or after having pasted n times?

3
  • 2
    You can use an AppleScrip command set the clipboard to "" or from the Command Line, printf "" | pbcopy used in this manner will copy nothing thus overriding the general pasteboard (clipboard). Either can be used in Automator to make an App or Service, the latter of which could have a keyboard shortcut assigned. Or in an AppleScript App you could put in the Dock to click when you'd like, etc. Jul 7, 2016 at 10:48
  • @user3439894 this will only add empty space as the last item in the clipboard history. It is still possible to observe previously copied items. Mar 31, 2018 at 17:53
  • 1
    @Sarge Borsch, As I read the OP the goal was not to accidentally paste what was on the Clipboard to the wrong place and thus clear is to avoid an accidental paste, not hide all history, etc. So I really fail to see the point you're trying to make in your comment to me. Furthermore, I posted it as a comment and not an answer for a reason! Mar 31, 2018 at 19:17

3 Answers 3

8

The script below clears the clipboard every S seconds:

#!/bin/sh
# pbclear [seconds]

S=${1:-10}

while true
do
    pbcopy < /dev/null
    sleep $S
done

Save it to a file named pbclear, set executable permissions with chmod +x pbclear, and put it somewhere in your path, for instance, /usr/local/bin. Then run it in the background with pbclear 60 & to clear the clipboard every 60 seconds.

2

You can use:

pbcopy < /dev/null
0

Here is the version that will check for clipboard changes every 10 seconds. If there is a change it will clear the clipboard after 10 minutes, but only if there were no new changes in the last 10 minutes.

The reason for this is that a simple solution from https://apple.stackexchange.com/a/331203/41513 may clear the clipboard between you copy something and try to paste it.

#!/bin/bash

clipboard=""
counter=0

while true; do
  # Get the current clipboard content
  new_clipboard=$(pbpaste)

  # Compare the new clipboard content with the previous one
  if [ "$new_clipboard" != "$clipboard" ]; then
    # Clipboard content has changed
    clipboard="$new_clipboard"
    counter=0
  else
    # Clipboard content has not changed
    counter=$((counter+10))

    # If 10 minutes have passed without any changes, clear the clipboard
    if [ $counter -ge 600 ]; then
      echo "Clipboard cleared"
      pbcopy < /dev/null
      clipboard=""
      counter=0
    fi
  fi

  # Wait for 10 seconds before checking the clipboard again
  sleep 10
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .