2

I have been using below script to clear the Safari history from selenium test in OS X Yosemite . I have upgraded to OS X El Capitan and now the script won't work.

Error returned : Can't get "Clear History" button

tell application "Safari" to activate
tell application "System Events"
    tell process "Safari"
        tell menu bar 1
            tell menu bar item "History"
                tell menu 1
                    click menu item "Clear History…"
                end tell
            end tell
        end tell
        delay 1 -- optional
        click button "Clear History"
    end tell
end tell
1
  • Is my updated answer working for you? Commented Sep 18, 2016 at 14:46

2 Answers 2

2

In OS X El Capitan, when clicking Clear History… on the History menu, it causes Clear History to appear either as a modal Dialog Box or as a focused Sheet on a window, as illustrated in the images at the bottom of this answer, and the AppleScript code below handles it in either case scenario.

The code is written with the assumption it appears as a Dialog Box and if it fails to process as a Dialog Box, then the on error handler processes it as a Sheet.

This code, directly below, essentially replicates what your code did in OS X Yosemite, although adjusted for use in OS X El Capitan, sans the delay command which you can add if you want.


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
end tell

To interact with the list on the Clear pop up button, for how much of the History to clear, the following code example address it.

Because it can appear two different ways, it unfortunately requires two separate sets of similar code and both sections of code surrounding the "Clear ..." list must be manually maintained and in sync if/when using that code segment.

Note: There are 10 lines of code commented out, preceded by --, pertaining to the "Clear ..." list, 5 lines in each grouping of code. You can change the current value for one of 4 choices by uncommenting the click pop up button 1 of window 1 of process "Safari" and click pop up button 1 of sheet 1 of window 1 of process "Safari" lines of code, the one in each section, and then only 1 of the other 4 at a time, the similar one in each section. This means only 4 lines total are to be uncommented, 2 in each section, 1 to click the popup button and the other for how much of the History you want cleared. The comments I've added, preceded by -- #, within the code should be self-explanatory.

tell application "Safari"
    activate
end tell

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
        -- # 
        -- # NOTE: Keep this block of commented out code in sync with the similar block below!
        -- # 
        -- # If you want to change the value of the "Clear ..." list, then uncomment the line of code directly below this comment.
        -- # 
        -- click pop up button 1 of window 1 of process "Safari"
        -- #     
        -- # If you've uncommented the line of code directly above this comment, then uncomment only 1 of the 4 lines of code below.
        -- # 
        -- click menu item "the last hour" of menu 1 of pop up button 1 of window 1 of process "Safari"
        -- click menu item "today" of menu 1 of pop up button 1 of window 1 of process "Safari"
        -- click menu item "today and yesterday" of menu 1 of pop up button 1 of window 1 of process "Safari"
        -- click menu item "all history" of menu 1 of pop up button 1 of window 1 of process "Safari"
        -- # 

        click button "Clear History" of front window of process "Safari"

    on error
        try
            -- # 
            -- # NOTE: Keep this block of commented out code in sync with the similar block above!
            -- #     
            -- # If you want to change the value of the "Clear ..." list, then uncomment the line of code directly below this comment.
            -- # 
            -- click pop up button 1 of sheet 1 of window 1 of process "Safari"
            -- #     
            -- # If you've uncommented the line of code directly above this comment, then uncomment only 1 of the 4 lines of code below.
            -- # 
            -- click menu item "the last hour" of menu 1 of pop up button 1 of sheet 1 of window 1 of process "Safari"
            -- click menu item "today" of menu 1 of pop up button 1 of sheet 1 of window 1 of process "Safari"
            -- click menu item "today and yesterday" of menu 1 of pop up button 1 of sheet 1 of window 1 of process "Safari"
            -- click menu item "all history" of menu 1 of pop up button 1 of sheet 1 of window 1 of process "Safari"
            -- # 

            click button "Clear History" of sheet 1 of window 1 of process "Safari"

        end try
    end try
end tell

This as coded runs very fast, however, you can insert some delay commands, where appropriate, if you want to see better what's happening as the History is being cleared.


In the images below, one with the red arrow is pointing at what the 10 lines of code commented out, preceded by --, pertaining to the "Clear ..." list is about. To change e.g., "the last hour" to one of the other values, this is where you uncomment the appropriate commented lines of code.

As a Sheet: enter image description here

As a Dialog Box:

enter image description here

4
  • @user3290656, I'll assume you understood the code that was commented out and how to use it, if needed/wanted. If not, just ask. Commented Sep 11, 2016 at 16:54
  • I understood the code. Sure. My Scenario was to clear all the history. It was straight forward. Your code works perfectly. Also , I am facing one more issue. Sometime the history sheet we get as pop up. How do I handle that scenario Commented Sep 13, 2016 at 2:31
  • @user3290656, I've updated, actually rewritten, my answer to your question. It now handles Clear History appearing as either a modal Dialog Box or a focused Sheet. Commented Sep 16, 2016 at 5:51
  • Hi: thanks for this script. However I get an error saying that „menu bar item "History" of menu bar 1 of process "Safari"“ cannot be read (-1728). What is the source of this? Thx for helping!
    – mcstamp
    Commented Aug 12, 2017 at 16:39
0

Awesome, awesome and even more awesome..

Your answer seems to get most things cleared out and I appreciate your efforts very much.

Thank you for your time and efforts good sir.

Now I wish that I had looked for a response much earlier.


So what I finished with so far that seems to be working is the following.

The "Safari On" script looks like this at the moment but works like so:

  • It activates Safari...

  • closes any open windows..

  • selects the "Clear History" option...

  • selects the "Clear ALL History" option...

  • actually does clear All History and ..(Most)data..

  • then it repeats to clear the history/data twice in total

  • then leaves you with a clean fresh window to browse with.

==== "Safari On":

tell application "System Events"
tell application "Safari"
    activate
    close every window
end tell
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 pop up button 1 of window 1 of process "Safari"
        click menu item "all history" of menu 1 of pop up button 1 of window 1 of process "Safari"
        click button "Clear History" of front window of process "Safari"
        end try
end tell
tell application "Safari"
    make new document
end tell
end tell

And the "Safari Off" script is basically the same except it closes all windows and quits the program :

======

"Safari Off":

tell application "Safari"
activate
close every window
end tell
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 pop up button 1 of window 1 of process "Safari"
    click menu item "all history" of menu 1 of pop up button 1 of window 1 of process "Safari"
    click button "Clear History" of front window of process "Safari"
end try
end tell
tell application "Safari"
quit
end tell

====

OK.. now that the scripts actually clear the History and data..

I saved them into the Application folder as.. an Application ...

Then I set up a separate Service for each of the two scripts that simply launches that script with a command key.

I used Automator to create the Services and then the System services/ keyboard/shortcuts... to assign a command key for each script.

this turns out to be really fast and convienent at clearing Safari history and data.

Who knows?.. maybe its possible to have these scripts run automatically upon open or close of Safari???

If anyone can improve on this then feel free. I would love to see a more efficient cleaner and faster script made by someone who knows what they are doing.

Also.. any other useful scripts for stream lining app processes. I made one for the "Clean My Mac" app that opens Clean my Mac, runs a scan... then runs the clean process and quits the app... all from a command key.

thanks again for your time and efforts at solving this issue... your awsome.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .