Search code examples
applescriptui-automation

AppleScript error when trying to get URL from Opera browser


I have a page opened in Opera browser and I'm trying to get its URL from terminal.

I've managed to do it like this:

osascript -e  'tell application "System Events" to return value of text field "Address field" of first group of text field "Address bar" of toolbar "Navigation" of group "Browser contents" of second group of first group of front window of application process "Opera"'

It works, but it requires access to System Events and Accessibility permission set. I'd like to access Opera directly like in this solution for Google Chrome. So I've tried to rewrite it like this:

osascript -e  'tell application "Opera" to return value of text field "Address field" of first group of text field "Address bar" of toolbar "Navigation" of group "Browser contents" of second group of first group of front window'

But got an error:

49:56: syntax error: A “"” can’t go after this identifier. (-2740)

Any ideas how to fix it?


Solution

  • It is possible to interact with Opera (or Safari and Chromium based browsers) without using of "System Events".

    With opening the "Script Editor.app": File > Open Dictionary > and choosing "Opera.app" from the list of dictionaries you can browse it's dictionary.

    Based on this with the following snippet you can fetch the URL of the active tab:

    tell application "Opera"
        return URL of active tab of front window
    end tell