Browse chrome tabs looking for a specific one Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Elermino
Posts: 114
Joined: 29 Nov 2021, 17:43

Browse chrome tabs looking for a specific one

30 Dec 2021, 20:21

What is missing from this code so that when you press the keyboard shortcut, it looks for the tab that was passed as an argument to the function? In this case, YouTube

Code: Select all

; Activate tab in Google Chrome if it exists, return true/false if exist/doesn't exist
ActivateChromeTab(soughtTab)
{
  IfWinNotExist Google Chrome
  {
    return false
  }

  WinActivate Google Chrome
  WinWaitActive Google Chrome
  WinGetTitle, currentTab, A
  firstTab := currentTab

  if (InStr(currentTab, soughtTab) > 0)
  {
    return true
  }

  Loop
  {
    Send {CtrlDown}{Tab}{CtrlUp}
    Sleep 50
    WinGetTitle, currentTab, A
    foundTab := InStr(currentTab, soughtTab) > 0
  }
  Until (foundTab || currentTab == firstTab)

  return foundTab
}
f2::ActivateChromeTab(Youtube - Google Chrome)
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Browse chrome tabs looking for a specific one

30 Dec 2021, 23:15

There are a bunch of mistakes. As for the main ones, you should learn this: SetTitleMatchMode and take your literal string function parameter in quotes:

Code: Select all

f2::ActivateChromeTab("Youtube - Google Chrome")
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
User avatar
mikeyww
Posts: 27309
Joined: 09 Sep 2014, 18:38

Re: Browse chrome tabs looking for a specific one

31 Dec 2021, 08:54

This works for one Chrome window. If needed, it can be modified to handle multiple windows.

Code: Select all

#Include D:\utils\acc.ahk ; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=26201

#IfWinActive ahk_exe chrome.exe
^F2::activateChromeTab("YouTube")
#IfWinActive

activateChromeTab(tabTitle) { ; https://www.autohotkey.com/boards/viewtopic.php?p=436429#p436429
 For each, oChild in Acc_Children(Acc_Get("Object", TABS := "4.1.1.2.1.1.1"))
  If oChild.accName(0) = tabTitle
   Return oChild.accDoDefaultAction(0)
}
Here is more good stuff from jeeswg, but the Acc paths might be outdated.

viewtopic.php?p=294316#p294316
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Browse chrome tabs looking for a specific one

04 Jan 2022, 15:54

mikeyww wrote:
31 Dec 2021, 08:54
If oChild.accName(0) = tabTitle
I think, we should use Instr() here instead. Or some sort of RegExmatch() if we want to set up more complex conditions.

Code: Select all

If InStr(oChild.accName(0), tabTitle)
...
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
User avatar
mikeyww
Posts: 27309
Joined: 09 Sep 2014, 18:38

Re: Browse chrome tabs looking for a specific one

04 Jan 2022, 15:57

That all depends on the other tabs, but certainly can be done. Yes, Instr was originally used.
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Browse chrome tabs looking for a specific one  Topic is solved

04 Jan 2022, 17:37

@mikeyww, can you tell why your code works, but if we remove #IfWinActive lines, then it would fail? Chrome window is active, but it fails!

Code: Select all

#Include D:\utils\acc.ahk ; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=26201

^F2::activateChromeTab("YouTube")

activateChromeTab(tabTitle) { ; https://www.autohotkey.com/boards/viewtopic.php?p=436429#p436429
 For each, oChild in Acc_Children(Acc_Get("Object", TABS := "4.1.1.2.1.1.1"))
  If oChild.accName(0) ~= tabTitle
   Return oChild.accDoDefaultAction(0)
}
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
User avatar
mikeyww
Posts: 27309
Joined: 09 Sep 2014, 18:38

Re: Browse chrome tabs looking for a specific one

05 Jan 2022, 05:37

The default window is the last found window. With #IfWinActive, the last found window is the window identified by that directive. The issue can be addressed below.

Code: Select all

activateChromeTab(tabTitle) { ; https://www.autohotkey.com/boards/viewtopic.php?p=436429#p436429
 For each, oChild in Acc_Children(Acc_Get("Object", TABS := "4.1.1.2.1.1.1",, "ahk_exe chrome.exe"))
  If oChild.accName(0) = tabTitle
   Return oChild.accDoDefaultAction(0)
}
With #IfWinActive, the last found window will be the active window. With "ahk_exe chrome.exe", the Chrome window that is used will be the topmost Chrome window.
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Browse chrome tabs looking for a specific one

