Script to pin a tab in Firefox

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
DutchPete

Re: Script to pin a tab in Firefox

27 Sep 2018, 07:12

tmplinshi wrote:That explains everything. This script only works on a solid background theme. The last code tested on firefox v62.0.2 (theme: Default/Light/Dark).
I am afraid it does not. I disabled the Nightly theme, so I am back to Default. Closed FF and relaunched.
If there is only 1 tab open that I want to pin, the script that "Unable to find active tab" message.

If I open more tabs, then F1 just pins tab 1.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Script to pin a tab in Firefox

27 Sep 2018, 08:10

Try some debugging like this:
1. Add MsgBox, % color or OutputDebug, % color after PixelGetColor.
2. Open 3 tabs in firefox, then press F1, see what colors do you get.

Image

(The debugger in the screenshot is DebugViewPP)
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Script to pin a tab in Firefox

27 Sep 2018, 14:18

tmplinshi wrote:That explains everything. This script only works on a solid background theme. The last code tested on firefox v62.0.2 (theme: Default/Light/Dark).
Here it is working with the Firefox Quantum Nightly theme. It uses a different color along the top of the tab.

Code: Select all

F1::FF_PinUnpin()

; Tested on FireFox v62.0.2 (Theme: Default/Light/Dark), Windows 10
FF_PinUnpin() {
	FF_PinUnpin.DoIt()
}

class FF_PinUnpin
{
	DoIt() {
		TabList := this.FindTabList()
		TabPos  := this.FindActiveTabPos(TabList)

		ControlClick, % "x" TabPos.x+TabPos.w//2 " y" TabPos.y+4, % "ahk_class MozillaWindowClass",, RIGHT,, NA
		WinWait, % "ahk_class MozillaDropShadowWindowClass"
		ControlSend,, % (TabPos.w < 50) ? "b" : "p"

		; Fix tab redraw problem.
		PostMessage, WM_MOUSEMOVE := 0x200, 0, 0,, % "ahk_class MozillaWindowClass"
	}

	FindTabList(WinTitle := "ahk_class MozillaWindowClass") {
		if !WinExist(WinTitle)
			throw WinTitle " does not exist."

		acc := Acc_ObjectFromWindow( WinExist() )

		for i, child in Acc_Children(acc)
		{
			if ( child.accRole(0) = 0x00000016 ) ; ROLE_SYSTEM_TOOLBAR
			{
				o := Acc_Children(child)
				if ( o.1.accRole(0) = 0x0000003C ) ; ROLE_SYSTEM_PAGETABLIST
					return o.1
			}
		}
		throw "Couldn't find TabList object"
	}

	FindActiveTabPos(oTabList) {
		if (oTabList.accChildCount = 2)
			pos := success := Acc_Location( Acc_Children(oTabList).1 )
		else
		{
			PreMode := A_CoordModePixel
			CoordMode, Pixel, Screen

			for i, child in Acc_Children(oTabList)
			{
				pos := Acc_Location(child)
				PixelGetColor, color, pos.x+pos.w//2, pos.y+1
				if (color = 0xFFFE00)
				{
					success := true
					Break
				}
			}
			CoordMode, Pixel, %PreMode%
		}

		if success {
			WinGetPos, winX, winY,,, % "ahk_class MozillaWindowClass"
			return pos, pos.x -= winX, pos.y -= winY
		}
		throw "Unable to find active tab."
	}
}
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Script to pin a tab in Firefox

28 Sep 2018, 07:18

Thanks TheDewd, that worked. I didn't see the "top line" in the DutchPete's screenshot, so I thought it's not possible.

I added more themes support:
  • Default
  • Light
  • Dark
  • Space Fantasy
  • A Light in Space
  • Firefox Quantum Nightly
  • Pastel Gradient
FF_PinUnpin
FF_PinUnpin (Faster approach using Gdip_GetPixel)
The Gdip_GetPixel approach is much faster than PixelGetColor. Speed testing on 12 tabs:
  • PixelGetColor: 0.2 seconds
  • Gdip_GetPixel: 0.03 seconds
DutchPete

Re: Script to pin a tab in Firefox

09 Oct 2018, 11:33

@tmplinshi: 1st of all I want to apologise for this late reply. Somehow I had seen the message in my Inbox, and thought you got p*ssed off with my stupid questions, particularly when I replied, perhaps a bit flippantly, to your "that explains all" comment.

Anyhow, I have installed the "more themes support" version and it works flawlessly. I had installed the "Pin Unpin Tab" extension, now I can uninstall that - I much prefer scripts. Once again, thanks a lot for your help !!!
DutchPete

Re: Script to pin a tab in Firefox

09 Oct 2018, 11:36

@TheDewd: I also want to thank you for your support. We have not communicated directly but your input for tmplinshi contributed to get my issue solved. I apologise for not replying sooner - the reason is shown in my reply to tmplinshi. Thank you !
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Script to pin a tab in Firefox

10 Oct 2018, 05:14

This might be useful to find the active tab. Cheers.
Help to find the active tab number in Firefox - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 56#p206156

I've turned the code into a function, see here:
Firefox/Chrome, get tab names/focus tab - Page 2 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 14#p243114

You're welcome to adapt the code to your style (and rename or discard the function).
Last edited by jeeswg on 08 Nov 2018, 19:25, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Script to pin a tab in Firefox

10 Oct 2018, 06:24

Wow, thank you very much jeeswg! That's quite a useful info about the accState.

@DutchPete Forget about previous scripts that using color matching, use this instead:

Code: Select all

#NoEnv
SetBatchLines, -1

F1::Firefox.PinUnpin()

class Firefox
{
	PinUnpin() {
		oTabList := this.FindTabList()
		oTab := this.FindActiveTab(oTabList)

		loc := this._ScreenToWin( Acc_Location(oTab) )

		ControlClick, % "x" loc.x+10 " y" loc.y+4,,, RIGHT,, NA
		WinWait, % "ahk_class MozillaDropShadowWindowClass"
		ControlSend,, % (loc.w < 50) ? "b" : "p"

		; Fix tab redraw problem.
		PostMessage, WM_MOUSEMOVE := 0x200, 0, 0,, % "ahk_class MozillaWindowClass"
	}

	FindTabList(WinTitle := "ahk_class MozillaWindowClass") {
		if !WinExist(WinTitle)
			throw WinTitle " does not exist."

		acc := Acc_ObjectFromWindow( WinExist() )

		for i, child in Acc_Children(acc)
		{
			if ( child.accRole(0) = 0x00000016 ) ; ROLE_SYSTEM_TOOLBAR
			{
				o := Acc_Children(child)
				if ( o.1.accRole(0) = 0x0000003C ) ; ROLE_SYSTEM_PAGETABLIST
					return o.1
			}
		}
		throw "Couldn't find TabList object"
	}

	FindActiveTab(oTabList) {
		for i, child in Acc_Children(oTabList)
		{
			if ( child.accRole(0) = 0x00000025 ) ; ROLE_SYSTEM_PAGETAB
			&& ( child.accState(0) & 0x2 ) ; STATE_SYSTEM_SELECTED := 0x2
				return child
		}
		throw "Unable to find active tab."
	}

	_ScreenToWin(oLocation) {
		WinGetPos, winX, winY
		oLocation.x -= winX
		oLocation.y -= winY
		return oLocation
	}
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww and 216 guests