Open a new tab from address bar in google chrome

Post your working scripts, libraries and tools for AHK v1.1 and older
gimel
Posts: 18
Joined: 15 Aug 2022, 17:31

Re: Open a new tab from address bar in google chrome

16 Dec 2022, 08:00

Oh good! A new day and this one worked in Edge - see note at end of quote below. I didn't change anything else.

Descolada wrote:
10 Jun 2022, 02:53
Same feature (when Enter is pressed in Google Chrome address bar, then the URL is opened in a new tab) but instead of Acc using UIAutomation:

Code: Select all

#NoEnv
SetWorkingDir %A_ScriptDir%

UIA := UIA_Interface(), URLBarName

#if WinActive("ahk_exe chrome.exe ahk_class Chrome_WidgetWin_1") ; Google Chrome. Replace with "ahk_exe msedge.exe" to make it work with Edge
NumpadEnter::
Enter::
	if !URLBarName {
		try {
			cEl := UIA.ElementFromHandle(WinExist("A"))
			EditControlCondition := UIA.CreatePropertyCondition(UIA_ControlTypePropertyId := 30003, UIA_EditControlTypeId := 50004, VT_I4:=3)
			URLBarName := cEl.FindFirst(EditControlCondition).CurrentName
		} catch
			URLBarName := "Address and search bar"
	}
	try {
		el := UIA.GetFocusedElement()
		if (el.CurrentControlType == 50004) && (el.CurrentName == URLBarName) {
			Send, !{Enter}
			return
		}
	}	
	Send {Enter}
	return

; UIA FUNCTIONS
class UIA_Base {
	__New(p="", flag=1) {
		ObjInsert(this,"__Type","IUIAutomation" SubStr(thisClass,5))
		,ObjInsert(this,"__Value",p)
		,ObjInsert(this,"__Flag",flag)
	}
	__Get(member) {
		if member not in base,__UIA ; base & __UIA should act as normal
		{	if raw:=SubStr(member,0)="*" ; return raw data - user should know what they are doing
				member:=SubStr(member,1,-1)
			if RegExMatch(this.__properties, "im)^" member ",(\d+),(\w+)", m) { ; if the member is in the properties. if not - give error message
				if (m2="VARIANT")	; return VARIANT data - DllCall output param different
					return UIA_Hr(DllCall(this.__Vt(m1), "ptr",this.__Value, "ptr",UIA_Variant(out)))? (raw?out:UIA_VariantData(out)):
				else if UIA_Hr(DllCall(this.__Vt(m1), "ptr",this.__Value, "ptr*",out))
					return raw?out:m2="BSTR"?StrGet(out):RegExMatch(m2,"i)IUIAutomation\K\w+",n)?new UIA_%n%(out):out ; Bool, int, DWORD, HWND, CONTROLTYPEID, OrientationType?
			}
			else throw Exception("Property not supported by the " this.__Class " Class.",-1,member)
		}
	}
	__Delete() {
		this.__Flag ? ObjRelease(this.__Value):
	}
	__Vt(n) {
		return NumGet(NumGet(this.__Value+0,"ptr")+n*A_PtrSize,"ptr")
	}
}

class UIA_Interface extends UIA_Base {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee671406(v=vs.85).aspx
	static __IID := "{30cbe57d-d9d0-452a-ab13-7ac5ac4825ee}"
		,  __properties := ""
	ElementFromHandle(hwnd) {
		return UIA_Hr(DllCall(this.__Vt(6), "ptr",this.__Value, "ptr",hwnd, "ptr*",out))? new UIA_Element(out):
	}
	GetFocusedElement() {
		return UIA_Hr(DllCall(this.__Vt(8), "ptr",this.__Value, "ptr*",out))? new UIA_Element(out):
	}
	CreatePropertyCondition(propertyId, var, type="Variant") {
		if (type!="Variant")
			UIA_Variant(var,type,var)
		return UIA_Hr((A_PtrSize == 4) ? DllCall(this.__Vt(23), "ptr",this.__Value, "int",propertyId, "int64", NumGet(var, 0, "int64"), "int64", NumGet(var, 8, "int64"), "ptr*",out) : DllCall(this.__Vt(23), "ptr",this.__Value, "int",propertyId, "ptr",&var, "ptr*",out))? new UIA_PropertyCondition(out):
	}
}
class UIA_Element extends UIA_Base {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee671425(v=vs.85).aspx
	static __IID := "{d22108aa-8ac5-49a5-837b-37bbb3d7591e}"
		,  __properties := "CurrentControlType,21,CONTROLTYPEID`r`nCurrentName,23,BSTR"
	
