Open Chrome tab in Firefox? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
ManualColdPadlock
Posts: 5
Joined: 31 Dec 2019, 14:57

Open Chrome tab in Firefox?

29 Apr 2024, 15:55

Hello, my default and preferred browser is Chrome, but sometimes I would like to open the URL of the current Chrome tab in Firefox.

Is that possible using AHK?

Thank you
User avatar
mikeyww
Posts: 27161
Joined: 09 Sep 2014, 18:38

Re: Open Chrome tab in Firefox?  Topic is solved

29 Apr 2024, 16:55

Code: Select all

#Requires AutoHotkey v2.0

#HotIf WinActive('ahk_exe chrome.exe')
^F5::Run('firefox.exe ' getUrl()), SoundBeep(1500)
#HotIf

getUrl() {  ; Get URL of the active Web browser window
 ; Adapted: https://gist.github.com/7cce378c9dfdaf733cb3ca6df345b140
 Static TreeScope_Descendants     := 4
      , UIA_ControlTypePropertyId := 30003
      , UIA_DocumentControlTypeId := 50030
      , UIA_EditControlTypeId     := 50004
      , UIA_ValueValuePropertyId  := 30045
 IUIAutomation := ComObject('{FF48DBA4-60EF-4201-AA87-54103EEF594E}'
                          , '{30CBE57D-D9D0-452A-AB13-7AC5AC4825EE}')
 eRoot         := ComValue(13, 0)
 If HRESULT    := ComCall(6, IUIAutomation, 'Ptr', WinGetID('A'), 'Ptr*', eRoot)
  Throw Error('IUIAutomation::ElementFromHandle()', -1, HRESULT)
 ctrlTypeId    := WinGetClass('A') ~= 'Chrome' ? UIA_DocumentControlTypeId : UIA_EditControlTypeId
 value         := Buffer(8 + 2 * A_PtrSize, 0)
 NumPut('UShort', 3, value, 0), NumPut('Ptr', ctrlTypeId, value, 8)
 condition     := ComValue(13, 0)
 If HRESULT    := A_PtrSize = 8
  ? ComCall(23, IUIAutomation, 'UInt', UIA_ControlTypePropertyId, 'Ptr', value, 'Ptr*', condition)
  : ComCall(23, IUIAutomation, 'UInt', UIA_ControlTypePropertyId
     , 'UInt64', NumGet(value, 0, 'UInt64')
     , 'UInt64', NumGet(value, 8, 'UInt64')
     , 'Ptr*'  , condition
    )
  Throw Error('IUIAutomation::CreatePropertyCondition()', -1, HRESULT)
 eFirst := ComValue(13, 0)
 If HRESULT := ComCall(5, eRoot, 'UInt', TreeScope_Descendants, 'Ptr', condition, 'Ptr*', eFirst)
  Throw Error('IUIAutomationElement::GetRootElement()', -1, HRESULT)
 propertyValue := Buffer(8 + 2 * A_PtrSize)
 If HRESULT := ComCall(10, eFirst, 'UInt', UIA_ValueValuePropertyId, 'Ptr', propertyValue)
  Throw Error('IUIAutomationElement::GetCurrentPropertyValue()', -1, HRESULT)
 ObjRelease(eFirst.Ptr), ObjRelease(eRoot.Ptr)
 Try {
  pProperty := NumGet(propertyValue, 8, 'Ptr')
  Return StrGet(pProperty, 'UTF-16')
 }
}
ManualColdPadlock
Posts: 5
Joined: 31 Dec 2019, 14:57

Re: Open Chrome tab in Firefox?

30 Apr 2024, 04:12

mikeyww wrote:
29 Apr 2024, 16:55

Code: Select all

#Requires AutoHotkey v2.0

#HotIf WinActive('ahk_exe chrome.exe')
^F5::Run('firefox.exe ' getUrl()), SoundBeep(1500)
#HotIf

