MacOS – Not able to clear History in Safari using AppleScript in OS X El Capitan

applescriptmacossafari

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

Best Answer

  • 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