05 Jan 2022, 09:53

Oh, it uses WinExist()... I should look at Acc_Get in acc.ahk before asking a question :).
Although I didn't know that the last found window can be got through #IfWinActive, thanks!
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
User avatar
mikeyww
Posts: 27309
Joined: 09 Sep 2014, 18:38

Re: Browse chrome tabs looking for a specific one

05 Jan 2022, 10:01

You are right.

It's a little bit hidden in the documentation (not on the "Last found window" page but in "#IfWinActive" documentation):
The Last Found Window is set by #IfWinActive/Exist (though not by #IfWinNotActive/NotExist).
User avatar
Elermino
Posts: 114
Joined: 29 Nov 2021, 17:43

Re: Browse chrome tabs looking for a specific one

11 Feb 2022, 10:02

amateur+ wrote:
04 Jan 2022, 15:54
mikeyww wrote:
31 Dec 2021, 08:54
If oChild.accName(0) = tabTitle
I think, we should use Instr() here instead. Or some sort of RegExmatch() if we want to set up more complex conditions.

Code: Select all

If InStr(oChild.accName(0), tabTitle)
...
It really works and navigates to the desired tab, but how do I do something (for example: send 0) when I get to that tab?
User avatar
mikeyww
Posts: 27309
Joined: 09 Sep 2014, 18:38

Re: Browse chrome tabs looking for a specific one

11 Feb 2022, 10:12

Code: Select all

^F2::
activateChromeTab("YouTube")
Send 0
Return
User avatar
Elermino
Posts: 114
Joined: 29 Nov 2021, 17:43

Re: Browse chrome tabs looking for a specific one

11 Feb 2022, 11:19

mikeyww wrote:
11 Feb 2022, 10:12

Code: Select all

^F2::
activateChromeTab("YouTube")
Send 0
Return
Tanks!
User avatar
submeg
Posts: 330
Joined: 14 Apr 2017, 20:39
Contact:

Re: Browse chrome tabs looking for a specific one

19 Feb 2022, 20:48

mikeyww wrote:
31 Dec 2021, 08:54
This works for one Chrome window. If needed, it can be modified to handle multiple windows.

Code: Select all

#Include D:\utils\acc.ahk ; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=26201

#IfWinActive ahk_exe chrome.exe
^F2::activateChromeTab("YouTube")
#IfWinActive

activateChromeTab(tabTitle) { ; https://www.autohotkey.com/boards/viewtopic.php?p=436429#p436429
 For each, oChild in Acc_Children(Acc_Get("Object", TABS := "4.1.1.2.1.1.1"))
  If oChild.accName(0) = tabTitle
   Return oChild.accDoDefaultAction(0)
}
Here is more good stuff from jeeswg, but the Acc paths might be outdated.

viewtopic.php?p=294316#p294316
@mikeyww, thanks for this! I have been able to use this when I specify the URL, but when I try to grab the URL (using the code HERE), but I can't get it to work?

I would like to cycle through all the open tabs and record all the names of the tabs and hold them for use later.
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
User avatar
mikeyww
Posts: 27309
Joined: 09 Sep 2014, 18:38

Re: Browse chrome tabs looking for a specific one

19 Feb 2022, 21:19

Code: Select all

#IfWinExist ahk_exe chrome.exe
F3:: ; F3 = Get Chrome tab names, and store them in a string
tabs =
For each, oChild in Acc_Children(Acc_Get("Object", "4.1.1.2.1.1.1"))
 tabs .= (tabs > "" ? "`n" : "") oChild.accName(0) ; Names of the tabs
MsgBox, 64, Tabs, %tabs%
Return
#IfWinExist
amateur+
Posts: 655
Joined: 09 Oct 2021, 15:43

Re: Browse chrome tabs looking for a specific one

20 Feb 2022, 12:55

@submeg, if you really want to grab Title+url, you can use this combination of maestro's codes:

Code: Select all