getUrl() {  ; Get URL of the active Web browser window
 ; Adapted: https://gist.github.com/7cce378c9dfdaf733cb3ca6df345b140
 Static TreeScope_Descendants     := 4
      , UIA_ControlTypePropertyId := 30003
      , UIA_DocumentControlTypeId := 50030
      , UIA_EditControlTypeId     := 50004
      , UIA_ValueValuePropertyId  := 30045
 IUIAutomation := ComObject('{FF48DBA4-60EF-4201-AA87-54103EEF594E}'
                          , '{30CBE57D-D9D0-452A-AB13-7AC5AC4825EE}')
 eRoot         := ComValue(13, 0)
 If HRESULT    := ComCall(6, IUIAutomation, 'Ptr', WinGetID('A'), 'Ptr*', eRoot)
  Throw Error('IUIAutomation::ElementFromHandle()', -1, HRESULT)
 ctrlTypeId    := WinGetClass('A') ~= 'Chrome' ? UIA_DocumentControlTypeId : UIA_EditControlTypeId
 value         := Buffer(8 + 2 * A_PtrSize, 0)
 NumPut('UShort', 3, value, 0), NumPut('Ptr', ctrlTypeId, value, 8)
 condition     := ComValue(13, 0)
 If HRESULT    := A_PtrSize = 8
  ? ComCall(23, IUIAutomation, 'UInt', UIA_ControlTypePropertyId, 'Ptr', value, 'Ptr*', condition)
  : ComCall(23, IUIAutomation, 'UInt', UIA_ControlTypePropertyId
     , 'UInt64', NumGet(value, 0, 'UInt64')
     , 'UInt64', NumGet(value, 8, 'UInt64')
     , 'Ptr*'  , condition
    )
  Throw Error('IUIAutomation::CreatePropertyCondition()', -1, HRESULT)
 eFirst := ComValue(13, 0)
 If HRESULT := ComCall(5, eRoot, 'UInt', TreeScope_Descendants, 'Ptr', condition, 'Ptr*', eFirst)
  Throw Error('IUIAutomationElement::GetRootElement()', -1, HRESULT)
 propertyValue := Buffer(8 + 2 * A_PtrSize)
 If HRESULT := ComCall(10, eFirst, 'UInt', UIA_ValueValuePropertyId, 'Ptr', propertyValue)
  Throw Error('IUIAutomationElement::GetCurrentPropertyValue()', -1, HRESULT)
 ObjRelease(eFirst.Ptr), ObjRelease(eRoot.Ptr)
 Try {
  pProperty := NumGet(propertyValue, 8, 'Ptr')
  Return StrGet(pProperty, 'UTF-16')
 }
}
Thank you @mikeyww unfortunately that hasn't worked for me; the code runs in the background but pressing the hotkey doesn't open Firefox. I've tried explicitly quoting the full path to the firefox.exe and changing the hotkey, but to no avail.
User avatar
boiler
Posts: 17170
Joined: 21 Dec 2014, 02:44

Re: Open Chrome tab in Firefox?

30 Apr 2024, 05:40

Do you hear a beep when you press Ctrl+F5 ? You are pressing the Ctrl key along with F5, right? And you don’t get an error saying that the file is not found? What exactly happens when you press the hotkey? Nothing at all?
ManualColdPadlock
Posts: 5
Joined: 31 Dec 2019, 14:57

Re: Open Chrome tab in Firefox?

30 Apr 2024, 06:57

boiler wrote:
30 Apr 2024, 05:40
Do you hear a beep when you press Ctrl+F5 ? You are pressing the Ctrl key along with F5, right? And you don’t get an error saying that the file is not found? What exactly happens when you press the hotkey? Nothing at all?
I'm an idiot! I wasn't holding down CTRL .... thank you both; it's working perfectly now I'm pressing the right buttons!

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Giresharu, kunkel321 and 57 guests