Clear Clipboard Before Specific Action

Hello!

How to clear all clipboard before specific action?

There are actions for deleting current and past clipboard but no action to delete all clipboard.

Thanks in advance.

You cannot actually “clear” the current clipboard on the Mac. The clipboard is always set to something, the only thing you can do is replace it.

You can set the clipboard to something (eg “”), and then you can delete all the past clipboards if desired:

  • Set Clipboard to Text “”
  • Repeat 200 times
  • Delete Past Clipboard 1

But that’s about as close as you can get. I’m not sure why you want to do this though.

So the only way i see is to open clipboard history swticher and press cmd A, then remove all

Is there a way to perform this action in background?

I think Peter already has shown you the solution (Repeat 200 times: Delete past clipboard 1).

IIRC, the Delete Past Clipboard action does the same as deleting a clipboard in the Clipboard History window.

1 Like

Okay, i will try it. Thanks a lot

In my case, I'm using KM to go through some fields in a structured database accessed through Citrix. I'm sequentially copying-and-pasting information. I can't depend that there will be anything in a given field, so clearing the clipboard between copies-pastes will prevent me pasting the same (old) information into a (new) field, when I'd rather paste NULL (since there wasn't anything to replace the clipboard on the previous copy)

There are several simple solutions to this:

  1. Set the Clipboard to something that will NOT possibly be what you want to copy, like "[EMPTY]".
    • Do the Copy
    • IF the Clipboard = "[EMPTY]", paste "NULL"
    • OR, even better, just set the Clipboard to "NULL" before each Copy.
      .
  2. Use the CLIPBOARDSEED function
    • Set Variable CBSeedBefore to CLIPBOARDSEED()
    • Do the Copy
    • IF CBSeedBefore is CLIPBOARDSEED(), then Paste "NULL"
      .
  3. Use the Copy action with a short (≤ 1 sec) timeout
    • Set the Action to NOT cancel the macro if it fails
    • IF the ActionResult token is not "OK", Paste "NULL"

These should give you some ideas.

Questions?

2 Likes

Thanks for the suggestions. Yes, the "NULL" system works great for me

How do I set a clipboard to NULL?

My specific clipboard carries with it the new and some older clips :frowning:

/

with best regards,

Omar KN

Stockholm, Sweden

Hello!

There is another way to clear variables by deleting them completely.
The macro/Applescript action provided below allows to remove all variables except ones that are added to the "KeepList"

Remove variables.kmmacros (1.8 KB)

property keepList : items 1 thru -2 of {¬
	"Test1", ¬
	""}


tell application "Keyboard Maestro Engine"
	repeat with theVariable in (get variables)
		set varName to theVariable's name
		if varName is not in keepList then
			setvariable varName to "%Delete%"
			try
				delete varName
			end try
		end if
	end repeat
end tell

Hmm, how to adapt this script if the clipboard only has text in it?

/okn

This should work:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

set thePasteboard to current application's NSPasteboard's generalPasteboard()
thePasteboard's clearContents()

According to my Clipboard Viewer.app, it is null

image

2 Likes

What about:

set the clipboard to missing value

What does that show up as (I'm hoping it won't) on your clipboard viewer ? Both of these scripts appear to make ⟨⌘⟩+⟨V⟩ simply return a beep, as if the clipboard is blank. But I don't have a clipboard viewer like your one—only Keyboard Maestro's, which just shows the most recent non-empty data and everything before it.

1 Like

Hey @CJK,

Some goop. See for yourself:

https://langui.net/clipboard-viewer/

Whatever it is doesn't paste as text, so the missing value trick is likely just fine. Nevertheless I'll probably use the AppleScriptObjC code if/when I need to do such a thing.

-Chris

1 Like

Thank you for this! This app is brilliant. I've just spent about 30 minutes setting my clipboard to all sorts of things.

I see now that the missing value class gets stored on the clipboard as its 4-byte character code 'msng'. In fact, even the class «class ­­­­» is still composed of four NUL characters and takes up space.

I did find out, however, that you can omit the direct parameter and do

set the clipboard to

which stores 0 bytes but still somehow has two pasteboard flavours associated with it. So it seems like the Objective-C method is the only way to get a truly clean, empty clipboard.

I would not advise clearing the clipboard. Instead, set it to something innocuous like “EMPTY”

The reason for this is clearing the clipboard breaks an invariant on the Mac that the clipboard always contains some sort of value (except when you first start up) since normally the only way to change it is by copying something new, and you cannot copy when nothing is selected.