#IfWinExist ahk_exe chrome.exe
F3:: ; F3 = Get Chrome tab names and urls, and store them in a string
VarSetCapacity(tabs, 2048)
For each, oChild in Acc_Children(Acc_Get("Object", "4.1.1.2.1.1.1")) {
	tabs .= (tabs > "" ? "`n`n" : "") oChild.accName(0)
	oChild.accDoDefaultAction(0)
	urlField := Acc_Get("Object", "4.1.1.2.1.2.5.3")
	loop 2
		urlField.accDoDefaultAction(0)
	tabs .=  "`n" urlField.accValue(0)
}
MsgBox, 64, Tabs, %tabs%
Return
#IfWinExist
EDIT: Added these lines to grab url with https://(www.):

Code: Select all

urlField := Acc_Get("Object", "4.1.1.2.1.2.5.3")
	loop 2
		urlField.accDoDefaultAction(0)
Last edited by amateur+ on 26 Feb 2022, 16:28, edited 3 times in total.
Have found any drawback in my code or approach? Please, point it out. /The moderator ordered to remove the rest of the signature, I had obeyed.
And I really apologize for our russian president. Being a citizen of an aggressor country is very shameful. Personally I tried to avoid this trying to defend elections from fraud being a member of the election commission of one of the precincts but only was subjected to a hooligan attack and right before the vote count was illegally escorted from the polling station and spent the night behind bars (in jail) in a result of illegal actions of corrupt policemen.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Browse chrome tabs looking for a specific one

20 Feb 2022, 14:18

For each, oChild in Acc_Children(Acc_Get("Object", "4.1.1.2.1.1.1"))
tabs .= "`n" Acc_Get("Object", "4.1.1.2.1.2.5.3").accValue(0)

Is there a chance to identify/extract those two colored values during a Chrome Browser session using an AHK script snippet/tool (I'd guess it's possible if using a specific section from ACCViewer.ahk, bc it is showing that ACC-path @ its footer section/status bar)? :shifty:
User avatar
mikeyww
Posts: 27309
Joined: 09 Sep 2014, 18:38

Re: Browse chrome tabs looking for a specific one

20 Feb 2022, 14:29

That is what the script does, so what am I missing?
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Browse chrome tabs looking for a specific one

20 Feb 2022, 14:45

@mikeyww - AFAIK, these values are dependent on Chrome's current release (here v98, so might change with whatever upgrade).
So, detecting these values seems key to keeping those scripts running with upcoming Chrome releases? Am I wrong? :think:
User avatar
mikeyww
Posts: 27309
Joined: 09 Sep 2014, 18:38

Re: Browse chrome tabs looking for a specific one

20 Feb 2022, 14:48

I see. I think you are asking how to identify the Acc path dynamically. Short of testing every path, I'm not sure! It seems like the path simply depends on the program's specific design and nothing more. Maybe there is a way to "search" for the name of the path element, and thereby identify the right path. Perhaps someone here @jeeswg knows.
User avatar
submeg
Posts: 330
Joined: 14 Apr 2017, 20:39
Contact:

Re: Browse chrome tabs looking for a specific one

26 Feb 2022, 15:20

amateur+ wrote:
20 Feb 2022, 12:55
@submeg, if you really want to grab Title+url, you can use this combination of maestro's codes:

Code: Select all

#IfWinExist ahk_exe chrome.exe
F3:: ; F3 = Get Chrome tab names and urls, and store them in a string
VarSetCapacity(tabs, 2048)
For each, oChild in Acc_Children(Acc_Get("Object", "4.1.1.2.1.1.1")) {
	tabs .= (tabs > "" ? "`n`n" : "") oChild.accName(0)
	oChild.accDoDefaultAction(0)
	urlField := Acc_Get("Object", "4.1.1.2.1.2.5.3")
	loop 2
		urlField.accDoDefaultAction(0)
	tabs .=  "`n" urlField .accValue(0)
}
MsgBox, 64, Tabs, %tabs%
Return
#IfWinExist
EDIT: Added these lines to grab url with https://(www.):

Code: Select all

urlField := Acc_Get("Object", "4.1.1.2.1.2.5.3")
	loop 2
		urlField.accDoDefaultAction(0)
Thanks @amateur+, I ended up doing something very similar.

Now the only question is, how can I reactivate a particular tab?

