andy4222 Posted May 29, 2021 Posted May 29, 2021 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
deanishe Posted May 30, 2021 Posted May 30, 2021 (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 May 30, 2021 by deanishe
alnetloc Posted December 5, 2021 Posted December 5, 2021 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?
deanishe Posted December 5, 2021 Posted December 5, 2021 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.
alnetloc Posted December 5, 2021 Posted December 5, 2021 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
vitor Posted December 6, 2021 Posted December 6, 2021 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
alnetloc Posted December 6, 2021 Posted December 6, 2021 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.
deanishe Posted December 6, 2021 Posted December 6, 2021 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.
alnetloc Posted December 6, 2021 Posted December 6, 2021 Well, I finally got it working after all. Thanks for your help.
matthewstroh Posted December 20, 2021 Posted December 20, 2021 This worked great for me on the first try. Thanks, @deanishe.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now