I’ve got an AppleScript that clears the “Recent Folders” and “Recent Items” menus on Shut Down. I’ve now added clearing the “Clipy” clipboard as well. I would like this to be totally automatic, but after trying many things, I can’t figure out how to have the script “click” the “Clear History” window that pops up at the very end. Any help would be appreciated.
Here’s the script:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Finder" to activate
tell application "System Events" to tell application process "Finder"
tell menu "Apple" of menu bar item "Apple" of menu bar 1
click menu item "Clear Menu" of menu "Recent Items" of menu item "Recent Items"
end tell
end tell
tell application "Finder" to activate
tell application "System Events" to tell application process "Finder"
tell menu "Go" of menu bar item "Go" of menu bar 1
click menu item "Clear Menu" of menu "Recent Folders" of menu item "Recent Folders"
end tell
end tell
tell application "System Events" to key code 8 using {option down, shift down, command down, control down}
And, here’s the popup window that pops up at the very end of the script:
I know that you can select the “Don’t ask again” but I’d really rather not do that.
Here’s what I’ve come up with so far (and it seems to work). Just looking for someone with more experience with AppleScript than myself to take a look at the script and see if it can be improved, shortened, etc.
tell application "System Events" to key code 8 using {option down, shift down, command down, control down}
tell application "System Events"
tell application process "Clipy"
activate
set frontmost to true
-- press return on default "Clear History" button. Assumption: That window is present.
tell front window to key code 36
-- tell front window to tell button "Clear History" to click
end tell
end tell
what does the line below do?
tell application “System Events” to key code 8 using {option down, shift down, command down, control down}
Here is a sample script for Clipy.
tell application "System Events"
tell application process "Clipy"
click menu bar item 1 of menu bar 2
repeat until exists menu item "Clear History" of menu 1 of menu bar 2
delay 0.1
end repeat
click menu item "Clear History" of menu 1 of menu bar 2
repeat until exists button "Clear History" of window 1
delay 0.1
end repeat
click button "Clear History" of window 1
end tell
end tell
Here is another version with timeouts.
tell application "System Events"
tell application process "Clipy"
click menu bar item 1 of menu bar 2
repeat with i from 30 to 0 by -1
if exists menu item "Clear History" of menu 1 of menu bar 2 then exit repeat
delay 0.2
end repeat
if i = 0 then return
if enabled of menu item "Clear History" of menu 1 of menu bar 2 then
click menu item "Clear History" of menu 1 of menu bar 2
repeat with i from 30 to 0 by -1
if exists button "Clear History" of window 1 then exit repeat
delay 0.2
end repeat
if i = 0 then return
click button "Clear History" of window 1
else
perform action "AXCancel" of menu 1 of menu bar 2
end if
end tell
end tell
That’s the “Clear History” for Clipy.
My thought was that with Clipy’s built in “Clear History” shortcut, the script would be much faster using that than the menu drop down. And, if you try it, it’s basically a quick flash on the desktop.
EDIT: I’m just learning AppleScript so please bear with me here. Turns out, after trial and error, that either 36 or 76 will work. It was a timing issue. If you insert a “delay 1” as shown in the image, it will work with either number.
In the script I posted to clear the history in Clipy, as I added other items that I wanted to be done on either Restart or Shut Down sometimes the script would work, sometimes not. Looked again at the AppleScript key codes here (Complete list of AppleScript key codes) and saw that the Return key had two numbers (see image). I was using 36, changed it to 76 and now the script runs successfully each time (that’s on my 2020 M1 MacBook Pro). May change again when run on my 2014 Intel Mac mini with an extended Apple keyboard, we’ll see.
1 Like
Now that you’re starting to dabble with key codes
, you will save yourself a lot of headaches in the long run if you comment, in your code, what the actual keys pressed are for the key code
commands you use. Because in the future if you have to go back and edit the code, there is a 99% chance you’re not gonna remember what the actual keys pressed are for the key code
you used.
For example instead of using this… tell front window to key code 36
You may want to consider using this… tell front window to key code 36 -- return key
With AppleScript, anything that comes after --
is just a comment you can use for your own convenience and will not be executed.
You also may want to consider installing the Key Codes.app, which pretty much allows you to type any keyboard sequence and it gives you the equivalent key code. I personally consider this one of the more valuable tools in my AppleScript arsenal.
If you have Homebrew installed, in Terminal, the command brew install key-codes
, will install the app for you. You could also simply download the app from here… Key Codes
1 Like
Thank you for the advice, and your timing is perfect. After putting hours and hours into this little project (totally unnecessary by the way, but it was fun to see if I could actually do it) and getting it finally working on my 2020 M1 MacBook Pro, I was disappointed when I transferred the script to my 2014 Intel Mac mini. Ran the script and . . . nothing . . . at . . . all would work. Then I remembered, the keyboard on the mini dates way, way back, probably late 90’s. And every time I install a macOS on that mini, when it gets to the part about the keyboard, it’s not recognized. I always shout at the screen, “But it’s an Apple keyboard, has that funny little bitten apple logo right on it.” I kept it because it’s the last “extended” keyboard Apple supplied with Macs. I’ve had two iMacs (both long gone) and both can with that little “magic” keyboard. I still have those in the office somewhere, just not willing to give up the number keys on the extended versions. Possibly, the Key Codes app will allow me to run the script on that keyboard by giving me the code for it’s “unrecognized” Enter key. Thanks again!
With Key Codes installed I was able to get the script working on my 2104 Mac mini with extended keyboard. Thanks!