Delete tab in Firefox using ahk

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Delete tab in Firefox using ahk

Post by teadrinker » 29 Nov 2021, 15:13

Try this:

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)
   try playing := InStr(AccTab.accName(2), "playing")
   try pip     := InStr(AccTab.accName(2), "Picture-in-Picture") 
   MsgBox, % AccTab.accName(0) . "`nplaying: " . !!playing . "`npip: " . !!pip
}

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)
}

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

Re: Delete tab in Firefox using ahk

Post by smbs » 29 Nov 2021, 15:45

@teadrinker
Brilliant!!
I will try and incorporate into your original script as to my needs :D
Many thanx
:thumbup:

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

Re: Delete tab in Firefox using ahk

Post by smbs » 02 Dec 2021, 11:20

@teadrinker
using code below in order to delete all tabs containing "CNN" say, 5 tabs as an example, the code deletes all tabs until only 2 tabs remain and then throws an error
Your help would be much appreciated!
thanx

Code: Select all

wordToClose := "CNN"
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
   }

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

Re: Delete tab in Firefox using ahk

Post by teadrinker » 02 Dec 2021, 12:20

Try adding Sleep in the Loop.

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

Re: Delete tab in Firefox using ahk

Post by smbs » 02 Dec 2021, 12:56

not luck-- pls see below
error.png
error.png (20.8 KiB) Viewed 323 times
pls see error note

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

Re: Delete tab in Firefox using ahk

Post by teadrinker » 02 Dec 2021, 13:13

This should work:

Code: Select all

wordToClose := "CNN"
arr := []
Loop % AccTabList.accChildCount() - 1 {
   AccTab := AccTabList.accChild(A_Index)
   if InStr(AccTab.accName(0), wordToClose)
      arr.Push(AccTab)
}
for k, v in arr {
   v.accDoDefaultAction(0)
   Send ^w
}

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

Re: Delete tab in Firefox using ahk

Post by smbs » 02 Dec 2021, 13:28

@teadrinker
Works like a charm
Many thanx
:D :D

Post Reply

Return to “Ask for Help (v1)”