Jump to content

Workflow/Shortcut to clear clipboard


Recommended Posts

Posted

Hi everyone, is there a way to clear the clipboard history without actually having to type "clear" inside alfred everytime. Thinking of having a shortcut or button on the touchbar to just tap and clear the clipboard history? Any API/Applescript or something else I can do to accomplish this?

 

thanks

Posted (edited)
17 hours ago, andy4222 said:

Any API/Applescript or something else I can do to accomplish this?

 

The following snippets will clear the current clipboard:

 

In AppleScript:

use framework "AppKit"
current application's NSPasteboard's generalPasteboard's clearContents()

 

In JXA:

ObjC.import('AppKit');
$.NSPasteboard.generalPasteboard.clearContents;

 

Alfred doesn't have an API method to clear its clipboard history, but you can use the following scripts to tell Alfred to search for "clear", then simulate ↩:

 

In AppleScript:

tell application id "com.runningwithcrayons.Alfred" to search "clear"
tell application "System Events" to key code 36

 

In JXA:

Application('com.runningwithcrayons.Alfred').search('clear');
Application('System Events').keyCode(36);

 

Edited by deanishe
  • 6 months later...
Posted

This is something I really want to do - I have a workflow that unmounts drives, locks screen and (tries to) clear clipboard with Apple Script.  The Alfred command line and Clear command appear, but the Enter key code (36) apparently does not get issued.  As best I can tell, the window focus is not on the Alfred window for the key code to be effective.  Any additional ideas?

Posted
1 minute ago, alnetloc said:

As best I can tell, the window focus is not on the Alfred window for the key code to be effective.

 

Right. When you're firing commands and keypresses at an application, you often need to add a delay to between them to give the application time to react to the previous one.

 

In AppleScript, add delay 0.1 or delay 0.2 before the key code 36 to give Alfred’s window time to appear and take focus.

Posted

Ok, this is probably basic stuff to you, but now I'm not even getting the frozen Clear command...and my quit terminal is no longer working.   Here's what I have (this comes at the end of a series of workflow items to lock screen, unmount drive with terminal command, delete some files (with Automator), then this, below.   I'm sure there's a more elegant/efficient way to do all this, but it's within my ability -- or so I thought, lol):


tell application id "com.runningwithcrayons.Alfred" to search "clear"
tell application "System Events" delay 0.5 key code 36
--
delay 3
--
tell application "terminal"
    quit
end tell

Posted
3 hours ago, alnetloc said:

tell application "System Events" delay 0.5 key code 36


When @deanishe mentioned adding the delay before the key code, he didn’t mean literally in the same line but above it:


tell application id "com.runningwithcrayons.Alfred" to search "clear"

delay 0.5

tell application "System Events" to key code 36

Posted

Ok, now nothing happens.  I copied the script you corrected above, put it into a standalone workflow with a hotkey and run it...but nothing happens...  Visually, I don't see the Alfred command prompt appear, nor does the clipboard clear. 

Posted
3 hours ago, alnetloc said:

put it into a standalone workflow with a hotkey and run it...but nothing happens...

 

We can't tell you what's wrong with a workflow we've never seen based on a description like that.

 

Check the debugger for a proper error message.

  • 2 weeks 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...