Jump to content

Paste and remove from clipboard history


Recommended Posts

Could an optional "Paste and remove from history" hotkey be added to Alfred's Clipboard History feature?

 

Sometimes I work with sensitive information that I want to pop off the clipboard history when pasting. Launchbar has this feature in its clipboard history and I've found it very handy.  If I'm copying some sensitive information around, I just paste with ⌃⌥⌘V (my chosen hotkey) and know the information will be removed from the clipboard history as it's pasted. I'm aware items can be deleted manually from the clipboard history using fn+delete, but clearing out items manually is tedious and much easier to forget to do.

 

In the meantime, is there possibly a workaround possible within Alfred? For example, can a workflow manipulate the clipboard history? If I could connect my ⌃⌥⌘V hotkey to a workflow that pastes and then removes the top entry of the clipboard entry, it could serve as a workaround.

 

Apologies if this feature has already been discussed/requested. I did some searching and didn't find this specific feature request.

Link to comment
Share on other sites

Well, I've found a workaround using a workflow to "Paste and remove from history". Here's what I've got:

 

image.thumb.png.2d87878624f8a7a15aeb678637c47f47.png

 

My hotkey maps to an AppleScript Run Script that waits for modifier keys to be released (since the paste step simulates a ⌘V, doing so while modifier keys are still pressed can make the paste action fail). The next step is another AppleScript Run Script to paste the clipboard contents. A bash script remove the most recent entry from Alfred's clipboard database. And, finally, the last step uses the "Copy to Clipboard" output to paste the new top entry of the clipboard history into the clipboard so it's ready to be pasted.

 

Wait for modifier keys to be released – Run Script – /usr/bin/osascript (AS)

use framework "Foundation"

property NSEventModifierFlagOption : a reference to 524288
property NSEventModifierFlagCommand : a reference to 1048576
property NSEventModifierFlagShift : a reference to 131072
property NSEventModifierFlagControl : a reference to 262144

on anyModifierKeysPressed()
	set currentModifiers to current application's class "NSEvent"'s modifierFlags()
	-- option
	if (currentModifiers div (get NSEventModifierFlagOption) mod 2 is 1) then return true
	-- command
	if (currentModifiers div (get NSEventModifierFlagCommand) mod 2 is 1) then return true
	-- shift
	if (currentModifiers div (get NSEventModifierFlagShift) mod 2 is 1) then return true
	-- control
	if (currentModifiers div (get NSEventModifierFlagControl) mod 2 is 1) then return true
	return false
end anyModifierKeysPressed

-- Wait until modifier keys are released
set anyModifier to anyModifierKeysPressed()
repeat while anyModifier
	set anyModifier to anyModifierKeysPressed()
end repeat

Paste clipboard contents – Run Script – /usr/bin/osascript (AS)

tell application "System Events"
	do shell script "export LANG=\"en_US.UTF-8\"; pbpaste | pbcopy"
	keystroke "v" using {command down}
end tell

Remove most recent entry from clipboard history – Run Script – /bin/bash

sqlite3 ~/Library/Application\ Support/Alfred/Databases/clipboard.alfdb 'delete from clipboard where ts=(select max(ts) from clipboard);'

Update clipboard with new top of clipboard history – Outputs > Copy to Clipboard (Automatically paste to front most app option unchecked)

{clipboard:0}

 

Link to comment
Share on other sites

1 hour ago, bmamlin said:

since the paste step simulates a ⌘V, doing so while modifier keys are still pressed can make the paste action fail

 

This is only true when using AppleScript. If you do it via Cocoa (e.g. have Alfred do it for you), any modifiers you're still holding down are ignored.

 

1 hour ago, bmamlin said:

do shell script "export LANG=\"en_US.UTF-8\"; pbpaste | pbcopy"

 

I don't understand this line. Aren't you just putting the contents of the clipboard back onto the clipboard?

Link to comment
Share on other sites

Thanks for the help, @deanishe!

 

13 hours ago, deanishe said:

This is only true when using AppleScript. If you do it via Cocoa (e.g. have Alfred do it for you), any modifiers you're still holding down are ignored.

 

Okay. I thought this was a problem when trying to paste within Citrix Viewer, since it was bringing up a menu instead of pasting. It turns out avoiding the option key (alt key for Windoze in Citrix) is sufficient – i.e., I changed my shortcut key to ⇧⌘V.

 

13 hours ago, deanishe said:

I don't understand this line. Aren't you just putting the contents of the clipboard back onto the clipboard?

 

I copied that from another workflow. I think it was a way of keeping UTF-8 text while removing any associated formatting. You're right, it's unnecessary here.

 

You've helped me simplify the workflow, but I'm still stuck here:

 

image.thumb.png.9fdfd0d9919c823d9706d6468d9912c6.png

 

