Clear recent items

This “Clear Recent Items” script works when run from Script Editor, but not when I export it as an app.


tell application "System Events" to click menu item "Clear Menu" of menu "Recent Items" of menu item "Recent Items" of menu "Apple" of menu bar item "Apple" of menu bar 1 of (first process whose frontmost is true)

Returns this error: “System Events got an error: Can’t get menu bar 1 of process 1 whose frontmost = true. Invalid index. (-1719)”

“Lock Screen” exported as an app works fine:


tell application "System Events" to click menu item "Lock Screen" of menu "Apple" of menu bar item "Apple" of menu bar 1 of (first process whose frontmost is true)

I’ve added both apps to Accessibility/Privacy tab of “Security & Privacy” in System Preferences.

Any ideas?

I tested both snippets as apps (with granting permissions). Both apps fails with error above.

It seems the problem is because when running app the frontmost process becomes your app’s process, to which OS X doesn’t provide “Resent items”.

So, it is better activate “Finder” and clear “Resent items” telling to it:


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

NOTE:
To turn off at all the Recent Items list on Mac you can do this:

  1. Go to System Preferences > General
  2. Click on the “Recent items” menu and select “None”.

@ KniazidisR

Thanks a lot!

Assuming that the asker wanted to include this function in a larger script supposed to do other tasks, I wrote a slightly more complex code which reset the frontmost status to the process belonging to the script.

on run
	my Germaine() # Pour que le fichier script ne soit pas modifié par son exécution !
end run

#=====

on Germaine()
	set RecentItemsLoc to localized string "2474.title" from table "MainMenu" in bundle (path to application "OnyX") --> "Éléments récents"
	set mySelf to my name
	tell application id "com.apple.systemevents"
		tell process "Finder"
			set frontmost to true
			tell menu bar 1 to tell menu bar item 1 to tell menu 1 to tell menu item RecentItemsLoc to tell menu 1 to click menu item -1
		end tell
		tell process mySelf to set frontmost to true
	end tell
end Germaine

As you see, I grabbed the localized name of the menu item from a resource belonging to ONYX.
If you are sure of the language in use in your system you may replace
[format]set RecentItemsLoc to localized string “2474.title” from table “MainMenu” in bundle (path to application “OnyX”) → “Éléments récents”[/format]

by
[format]set RecentItemsLoc to “Éléments récents” – replacing “Éléments récents” by the spelling in use on your system[/format]

An alternate way would be to drop the instruction
[format]set RecentItemsLoc to localized string “2474.title” from table “MainMenu” in bundle (path to application “OnyX”) → “Éléments récents”[/format]
and edit the code as :
[format]tell menu bar 1 to tell menu bar item 1 to tell menu 1 to tell menu item 8 to tell menu 1 to click menu item -1[/format]

The name of menu item 8 is “Recent items” for years so I assume that it’s stable enough.

I apologizes but I didn’t found an Apple’s application containing a localization table defining the different spellings for “Recent items”.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 26 aout 2019 22:12:06

Hello,

For Sonoma :

        try
            tell application id "com.apple.systemevents" to tell application process "Finder"
                tell menu bar 1 to tell menu bar item 1 to tell menu 1 to tell menu item 7 to tell menu 1 to click last menu item
            end tell
        on error errText number errNum
            display alert "Error: " & errNum message errText
        end try

This one does both Recent Folders and Recent Items. I’m on Monterey, so I have no idea if it still works on later MacOS systems.

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