EDIT:I just reused the code that populates the list box to cycle through until it finds the correct tab as opposed to the first tab. Is there a way to go directly to a tab without cycling a second time?

Code: Select all


#SingleInstance, Force

Menu, Tray, Icon, % A_WinDir "\system32\netshell.dll", 86 ; Shows a world icon in the system tray

TabCount := 0		;To count how many tabs there are.

;Create a string array.
TabArray := []				;A way to store the tab names in an array. Not used, left for learning.
sListOfTabs := ""
iListBoxNumRows := 10		;How many tabs to display in the ListBox

;==================================================
;--------------------------------------------------

#1::

TabCount := 0
FirstTitle := ""
TabTitle := ""
sListBox := ""

If WinExist("ahk_class Chrome_WidgetWin_1")
{

;https://www.autohotkey.com/board/topic/118664-count-open-tabs-in-chrome-browser/#entry678852
Loop
  { 
  
	WinGetTitle, TabTitle, A ;<- first get the title
	
    FirstTitle := Titl1 ;<- and see if it matches the first window checked
	
    if (FirstTitle = TabTitle) ;<- if a match we've looped back to the first one
      break
    
	TabCount++ ;<- otherwise  count it
    titl%A_Index% = %TabTitle% ;<- save it's title
	TabArray[TabCount] := TabTitle
	sListBox := sListBox . TabTitle . "|"
    
	;msgbox,0x1000,%A_LineNumber%,%vwintitl% ;<- and show the name
    
	send ^{tab} ;<- move to the next possible window
  }
  ; show how many tabs were found
  ;msgbox,0x1000,%A_LineNumber%,there are %TabCount% tabs in this chrome browser
  
;Reset variables.
TabTitle := ""
Titl1 := ""
x := 1
TabString := ""

/*
;Put all the values into a string array.
;NOTE: This is not used, leaving for learning / notes

Loop
{

if (x = (TabCount+1)) ;<- if a match we've looped back to the first one
      break

TabString := TabString . "•  " . TabArray[x] . "`r"
x++ ;<- otherwise  count it

}
;msgbox % TabString
*/

;Build the List Box Window
Gui, ChromeTabSelection:New, -Caption +LastFound +AlwaysOnTop,Double Click to select tab      ;Titled Double Click Option
Gui, ChromeTabSelection:Font, s11 Calibra                                              ;Add user instructions with formatting
Gui, ChromeTabSelection:Font, Bold
Gui, ChromeTabSelection:Add, Text, ,Double Click 
Gui, ChromeTabSelection:Font, Normal
Gui, ChromeTabSelection:Add, Text, yp x+5, to open the selected tab
Gui, ChromeTabSelection:Add, ListBox, x10 W500 h400 r%iListBoxNumRows% vguiChromeTabSelectionLB gguiChromeTabSelectionListBox multi,  %sListBox%
Gui, ChromeTabSelection:Add, Button , x230 y210 vguiSettingsHelpbClose gguiChromeTabSelection, Close                    
Gui, ChromeTabSelection:Show, w520 h250

}

Return

;----------------------------------------------------------------------------
guiChromeTabSelection:                                                       ;Close Button
{    
	
	;Reset variables.
	TabCount := 0
	FirstTitle := ""
	TabTitle := ""
	sListBox := ""	

	Gui, ChromeTabSelection:Destroy
}        
return


guiChromeTabSelectionListBox:

SetTitleMatchMode, 2

if (A_GuiEvent = "DoubleClick")
{
	Gui, Submit, Hide                                                 ;Saves the GUI Control text double clicked to the LB variable 'guiSettingsHelpLB'
	
	activateChromeTab(guiChromeTabSelectionLB)
	
	
}

return

;=====================================================================================================

;=====================================================================================================

activateChromeTab(TabTitle) {

;https://www.autohotkey.com/board/topic/118664-count-open-tabs-in-chrome-browser/#entry678852
Loop
  { 
  
	WinGetTitle, CurrentTitle, A ;<- first get the title
	
    if (CurrentTitle = TabTitle) ;<- if a match we've looped back to the first one
      break
    
	send ^{tab} ;<- move to the next possible window
  }

}

;=====================================================================================================

Last edited by submeg on 26 Feb 2022, 16:28, edited 1 time in total.
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], hobbycoder1984, MiM and 106 guests