Dear
@Descolada - happily testing! I'm using an AHK script which detects if Chrome, Firefox, Edge or Vivaldi are running (WinWaitActive) and then grabs the currently active URL in order to open this URL in a new tab with Google Translate. I have updated the UIA-v2 libs to your latest revision and added "vivaldi.exe" to the browser group.
Symptoms:
- Works still flawlessly with Chrome and Edge!
- Firefox: No errors thrown, but NewTab() has no effect, the "translate URL" is opened again in the currenly active Tab. thus overwritten!
- Vivaldi: NewTab() fails with this error:
---- C:\XXXXX\AutoHotkey_v2\Libs\UIA-v2-main\Lib\UIA_Browser.ahk
158: If !this.HasOwnProp("NewTabButton")
158: {
▶ 159: lastTab := this.MainPaneElement.FindElement({AutomationId:"tab-", matchmode:"Substring", i:-1}, UIA.TreeScope.Children)
160: this.NewTabButton := this.MainPaneElement.FindElement({Type:"Button", startingElement:lastTab},2)
161: }
Here's my complete script. Thanks so much for looking into this!
Code: Select all
#Requires AutoHotkey v2
#include Libs\UIA-v2-main\Lib\UIA.ahk
#include Libs\UIA-v2-main\Lib\UIA_Browser.ahk
#SingleInstance Force
; Combine all browsers to be detected in "mygroup" in order to make this script work with "all browsers" !
GroupAdd("mygroup", "ahk_exe vivaldi.exe") ; Problem: Vivaldi UIA-v2 support added in May'23 but NewTab throws error!
GroupAdd("mygroup", "ahk_exe firefox.exe") ; Problem: NewTab still does not work with Firefox!
GroupAdd("mygroup", "ahk_exe chrome.exe")
GroupAdd("mygroup", "ahk_exe msedge.exe")
; This detects all browsers in mygroup
If !WinWaitActive("ahk_group mygroup",, 0.1)
ExitWithToolTip("No browser window detected within 100ms timeout!", 2000, 310, 3)
Try cUIA := UIA_Browser() ; Initialize UIA_Browser, use Last Found Window (returned by WinWaitActive)
Catch
ExitWithToolTip("UIA_Browser initialization failed, please try again.", 2000, 310, 3)
Sleep 200 ; This seems to help avoiding a error here! No more problems ever since.
Try BrowserURL := cUIA.GetCurrentURL() ; Hopefully this now also works with all browsers? At least, no errors are thrown with Firefox/Vivaldi!
Catch
ExitWithToolTip("GetCurrentURL() failed, please try again.", 2000, 310, 3)
; Encode the url with "URLencode / URIencode" so that complex urls are passed correctly to Google translate!
BrowserURLencoded := EncodeDecodeURI(BrowserURL)
Try cUIA.NewTab() ; <---------- ### This currently fails with Vivaldi and Firefox!!!
Catch
ExitWithToolTip("NewTab() failed.", 2000, 310, 3)
Try cUIA.SetURL("https://translate.google.com/website?op=translate&sl=auto&tl=en&hl=en&u=" . BrowserURLencoded, True) ; Set the URL and navigate to it
Catch
ExitWithToolTip("SetURL() failed calling translate.google.com.", 2000, 310, 3)
ExitWithToolTip("Script successfully executed, done.", 2500, 200, 60)
; Terminate script with a ToolTip & SoundBeep
ExitWithToolTip(msg, timer, soundfreq, soundmillisecs)
{
ToolTip msg
; #Hides the tooltip after 2s without having to use Sleep (which would stop the current thread)
SetTimer () => ToolTip(), -timer
SoundBeep soundfreq, soundmillisecs
Sleep timer
ExitApp()
}
; URLencode required to correctly pass "complicated" urls with spaces and special characters to Google translate !
EncodeDecodeURI(str, encode := true, component := true) {
; Adapted from teadrinker: https://www.autohotkey.com/boards/viewtopic.php?p=372134#p372134
static Doc, JS
if !IsSet(Doc) {
Doc := ComObject("htmlfile")
Doc.write('<meta http-equiv="X-UA-Compatible" content="IE=9">')
JS := Doc.parentWindow
( Doc.documentMode < 9 && JS.execScript() )
}
Return JS.%( (encode ? "en" : "de") . "codeURI" . (component ? "Component" : "") )%(str)
}