2

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
  • I'm far from being an expert on this, but I'm pretty sure you need to be looking at AXMenuItems to be able to accurately hit a specific menu command by Applescript, as I did in this Answer - apple.stackexchange.com/a/209219/85275 - More good luck than good management got me there, but it might be useful to you. – Tetsujin Dec 4 '15 at 11:17
  • Will that be applicable since this is not a Check/Uncheck option? – LearningtoCode Dec 4 '15 at 11:25
  • That, I'm not absolutely certain on - but I think you need that structure to find the menu option in the first place. The long list of "of menu "x" of menu item 1 of …" etc seems crucial, though - & it's one part I really do not fully comprehend as to how it is structured. My Googling took me far & wide whilst trying to work out that answer. – Tetsujin Dec 4 '15 at 11:34
  • Just to clarify: You want to click Clear History and Empty Caches right? – Arc676 Dec 4 '15 at 11:50
  • Yes, correct. I am basically wanting to reset safari. Which is emptying cache and clearing history – LearningtoCode Dec 4 '15 at 11:58
2

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.

You must log in to answer this question.

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