	SetFocus() {
		return UIA_Hr(DllCall(this.__Vt(3), "ptr",this.__Value))
	}
	FindFirst(c="", scope=0x4) {
		return UIA_Hr(DllCall(this.__Vt(5), "ptr",this.__Value, "uint",scope, "ptr",(c=""?this.TrueCondition:c).__Value, "ptr*",out))&&out? new UIA_Element(out):
	}
}
class UIA_Condition extends UIA_Base {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee671420(v=vs.85).aspx
	static __IID := "{352ffba8-0973-437c-a61f-f64cafd81df9}"
}
class UIA_PropertyCondition extends UIA_Condition {
	;~ http://msdn.microsoft.com/en-us/library/windows/desktop/ee696121(v=vs.85).aspx
	static __IID := "{99ebf2cb-5578-4267-9ad4-afd6ea77e94b}"
		,  __properties := "PropertyId,3,PROPERTYID`r`nPropertyValue,4,VARIANT`r`nPropertyConditionFlags,5,PropertyConditionFlags"
}
UIA_Interface() {
	try {
		if uia:=ComObjCreate("{ff48dba4-60ef-4201-aa87-54103eef594e}","{30cbe57d-d9d0-452a-ab13-7ac5ac4825ee}")
			return uia:=new UIA_Interface(uia, 1), uia.base.base.__UIA:=uia
		throw "UIAutomation Interface failed to initialize."
	} catch e
		MsgBox, 262160, UIA Startup Error, % IsObject(e)?"IUIAutomation Interface is not registered.":e.Message
}
UIA_Hr(hr) {
	;~ http://blogs.msdn.com/b/eldar/archive/2007/04/03/a-lot-of-hresult-codes.aspx
	static err:={0x8000FFFF:"Catastrophic failure.",0x80004001:"Not implemented.",0x8007000E:"Out of memory.",0x80070057:"One or more arguments are not valid.",0x80004002:"Interface not supported.",0x80004003:"Pointer not valid.",0x80070006:"Handle not valid.",0x80004004:"Operation aborted.",0x80004005:"Unspecified error.",0x80070005:"General access denied.",0x800401E5:"The object identified by this moniker could not be found.",0x80040201:"UIA_E_ELEMENTNOTAVAILABLE",0x80040200:"UIA_E_ELEMENTNOTENABLED",0x80131509:"UIA_E_INVALIDOPERATION",0x80040202:"UIA_E_NOCLICKABLEPOINT",0x80040204:"UIA_E_NOTSUPPORTED",0x80040203:"UIA_E_PROXYASSEMBLYNOTLOADED"} ; //not completed
	if hr&&(hr&=0xFFFFFFFF) {
		RegExMatch(Exception("",-2).what,"(\w+).(\w+)",i)
		throw Exception(UIA_Hex(hr) " - " err[hr], -2, i2 "  (" i1 ")")
	}
	return !hr
}
UIA_Hex(p) {
	setting:=A_FormatInteger
	SetFormat,IntegerFast,H
	out:=p+0 ""
	SetFormat,IntegerFast,%setting%
	return out
}
UIA_GUID(ByRef GUID, sGUID) { ;~ Converts a string to a binary GUID and returns its address.
	VarSetCapacity(GUID,16,0)
	return DllCall("ole32\CLSIDFromString", "wstr",sGUID, "ptr",&GUID)>=0?&GUID:""
}
UIA_Variant(ByRef var,type=0,val=0) {
	; Does a variant need to be cleared? If it uses SysAllocString? 
	return (VarSetCapacity(var,8+2*A_PtrSize)+NumPut(type,var,0,"short")+NumPut(type=8? DllCall("oleaut32\SysAllocString", "ptr",&val):val,var,8,"ptr"))*0+&var
}
UIA_IsVariant(ByRef vt, ByRef type="") {
	size:=VarSetCapacity(vt),type:=NumGet(vt,"UShort")
	return size>=16&&size<=24&&type>=0&&(type<=23||type|0x2000)
}
UIA_VariantData(ByRef p, flag=1) {
	return !UIA_IsVariant(p,vt)?"Invalid Variant"
			:vt=3?NumGet(p,8,"int")
			:vt=8?StrGet(NumGet(p,8))
			:vt=9||vt=13||vt&0x2000?ComObj(vt,NumGet(p,8),flag)
			:vt<0x1000&&UIA_VariantChangeType(&p,&p)=0?StrGet(NumGet(p,8)) UIA_VariantClear(&p)
			:NumGet(p,8)
}
UIA_VariantChangeType(pvarDst, pvarSrc, vt=8) { ; written by Sean
	return DllCall("oleaut32\VariantChangeTypeEx", "ptr",pvarDst, "ptr",pvarSrc, "Uint",1024, "Ushort",0, "Ushort",vt)
}
UIA_VariantClear(pvar) { ; Written by Sean
	DllCall("oleaut32\VariantClear", "ptr",pvar)
}
The first time pressing "Enter" might be a bit sluggish, but afterwards it should be fast. This drawback can be avoided if the URL bar name is already known, for example if Chrome is being ran in English, change the line UIA := UIA_Interface(), URLBarName to UIA := UIA_Interface(), URLBarName:="Address and search bar"
The name of the address bar can be found out using UIAViewer or Accessibility Insights

By default this works with Google Chrome, but can work with Edge too (change #if WinActive("ahk_exe chrome.exe ahk_class Chrome_WidgetWin_1") to #if WinActive("ahk_exe msedge.exe")).
Kudos
Posts: 8
Joined: 23 Aug 2015, 05:52

Re: Open a new tab from address bar in google chrome

02 May 2023, 02:21

Works perfectly in Edge for me. This was bugging me so much since I switched from Slimjet browser. Thanks so much! :D
DustinLuck
Posts: 7
Joined: 28 Jul 2021, 12:26

Re: Open a new tab from address bar in google chrome

27 Jun 2023, 10:47

Late to the party here, but for anyone who reads down this far, the built-in Chrome shortcut to open the contents of the address bar is Alt + Enter.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: gwarble and 151 guests