Use AppleScript to raise and focus on window

applescript

I'm new to AppleScript. I'd like to write a piece of AppleScript that will raise and give focus to a particular Chrome window given a string that appears in the window name. After poking around a bit, I found the following:

tell application "Chrome"
    activate
    set index of first window whose name contains "mail" to 1
end tell

That makes Chrome the current application and puts the right window on top, but that window doesn't have the focus. Is there something I can add to this to direct the focus to that window (or is there a better way of doing this)?

In case it matters, I'm running Mojave.

Best Answer

I ended up doing this by clicking on the desired window in the Window menu:

tell application "System Events"
    tell process "Google Chrome"
        set frontmost to true
        click (first menu item whose name contains "mail") of menu "Window" of menu bar 1
    end tell
end tell