Delete tab in Firefox using ahk

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

Delete tab in Firefox using ahk

Post by smbs » 27 Nov 2021, 06:21

I want to delete specific tabs in Firefox by title contents programmatically.
Any ideas would be great
Many thanx

User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Delete tab in Firefox using ahk

Post by mikeyww » 27 Nov 2021, 08:40

I never tried this library, but it may be useful. viewtopic.php?p=294316#p294316

smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

Re: Delete tab in Firefox using ahk

Post by smbs » 27 Nov 2021, 08:50

Thanx had a look at that post earlier
It seems way beyond me as it seems very version dependent
Will give it another try
Again many thanx

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Delete tab in Firefox using ahk

Post by teadrinker » 27 Nov 2021, 09:09

This is for you to start:

Code: Select all

if !AccFF := AccObjectFromWindow( hWnd := WinExist("ahk_class MozillaWindowClass") )
   throw "Failed to get accessible object from FF"
AccTabList := SearchElement(AccFF, {Role: ROLE_SYSTEM_PAGETABLIST := 0x3C})
WinActivate
Loop % AccTabList.accChildCount() - 1 {
   AccTab := AccTabList.accChild(A_Index)
   AccTab.accDoDefaultAction(0)
   MsgBox, % AccTab.accName(0)
}

SearchElement(parentElement, params)
{
   found := true
   for k, v in params {
      try {
         if (k = "State")
            (!(parentElement.accState(0)    & v) && found := false)
         else if (k ~= "^(Name|Value)$")
            (!(parentElement["acc" . k](0) ~= v) && found := false)
         else if (k = "ChildCount")
            (parentElement["acc" . k]      != v  && found := false)
         else
            (parentElement["acc" . k](0)   != v  && found := false)
      }
      catch 
         found := false
   } until !found
   if found
      Return parentElement
   
   for k, v in AccChildren(parentElement)
      if obj := SearchElement(v, params)
         Return obj
}

AccObjectFromWindow(hWnd, idObject = 0) {
   static IID_IDispatch   := "{00020400-0000-0000-C000-000000000046}"
        , IID_IAccessible := "{618736E0-3C3D-11CF-810C-00AA00389B71}"
        , OBJID_NATIVEOM  := 0xFFFFFFF0, VT_DISPATCH := 9, F_OWNVALUE := 1
        , h := DllCall("LoadLibrary", "Str", "oleacc", "Ptr")
        
   VarSetCapacity(IID, 16), idObject &= 0xFFFFFFFF, AccObject := 0
   DllCall("ole32\CLSIDFromString", "Str", idObject = OBJID_NATIVEOM ? IID_IDispatch : IID_IAccessible, "Ptr", &IID)
   if DllCall("oleacc\AccessibleObjectFromWindow", "Ptr", hWnd, "UInt", idObject, "Ptr", &IID, "PtrP", pAcc) = 0
      AccObject := ComObject(VT_DISPATCH, pAcc, F_OWNVALUE)
   Return AccObject
}

AccChildren(Acc) {
   if ComObjType(Acc, "Name") != "IAccessible" {
      ErrorLevel := "Invalid IAccessible Object"
      Return
   }
   try childCount := Acc.accChildCount
   if !childCount
      Return
   Children := []
   VarSetCapacity(varChildren, childCount*(8 + A_PtrSize*2), 0)
   res := DllCall("oleacc\AccessibleChildren", "Ptr", ComObjValue(Acc), "Int", 0
                                             , "Int", childCount, "Ptr", &varChildren, "IntP", childCount)
   if (res != 0) {
      ErrorLevel := "winapi AccessibleChildren failed"
      Return
   }
   Loop % childCount  {
      offset := (A_Index - 1)*(A_PtrSize*2 + 8)
      _child := NumGet(varChildren, offset + 8)
      if NumGet(varChildren, offset) != (VT_DISPATCH := 9)
         child := _child
      else
         child := AccQuery(_child), ObjRelease(_child)
      Children.Push(child)
   }
   Return Children
}

AccQuery(Acc) {
   static IAccessible := "{618736e0-3c3d-11cf-810c-00aa00389b71}", VT_DISPATCH := 9, F_OWNVALUE := 1
   try Return ComObject(VT_DISPATCH, ComObjQuery(Acc, IAccessible), F_OWNVALUE)
}

User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Delete tab in Firefox using ahk

Post by mikeyww » 27 Nov 2021, 09:10

Another example is below. I had not seen teadrinker's earlier post.

Code: Select all

#Include d:\utils\acc\Acc.ahk    ; https://www.autohotkey.com/boards/viewtopic.php?t=40590
#Include d:\utils\firefoxLib.ahk ; https://www.autohotkey.com/boards/viewtopic.php?p=294316#p294316

