0
tell application "System Events" to get value of UI element 1 of combo box 1 of toolbar "Navigation" of first group of front window of application process "Firefox"

I am using the above in an AppleScript to get the URL from the Firefox browser, what would the equivalent be using JXA. I am using JXA rather than an AppleScript because applescripts hate when you don't have a specific browser installed but still use it in the script.

Huckerby
  • 1
  • 1
  • Using JXA will do the same - it isn’t AppleScript per se, but the Script Editor that will try to look up the terminology for whatever app you are targeting. – red_menace May 23 '22 at 13:39
  • I have managed to get it to work with JXA so you can include browsers even if the user doesn't have the browser installed. Firefox doesn't work with AppleScript but if you change the accessibility settings within Firefox browser you can capture the URL. I'm just unsure how what the above AppleScript would look like using JXA – Huckerby May 23 '22 at 15:04

1 Answers1

0

Here I tested the apple-script you mentioned from the GitHub site that you would like to translate into JXA.

It contains 2 important bugs: 1) When the script is run, the front application is one, which executes the script and not the browser. This has been erroneously ignored. 2) If you do not have the Google Chrome application installed, then the script will not even compile.

The following Apple-script fixes these 2 critical bugs. I am not a JXA expert, so I leave my script as is.

property chromium_variants : {"Google Chrome", "Chromium", "Opera", "Vivaldi", "Brave Browser", "Microsoft Edge"}
property webkit_variants : {"Safari", "Webkit"}
property browsersList : chromium_variants & webkit_variants & "firefox"


tell application "System Events"
    repeat 10 times
        set frontApp to name of first process whose frontmost is true
        if browsersList contains frontApp then exit repeat
        set visible of process frontApp to false
    end repeat
end tell

if (frontApp starts with "Safari") or (frontApp starts with "Webkit") then
    set videoURL to run script "tell application " & frontApp & " to return URL of front document"
    set videoTitle to run script "tell application " & frontApp & " to return name of front document"
else if frontApp is "Firefox" then
    set videoURL to my firefoxCurrentTabURL()
    set videoTitle to my firefoxCurrentTabUTitle()
else if (frontApp starts with "Google Chrome") or (frontApp starts with "Chromium") or (frontApp starts with "Opera") or (frontApp starts with "Vivaldi") or (frontApp starts with "Brave Browser") or (frontApp starts with "Microsoft Edge") then
    set videoURL to run script "tell application " & frontApp & " to return URL of active tab of first window"
    set videoTitle to run script "tell application " & frontApp & " to return title of active tab of first window"
else
    return "You need a supported browser as your frontmost app"
end if

return {videoURL:videoURL, videoTitle:videoTitle}


on firefoxCurrentTabURL()
    -- Store the current clipboard contents.
    set theClipboard to (the clipboard as record)
    -- Set the clipboard to a default blank value
    set the clipboard to ""
    -- Bring Firefox to the front, highlight the URL in the URL field and copy it.
    tell application "System Events"
        set frontmost of application process "firefox" to true
        keystroke "lc" using {command down}
    end tell
    -- Read the clipboard contents until either they change from "" or a second elapses.
    repeat 10 times
        delay 0.2
        set theURL to the clipboard
        if theURL is not "" then exit repeat
    end repeat
    -- Restore the old clipboard contents.
    set the clipboard to theClipboard
    return theURL
end firefoxCurrentTabURL


on firefoxCurrentTabUTitle()
    tell application "System Events" to tell process "firefox"
        set frontmost to true
        set the_title to name of windows's item 1
        set the_title to (do shell script "echo " & quoted form of the_title & " | tr '[' ' '")
        set the_title to (do shell script "echo " & quoted form of the_title & " | tr ']' ' '")
    end tell
end firefoxCurrentTabUTitle
Robert Kniazidis
  • 1,760
  • 1
  • 7
  • 8
  • For other browsers other than Firefox there is a much simpler script in JXA that doesn't require any browsers to already be installed. You can find it here: https://gist.github.com/vitorgalvao/5392178#file-other_browsers-md I just need the above AppleScript statement translated to JXA to input into my JXA script. There is one pre-requisite and thats the user has to change their accessibility settings to -1 but for my use case this isnt an issue – Huckerby May 24 '22 at 09:12