Search code examples
safariapplescriptautomator

copy whatever is in Safari search bar


I am trying to click on the Safari search bar. I have already used the accessibility inspector to get the hierarchy.

tell application "System Events" to tell application process "Safari"
    click text field of group 1 of toolbar 1 of window 1
end tell

The hierarchy picture is in the link below

https://drive.google.com/file/d/1UBUA_1mb6UzoKnge5u4yw4iJYLBDScKL/view?usp=sharing

However I still don't see the text in the Safari search bar being highlighted.

Hope someone can help with this.

My aim to is to copy whatever is in the search bar and transfer that info to another app. So I am now just trying to click the search bar before figuring out how to use keystroke to copy the info in the search bar


Solution

  • I'm not sure that you can access the search bar as a text field — didn't work for me.

    However, this should allow you to accomplish the same task.

    tell application "Safari"
        activate
        tell application "System Events"
            key code 37 using command down
            
        end tell
    end tell
    

    It is the equivalent of typing command-l.

    FWIW, if you have a page loaded then you can typically just get the page's url with either of these commands. It would only be if you wanted to get bare search terms that you would need the above command.

    tell application "Safari"
        URL of tab 1 of window 1
        URL of document 1
    end tell