closeTabs("AutoHotkey")

closeTabs(name) { ; Close Firefox tabs
 If !hWnd := WinExist("ahk_exe firefox.exe")
  Return
 tab := []
 For each, tabName in StrSplit(JEE_FirefoxGetTabNames(hWnd), "`n") {
  If !Instr(tabname, name)
   Continue
  JEE_FirefoxFocusTabByName(hWnd, tabName)
  Sleep, 500
  ControlSend, ahk_parent, {Ctrl down}w{Ctrl up}, ahk_id %hWnd%
 }
}

smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

Re: Delete tab in Firefox using ahk

Post by smbs » 28 Nov 2021, 04:41

thanx will give it a try

smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

Re: Delete tab in Firefox using ahk

Post by smbs » 28 Nov 2021, 05:48

Sorry for my ignorance
#Include d:\utils\firefoxLib.ahk ; viewtopic.php?p=294316#p294316
I cannot find firefoxlib.ahk in above board post
I saved the firefox file -first line starting with ";Firefox functions suite (tested on Firefox v69):"
as firefoxlib.ahk --Is that correct?
Last edited by smbs on 28 Nov 2021, 07:35, edited 1 time in total.

smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

Re: Delete tab in Firefox using ahk

Post by smbs » 28 Nov 2021, 06:26

@ tea drinker
Many thanx but your script is way beyond me but works great!
however once we loop thru tab names AccTab.accName(0)
I want to delete certain tabs which contain a specific word or words
How can this be done?
many thanx

User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Delete tab in Firefox using ahk

Post by mikeyww » 28 Nov 2021, 06:54

The Firefox library is the script that is posted at the other site. I happened to name it firefoxLib.ahk on my computer.

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Delete tab in Firefox using ahk

Post by teadrinker » 28 Nov 2021, 07:24

smbs wrote: How can this be done?
You need to change the Loop like this:

Code: Select all

wordToClose := "AutoHotkey"
Loop % AccTabList.accChildCount() - 1 {
   AccTab := AccTabList.accChild(A_Index)
   if InStr(AccTab.accName(0), wordToClose) {
      AccTab.accDoDefaultAction(0) ; switch to the tab containing %wordToClose%
      Send ^w                      ; close that tab
      break
   }
}

smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

Re: Delete tab in Firefox using ahk

Post by smbs » 28 Nov 2021, 07:53

@Mickeyww
I saved the firefox file -first line starting with ";Firefox functions suite (tested on Firefox v69):"
as firefoxlib.ahk --Is that correct?

smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

Re: Delete tab in Firefox using ahk

Post by smbs » 28 Nov 2021, 08:03

@teadrinker
Many thanx works great Really learnt something!
:D :D

User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Delete tab in Firefox using ahk

Post by mikeyww » 28 Nov 2021, 08:11

smbs: Yes.

smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

Re: Delete tab in Firefox using ahk

Post by smbs » 28 Nov 2021, 09:06

@mikeyww
Have you tested it? It doesn't work for me--just exits without error
using FF version 94.0.2 (64-bit)
Many thanx again

User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Delete tab in Firefox using ahk

Post by mikeyww » 28 Nov 2021, 09:41

Indeed, I tested the script that I posted before posting it.

Feel free to post your script for feedback about it.

smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

Re: Delete tab in Firefox using ahk

Post by smbs » 29 Nov 2021, 04:39

@mikeyww
Got it working
Many thanx for your help and patience!!
:D :D :D :D

smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

Re: Delete tab in Firefox using ahk

Post by smbs » 29 Nov 2021, 08:28

@mikeyww
BTW , using your script, if there is a "PIP" tab -picture in picture the tab is not found and is overlooked --might be a bug in library file
Drink tea's solution works great in all cases.
Just for your interest
Regards

User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Delete tab in Firefox using ahk

Post by mikeyww » 29 Nov 2021, 08:29

Thanks for letting me know. By all means, I'm on board with drinking tea as an approach of first choice. :) :thumbup:

smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

Re: Delete tab in Firefox using ahk

Post by smbs » 29 Nov 2021, 08:34

;) :)

smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

Re: Delete tab in Firefox using ahk

Post by smbs » 29 Nov 2021, 13:57

@teadrinker
I have an additional request if you have the patience.
As FF supports multiple PIPS therefore
In FF I would like to close only tabs which are not showing in "picture in picture" mode.
A tab in FF playing a stream has "playing" below tab name.
A tab in FF playing a stream in "Picture in picture" mode has "Picture-in-Picture" below tab name.
Is there a way to differentiate between these 2 types of tabs?
Many thanx

Post Reply

Return to “Ask for Help (v1)”