Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Find any tab by name in any firefox window (fast re-find)


  • Please log in to reply
14 replies to this topic
harryman
  • Members
  • 27 posts
  • Last active: Mar 09 2011 10:20 PM
  • Joined: 20 Jul 2007
See comments for usage. Could be cleaner I suppose.

I'd be interested to see if anyone has a solution for multiple tabs with the same name in a firefox window limitation. Also if someone has a reason why I had to put in the code to retry activating a window (see OutputDebug, ERROR!).

Thanks to the person who posted how to iterate over firefox tabs.

-harryman

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Find something in a firefox tab
#5::
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
TabToFind = woot
; CachedMantisWindowId - Gobal var - cache off the id of the last one found, 
;     so we can try to find it quicker next time
if (FindInAnyFirefoxTab(TabToFind, CachedMantisWindowId))
{
}
return


FindTabInActiveFirefox(TabName, SleepForTabSwitch=400)
{
   ; This function assumes that the Acvite window is a firefox window.
   ;
   ; A large delay like 400 seems to work best when heavy processing
   ; is going on.
   ; 
   ; Returns true if tab is found, false if not found
   ;
   ; Limitations:
   ;
   ; Two tabs that have the same name will halt the tab search for that firefox
   ; window.  When looping over firefox tabs, it remembers the first tab name
   ; and uses that to determine when its finished looping.
   ;
   ; The delay value can be tweaky.  A loop with a small sleep might be better but
   ; given issues I had with FindInAnyFirefoxTab, I'll wait a bit.
   ;

	Settitlematchmode, 2
	WinGetActiveTitle, StartingTitle
	loop
	{
	   ifWinActive, %TabName%
      {
         OutputDebug, FOUND!
	      return true
      }
	   else
      {
	      Send {Control down}{Tab}{Control Up}
         }

	   Sleep SleepForTabSwitch
      
      WinGetActiveTitle, CurrentTabTitle
      OutputDebug, Current Tab is %CurrentTabTitle%
	   If (CurrentTabTitle == StartingTitle)
      {
         OutputDebug, BREAK! %CurrentTabTitle% %StartingTitle%
	      break
      }
	         
	}   
   
   return false
}

FindInAnyFirefoxTab(TabToFind, ByRef ReturnFoundId)
{
   ; 
   ; Iterate over all firefox windows.  Within each window iterate over
   ; each tab.
   ; 
   ; With lots of tabs and windows open this process can take a few
   ; seconds.  In order to optimize you can save (cache) the last found window id
   ; and it will jump to that one first to find it much quicker.  So the 
   ; first find might be slow.  All other finds will be fast.
   ; 
   ; Returns true if tab is found, false if not found
   ;
   ; Limitations:
   ;
   ; Two tabs that have the same name will halt the tab search for that firefox
   ; window.  When looping over firefox tabs, it remembers the first tab name
   ; and uses that to determine when its finished looping.
   ;
   
   SetTitleMatchMode 2

   ; First see if it is the currently active tab on any firefox windows
   IfWinExist %TabToFind%
   {
   	WinActivate
      return true ;found without looping
   }

   ; Try the last known window id to be good
   IfWinExist ahk_id %ReturnFoundId%
   {
      ;OutputDebug, using cache value %ReturnFoundId%
   	WinActivate
      WinWaitActive
      if (FindTabInActiveFirefox(TabToFind, 50))
      {
         return true ;found with cached value
      }
   }

   /* ; just some debug stuff 
   Loop, %id%
   {
      StringTrimRight, this_id, id%a_index%, 0
      WinGetTitle, this_title, ahk_id %this_id%
      OutputDebug, PreTest ahk_id %this_id% id%a_index%, %this_title%
   }
   */

   ; Loop through all firefox windows and all tabs
   WinGet, id, list, Firefox, , Program Manager
   Loop, %id%
   {
      StringTrimRight, this_id, id%a_index%, 0
      WinActivate, ahk_id %this_id%
      ; WinGetClass, this_class, ahk_id %this_id%
      WinGetTitle, this_title, ahk_id %this_id%
      WinWaitActive, ahk_id %this_id%,,2
      if ErrorLevel
      {
         OutputDebug, WinWaitActive FAILED.
      }
      ; The previous WinWaitActive seems to fail sometimes.  
      ; The following fix does a couple of retrys and seems to mostly work, but slows things down
      ; when hit a bit.  
      TryCount = 0
      Loop
      {
      	WinGetActiveTitle, StartingTitle
         if (StartingTitle <> this_title)
         {
            OutputDebug, ERROR! Trying to get %this_title%, GOT %StartingTitle%
            WinActivate, ahk_id %this_id%
            WinWaitActive, ahk_id %this_id%,,2
            ; I've never seen it take more than one try, but limit to 3
            TryCount++
            if (TryCount >= 3)
            {
               ;failed
               return false
            }
         }
         else
         {
            ; title matches - lets stop looping and continue on
            break
         }
      }
      ; OutputDebug, This is a test ahk_id %this_id% id%a_index%, Start Title is %StartingTitle%
      if (FindTabInActiveFirefox(TabToFind))
      {
         ReturnFoundId = %this_id%
         return true
      }
   }
   
   ; OutputDebug, Done
   return false
}


