MacOS – AppleScript “can’t get menu item” error

applescriptmacosmenu barsafari

I am looking for some help on correcting this error:

error "System Events got an error: Can’t get menu item \"Clear History…\" of menu \"Safari\" of menu bar 1 of process \"Safari\"." number -1728 from menu item "Clear History…" of menu "Safari" of menu bar 1 of process "Safari"

My code is:

tell application "Safari"
    launch
    activate
    if not (exists document 1) then
        make new document at front
    end if
end tell
delay 1
tell application "System Events"
    keystroke "e" using {option down, command down}
    tell process "Safari"
        tell menu bar 1
            tell menu "Safari"
                tell menu item "Clear History..."
                    click
                    every UI element
                end tell
            end tell
        end tell
        tell window 1
            tell sheet 1
                tell button "Clear History"
                    click
                end tell
            end tell
        end tell
        tell menu bar 1
            tell menu bar item "Develop"
                tell menu 1
                    click menu item "Empty Caches"
                end tell
            end tell
        end tell
    end tell
end tell

Best Answer

I think you need to make the menu item "Clear History and Website Data…", not "Clear History and Website Data..." (a single ellipses character: "…" vs. three periods: "..."

I didn't test the whole script, but this change brought the menu item up in Safari for me via AppleScript.