So, I can use the paste as provided in the "Clipboard - Getting Started" example and then remove the top entry from the clipboard history, but, despite the item being removed from the history, it's still sitting in the active clipboard instead of being replaced with the next item in the clipboard history. If I try adding the final step of copying the top entry of the clipboard history – {clipboard:0} – to the clipboard, the workflow pastes that to the active application instead of the original item.

 

For example:

  • Type "foo", select it and cut it.
  • Type "bar", select it and cut it.
  • Verify clipboard history shows "bar" and "foo" as 1st and 2nd entries, respectively.
  • Press hotkey (⇧⌘V)
  • "foo" is pasted, is shown as the top item in the clipboard history, is the active clipboard contents. "bar" is gone from the clipboard history.

In the last step, I was expecting/hoping it would paste "bar". If I leave off the last step in the workflow, it works as expected, but the active clipboard still contains "bar".

 

I've tried re-ordering steps, but so far I haven't found a way to paste the current clipboard contents and then reload the active clipboard with a different item from the clipboard history.

 

Any ideas what I'm doing wrong?

Link to comment
Share on other sites

17 minutes ago, bmamlin said:

Any ideas what I'm doing wrong?

 

Hard to say without having the actual workflow to look at (hint, hint). So, I'm guessing because I'm not going to try and re-implement your workflow for myself to check.

 

It sounds like you’ve got the order wrong. You need to paste the newest entry on the clipboard history, and wait for a fraction of a second for the paste to complete before you start messing with Alfred's history.


So, you want to paste {clipboard:0} (or just use a Dispatch Key Combo to simulate ⌘V), add a short delay, and only then run your SQL to clean up the history.

 

Edited by deanishe
Link to comment
Share on other sites

Awesome! That worked. Thanks so much, @deanishe! Locally, it works with a 0.1 second delay, but a 0.5 second delay seems to work more reliably when working in a Citrix Viewer.

 

image.thumb.png.d8358b6cb16c953e578b10c97f5e295f.png

 

You can download the Paste and remove workflow from here. :) 

 

Update: I changed the first step to not send an argument and replaced the second step with a simulated ⌘V. I did this because using Alfred's built-in Copy to Clipboard was removing/altering some formatting from clipboard content (underscoring & bulleted lists) in my particular situation of pasting from a Mac into a Citrix Windoze environment.

Edited by bmamlin
Link to comment
Share on other sites

7 hours ago, bmamlin said:

replaced the second step with an AppleScript script to paste the clipboard by simulating ⌘V

 

As noted above, it's generally better to use Alfred to simulate keypresses (or import the necessary Cocoa gubbins into your AppleScript) because it's not affected by any modifier keys you still have your fingers on.

Link to comment
Share on other sites

1 hour ago, deanishe said:

As noted above, it's generally better to use Alfred to simulate keypresses (or import the necessary Cocoa gubbins into your AppleScript) because it's not affected by any modifier keys you still have your fingers on.

 

Okay. Fair enough (I just haven't discovered all the amazing things workflows can do :) ). I updated the workflow to have Alfred simulate the ⌘V keypress.

 

Now my "Paste and remove" workflow looks much sleeker:

image.thumb.png.fa32d5f8860d2052d1728e9ecaac3d6d.png

(download link)

 

The only "messy" bit is the bash one-liner to remove the most recent item from the clipboard history:

sqlite3 ~/Library/Application\ Support/Alfred/Databases/clipboard.alfdb 'delete from clipboard where ts=(select max(ts) from clipboard);'

Thanks for your help, @deanishe! This turned out better than I expected. Now I don't need Launchbar's clipboard history any longer and I've got a decent workaround until Alfred adds a native "Paste and remove" option.

Edited by bmamlin
Link to comment
Share on other sites

  • 2 years later...
Hi All

I'm having an issue, with the 'replace text' in clipboard history. Now, I can replace the text ok and put it in the first row in clipboard history, but the problem is that text before replaced was copied as well and in the 2nd item in clipboard history (even those I chose Selection in Mac OS instead of  Mac OS Clipboard contents . I use the code: 

sqlite3 ~/Library/Application\ Support/Alfred/Databases/clipboard.alfdb 'delete from clipboard where ts=(select max(ts) from clipboard);'

to delete it, but they delete both the 1st and 2nd item in the list, while what I want is to keep the replaced text only in clipboard (mean the 1st item) and delete the second item, please help me out !!!

 

SCR-20220924-ly2.png

Edited by Walter9981
Link to comment
Share on other sites

Welcome @Walter9981,

 

3 hours ago, Walter9981 said:

even those I chose Selection in Mac OS

 

There isn’t a universal way of getting the selection which works for every app, so Alfred simulates a copy to get the selection.

 

3 hours ago, Walter9981 said:

but they delete both the 1st and 2nd item in the list

 

You are deleting by timestamp and both entries have the same one because the copy happens too fast. Add a delay.

Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...