Walkman_
  • Members
  • 1 posts
  • Last active: Jan 11 2013 02:56 PM
  • Joined: 08 Jun 2009
You are a genius, thank you very much !! :D

Gnatp
  • Members
  • 1 posts
  • Last active: Apr 02 2011 03:53 PM
  • Joined: 02 Apr 2011
Great script...thank you!

bruno
  • Members
  • 635 posts
  • Last active: Nov 04 2015 02:26 PM
  • Joined: 07 Mar 2011
Can the tab name be partial or full? For example, in the case of [Pandora Radio - Listen to Free Internet Radio, Find New Music - Mozilla Firefox], is this supposed to be the tab name, or just Pandora, etc.? :?:

bruno
  • Members
  • 635 posts
  • Last active: Nov 04 2015 02:26 PM
  • Joined: 07 Mar 2011
When I use the following script, it says, "Error: Call to nonexistent function." :roll:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Find something in a firefox tab
#5::
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
TabToFind = woot
; CachedMantisWindowId - Gobal var - cache off the id of the last one found,
; so we can try to find it quicker next time
; if (FindInAnyFirefoxTab(TabToFind, CachedMantisWindowId))
{
}
return
Can you provided a specific example? :lol:

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
@bruno: you copied the example on how to use it (fist code block in the first post) but you didn't copy the actual functions posted below them. Either copy them in your script or #include it. That should make it work.

bruno
  • Members
  • 635 posts
  • Last active: Nov 04 2015 02:26 PM
  • Joined: 07 Mar 2011
I tried some and it never worked... Could you give me one specific, complete example (first script + actual function) that works out of the box (batteries included [:lol:])? Thanks!

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
- Copy the two blocks of code into one file
- Change
TabToFind = woot
to
TabToFind = Find any tab ; partial window of this forum thread, open a few other tabs in your FF browser
- Run the script, and press winkey-5 and it should just work (does for me anyway). It seems you can pass on the ahk_ID of firefox process you want to check to make it more safe in case you have multiple programs with the same window title and/or multiple firefox processes (running multiple copies)

bruno
  • Members
  • 635 posts
  • Last active: Nov 04 2015 02:26 PM
  • Joined: 07 Mar 2011
Thanks! It seems to be working great now. Posted Image I am sure I can use this in one of my projects! PS: I also noticed the beauty and power of this script is finding a tab even if the focus is in another application, such as MS Word, Outlook, etc. In other words, the focus doesn't have to be in FF. :idea: PS2: Works great, generally, but at times, seems buggy. :lol:
; http://www.autohotkey.com/forum/viewtopic.php?t=21491
; Find any tab by name in any Firefox window (fast re-find)

; Part I

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Find something in a Firefox tab
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#5::

