Automating tasks using Automator, AppleScript and Quick Actions
Safari Cleaner
In this fourth article in the series, we will look at automating the task of emptying cache and cleaning history in the Safari web browser. We will make an Automator application that one can run before quitting Safari.
Unlike FireFox and other web browsers, Safari does not offer a preference option to clear cache and history when quit. Instead the user must go into the menus and submenus to do so and then quit the browser. Below, we will automate all these keystrokes and reduce the process and will make an app called Safari Cleaner. This will be done using AppleScript.
** This script was revised and tested for macOS Ventura and Safari v16.1
Let’s get started…
Go to your Applications folder, utilities folder and launch the Automator app. When the Automator app runs, you will not see a window until you select from its menu File>New. Then you will get the following…
Select Application as seen above and click choose.
At the left of the Automator window, make sure Library is selected. Then at the top of the Automator window there is a search field will use to quickly get us what we need. Type “run” into the search field. No need to press enter, it should come up with a short list and including an item called “Run AppleScript.” Double-click Run AppleScript to add it to the workflow area.
You will notice there is a place holder script. Delete it, and copy then paste the following script in its place.
-- Safari Cleaner v3 railgunsgamingx.wordpress.com tell application "Safari" to activate tell application "System Events" click menu item "Clear History…" of menu 1 of menu bar item "History" of menu bar 1 of process "Safari" try click button "Clear History" of front window of process "Safari" on error try click button "Clear History" of sheet 1 of window 1 of process "Safari" end try end try delay 1 tell application "System Events" keystroke "e" using {command down, option down} end tell end tell tell application "Safari" to quit delay 1 display notification "Safari cache & history have been cleaned" with title "Safari Cleaner" subtitle "Processing is complete." end run
After pasting in the script, click the little “Hammer” button to properly auto format it.
As you read through the script, you can see that we are just automating keystrokes and telling Safari to go through its menu items to perform the tasks for us. When done we tell Safari to Quit and the System to display a notification.
Now you will need to save the app. You can name it and save it to the applications folder. This will make it handy in Launch Pad or you can pin it in the Dock. But we are not done yet…
Before we can use our Safari Cleaner app, we must grant it access in System Settings.
IMPORTANT: Safari Cleaner needs to be granted access to run keystrokes and to access folders in the user library to delete cached files.
In macOS (Ventura shown bellow), add Safari Cleaner to both the following:
-
System Settings > Security & Privacy > Privacy > Accessibility
-
System Settings > Security & Privacy > Privacy > Full Disk Access.
Simply click the + button to add Safari Cleaner to the list and then enable it by setting the slider to on (or tick box for Monterey or older). Remember in both Accessibility and Full Disk Access.
Note: If you relocate the app (or make changes), you will need to repeat the process to grant access. Remove the current Safari Cleaner in both lists and click the plus button to re-add them and make sure they are enabled.
For best use, run Safari Cleaner just before quitting Safari… let it do the rest. If Safari is not running, the script will launch Safari and preform the tasks.
I hope that you enjoy this little series of articles and that they help you in your day-to-day tasks in macOS. Automating these simple tasks can save time and replace some paid utility apps (also freeing up more of your memory).