TabToFind = Find any tab ; Partial name is OK
; CachedMantisWindowId - Gobal var - cache off the ID of the 
; last one found, so we can try to find it quicker next time

if (FindInAnyFirefoxTab(TabToFind, CachedMantisWindowId))
{

}
return

; Part II

FindTabInActiveFirefox(TabName, SleepForTabSwitch=400)
{
   ; This function assumes that the Acvite window is a Firefox window.
   ;
   ; A large delay like 400 seems to work best when heavy processing
   ; is going on.
   ;
   ; Returns true if tab is found, false if not found.
   ;
   ; Limitations:
   ;
   ; Two tabs that have the same name will halt the tab search for that Firefox
   ; window. When looping over Firefox tabs, it remembers the first tab name
   ; and uses that to determine when its finished looping.
   ;
   ; The delay value can be tweaky. A loop with a small sleep might be better but
   ; given issues I had with FindInAnyFirefoxTab, I'll wait a bit.

   Settitlematchmode, 2
   WinGetActiveTitle, StartingTitle
   loop
   {
      ifWinActive, %TabName%
      {
         OutputDebug, FOUND!
         return true
      }
      else
      {
         Send {Control down}{Tab}{Control Up}
         }

      Sleep SleepForTabSwitch
     
      WinGetActiveTitle, CurrentTabTitle
      OutputDebug, Current Tab is %CurrentTabTitle%
      If (CurrentTabTitle == StartingTitle)
      {
         OutputDebug, BREAK! %CurrentTabTitle% %StartingTitle%
         break
      }
            
   }   
   
   return false
}

FindInAnyFirefoxTab(TabToFind, ByRef ReturnFoundId)
{
   ; Iterate over all Firefox windows. Within each window iterate over
   ; each tab.
   ;
   ; With lots of tabs and windows open this process can take a few
   ; seconds.  In order to optimize you can save (cache) the last found window ID
   ; and it will jump to that one first to find it much quicker. So the
   ; first find might be slow. All other finds will be fast.
   ;
   ; Returns true if tab is found, false if not found.
   ;
   ; Limitations:
   ;
   ; Two tabs that have the same name will halt the tab search for that Firefox
   ; window. When looping over Firefox tabs, it remembers the first tab name
   ; and uses that to determine when its finished looping.
   
   SetTitleMatchMode 2

   ; First see if it is the currently active tab on any Firefox windows.
   IfWinExist %TabToFind%
   {
      WinActivate
      return true ; Found without looping.
   }

   ; Try the last known window id to be good.
   IfWinExist ahk_id %ReturnFoundId%
   {
      ; OutputDebug, using cache value %ReturnFoundId%
      WinActivate
      WinWaitActive
      if (FindTabInActiveFirefox(TabToFind, 50))
      {
         return true ; Found with cached value.
      }
   }

   /* ; Just some debug stuff...
   Loop, %id%
   {
      StringTrimRight, this_id, id%a_index%, 0
      WinGetTitle, this_title, ahk_id %this_id%
      OutputDebug, PreTest ahk_id %this_id% id%a_index%, %this_title%
   }
   */

   ; Loop through all Firefox windows and all tabs.
   WinGet, id, list, Firefox, , Program Manager
   Loop, %id%
   {
      StringTrimRight, this_id, id%a_index%, 0
      WinActivate, ahk_id %this_id%
      ; WinGetClass, this_class, ahk_id %this_id%
      WinGetTitle, this_title, ahk_id %this_id%
      WinWaitActive, ahk_id %this_id%,,2
      if ErrorLevel
      {
         OutputDebug, WinWaitActive FAILED.
      }
      ; The previous WinWaitActive seems to fail sometimes. 
      ; The following fix does a couple of retrys and seems to mostly work, but slows things down
      ; when hit a bit. 
      TryCount = 0
      Loop
      {
         WinGetActiveTitle, StartingTitle
         if (StartingTitle <> this_title)
         {
            OutputDebug, ERROR! Trying to get %this_title%, GOT %StartingTitle%
            WinActivate, ahk_id %this_id%
            WinWaitActive, ahk_id %this_id%,,2
            ; I've never seen it take more than one try, but limit to 3
            TryCount++
            if (TryCount >= 3)
            {
               ; Failed!
               return false
            }
         }
         else
         {
            ; Title matches - lets stop looping and continue on.
            break
         }
      }
      ; OutputDebug, This is a test ahk_id %this_id% id%a_index%, Start Title is %StartingTitle%
      if (FindTabInActiveFirefox(TabToFind))
      {
         ReturnFoundId = %this_id%
         return true
      }
   }
   
   ; OutputDebug, Done!
   return false
}


bruno
  • Members
  • 635 posts
  • Last active: Nov 04 2015 02:26 PM
  • Joined: 07 Mar 2011
I changed the tab name, but it still goes to the previously-designated tab name. :roll: What gives? Is it a cache issue? :lol: It also fails with "certain" tabs (pages)! :shock:

bruno
  • Members
  • 635 posts
  • Last active: Nov 04 2015 02:26 PM
  • Joined: 07 Mar 2011
Believe it or not, I have found some applications for this code, but the problem is that the code is not as robust as it could be. Any updates or improvements would be welcome! PS: I waited exactly 3 weeks for a reply to my previous post. :lol:

Asking One
  • Guests
  • Last active:
  • Joined: --
Can this script work with 2 hotkeys (^#0 and ^#9, for example) for 2 different tabs (all in one script)? Currently, it only works with one tab (Pandora), even though when I disable it (Pandora) and enable the other tab (Yahoo!). The script always behaves as if it is stuck on Pandora tab (permanently) and refuses to switch to any other tab (such as Yahoo!)? :lol:
; http://www.autohotkey.com/forum/viewtopic.php?t=21491
; Find any tab by name in any Firefox window (fast re-find)

; Part I

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Find something in a Firefox tab
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

^#0:: ; Windows+0 = Pandora

; TabToFind = Fatwallet ; Partial name is OK (test case)
TabToFind = Pandora ; Partial name is OK
; TabToFind = FreeAdvice ; Partial name is OK
; CachedMantisWindowID - Gobal var - cache off the ID of the 
; last one found, so we can try to find it quicker next time

if (FindInAnyFirefoxTab(TabToFind, CachedMantisWindowID))
{
}
Return

^#9:: ; Windows+9 = Yahoo!
TabToFind = Yahoo! ; Partial name is OK

if (FindInAnyFirefoxTab(TabToFind, CachedMantisWindowID))
{
}
Return

; Part II

FindTabInActiveFirefox(TabName, SleepForTabSwitch=400)
{

   ; This function assumes that the Acvite window is a Firefox window.
   ;
   ; A large delay like 400 seems to work best when heavy processing
   ; is going on.
   ;
   ; Returns true if tab is found, false if not found.
   ;
   ; Limitations:
   ;
   ; Two tabs that have the same name will halt the tab search for that
   ; Firefox window. When looping over Firefox tabs, it remembers the
   ; first tab name and uses that to determine when its finished 
   ; looping.
   ;
   ; The delay value can be tweaky. A loop with a small sleep might be 
   ; better but given issues I had with FindInAnyFirefoxTab, I'll wait 
   ; a bit.

   Settitlematchmode, 2
   WinGetActiveTitle, StartingTitle
   loop
   {
      ifWinActive, %TabName%
      {
         OutputDebug, FOUND!
         Return true
      }
      else
      {
         Send {Control down}{Tab}{Control Up}
      }

      Sleep SleepForTabSwitch
     
      WinGetActiveTitle, CurrentTabTitle
      OutputDebug, Current Tab is %CurrentTabTitle%
      If (CurrentTabTitle == StartingTitle)
      {
         OutputDebug, BREAK! %CurrentTabTitle% %StartingTitle%
         break
      }
            
   }   
   
   Return false
}

FindInAnyFirefoxTab(TabToFind, ByRef ReturnFoundID)
{

   ; Iterate over all Firefox windows. Within each window iterate over
   ; each tab.
   ;
   ; With lots of tabs and windows open this process can take a few
   ; seconds. In order to optimize you can save (cache) the last found
   ; window ID and it will jump to that one first to find it much 
   ; quicker. So the first find might be slow. All other finds will be 
   ; fast.
   ;
   ; Returns true if tab is found, false if not found.
   ;
   ; Limitations:
   ;
   ; Two tabs that have the same name will halt the tab search for that
   ; Firefox window. When looping over Firefox tabs, it remembers the
   ; first tab name and uses that to determine when its finished 
   ; looping.
   
   SetTitleMatchMode 2

   ; First see if it is the currently active tab on any Firefox 
   ; windows.

   IfWinExist %TabToFind%
   {
      WinActivate
      Return true ; Found without looping.
   }

   ; Try the last known window ID to be good.

   IfWinExist AHK_ID %ReturnFoundID%
   {

      ; OutputDebug, using cache value %ReturnFoundID%

      WinActivate
      WinWaitActive
      if (FindTabInActiveFirefox(TabToFind, 50))
      {
         Return true ; Found with cached value.
      }
   }

   /* ; Just some debug stuff...
   Loop, %ID%
   {
      StringTrimRight, This_ID, ID%A_Index%, 0
      WinGetTitle, This_Title, AHK_ID %This_ID%
      OutputDebug, PreTest AHK_ID %This_ID% ID%A_Index%, %This_Title%
   }
   */

   ; Loop through all Firefox windows and all tabs.

   WinGet, ID, list, Firefox, , Program Manager
   Loop, %ID%
   {
      StringTrimRight, This_ID, ID%A_Index%, 0
      WinActivate, AHK_ID %This_ID%
      ; WinGetClass, This_Class, AHK_ID %This_ID%
      WinGetTitle, This_Title, AHK_ID %This_ID%
      WinWaitActive, AHK_ID %This_ID%,,2
      if ErrorLevel
      {
         OutputDebug, WinWaitActive FAILED.
      }

      ; The previous WinWaitActive seems to fail sometimes. 
      ; The following fix does a couple of retrys and seems to mostly
      ; work, but slows things down when hit a bit.

      TryCount = 0
      Loop
      {
         WinGetActiveTitle, StartingTitle
         if (StartingTitle <> This_Title)
         {
            OutputDebug, ERROR! Trying to get %This_Title%, GOT %StartingTitle%
            WinActivate, AHK_ID %This_ID%
            WinWaitActive, AHK_ID %This_ID%,,2

            ; I've never seen it take more than one try, but limit to 3

            TryCount++
            if (TryCount >= 3)
            {

               ; Failed!

               Return false
            }
         }
         else
         {

            ; Title matches - lets stop looping and continue on.

            break
         }
      }

      ; OutputDebug, This is a test AHK_ID %This_ID% ID%A_Index%, Start
      ; Title is %StartingTitle%

      if (FindTabInActiveFirefox(TabToFind))
      {
         ReturnFoundId = %This_ID%
         Return true
      }
   }

   ; OutputDebug, Done!

   Return false
}


SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
Your script works for me, doesn't the pandora site use Flash? if so it will overtake AutoHotkey hotkeys e.g. as long as has focus you can't press a hotkey because the Flash "grabs" them and not AutoHotkey.

Senethril
  • Members
  • 2 posts
  • Last active: Nov 22 2018 12:01 AM
  • Joined: 25 Feb 2014

Not sure if all improvements can be included in my trick, tell me if i am wrong.

 

There is a great hidden shortcut in urlbar in firefox:

 

Try % + space + what u search in any tabs and ff windows

 

Works only for titles.

 

:)



Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

Not sure if all improvements can be included in my trick, tell me if i am wrong.

 

There is a great hidden shortcut in urlbar in firefox:

 

Try % + space + what u search in any tabs and ff windows

 

Works only for titles.

 

:)

could you please elaborate a bit more. i can't quite follow. thanks ~ :D