Script to pin a tab in Firefox Topic is solved

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

Script to pin a tab in Firefox

17 Sep 2018, 06:44

Does anyone know of an AHK script with a keyboard shortcut to pin a tab in Firefox ?
User avatar
TheDewd
Posts: 1507
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Script to pin a tab in Firefox

25 Sep 2018, 15:46

Mozilla Firefox doesn't have any hotkeys for pinning tabs.

I would suggest that you install an add-on for Firefox that adds hotkeys: https://addons.mozilla.org/en-US/firefo ... unpin-tab/
DutchPete

Re: Script to pin a tab in Firefox

26 Sep 2018, 03:15

TheDewd wrote:Mozilla Firefox doesn't have any hotkeys for pinning tabs.

I would suggest that you install an add-on for Firefox that adds hotkeys: https://addons.mozilla.org/en-US/firefo ... unpin-tab/
I am fully aware Firefox does not have a hotkey to pin tabs, I am also fully aware of the pinunpin add-on. But I wanted to avoid having to install yet another add-on, which is why I asked on this forum if anyone had an AHK script to pin & unpin a tab. If I wanted something else I would not have asked the question on this forum.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Script to pin a tab in Firefox

26 Sep 2018, 11:13

Tested on FireFox v62.0.2 (64-bit), require acc.ahk.

Code: Select all

F1::FF_PinUnpin() ; Press F1 to pin/unpin

FF_PinUnpin() {
	FF_PinUnpin.DoIt()
}

class FF_PinUnpin
{
	static ROLE_SYSTEM_PAGETABLIST := 0x0000003C
	static ROLE_SYSTEM_APPLICATION := 0x0000000E
	static ROLE_SYSTEM_TOOLBAR     := 0x00000016
	static ROLE_SYSTEM_PAGETAB     := 0x00000025

	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) = this.ROLE_SYSTEM_TOOLBAR )
			{
				o := Acc_Children(child)
				if ( o.1.accRole(0) = this.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

			children := Acc_Children(oTabList)
			Loop, % len := children.MaxIndex()
			{
				child := children[len-A_Index+1]
				pos := Acc_Location(child)
				PixelGetColor, color, pos.x+pos.w//2, pos.y+4
				if (A_Index = 1)
					inactiveColor := color
				else if (color != inactiveColor)
				{
					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."
	}
}
Last edited by tmplinshi on 26 Sep 2018, 12:33, edited 3 times in total.
DutchPete

Re: Script to pin a tab in Firefox

26 Sep 2018, 11:38

@tmplinshi: thanks for your reply.
Excuse my ignorance, I have a couple of questions:
1. acc.ahk is another script. Should I add it to my scripts folder where your suggested script will also reside?
2. does acc.ahk need to be activate separately from your script?
3. I noticeyour script refers to pinunpin. I assume that does not mean the Firefox pinunpin add-on needs to be installed, otherwise there is no need for that script, so please explain/confirm
4. I am running Firefox 62 (64-bit): does that mean your script won't work or is it a question of trying it out?
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Script to pin a tab in Firefox

26 Sep 2018, 11:56

1. You can add #include acc.ahk, or save acc.ahk to lib folder without needed to include.
2. No
3. No. This script does not need a FireFox plugin.
4. I just updated the script to be more compatible, please try the new code.
User avatar
TheDewd
Posts: 1507
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Script to pin a tab in Firefox  Topic is solved

26 Sep 2018, 12:04

tmplinshi wrote:4. I just updated the script to be more compatible, please try the new code.
The pin functionality works here (v62.0.2 64-bit), however the unpin only works on the last tab.

For example, pressing the hotkey will continue to pin all tabs until the last tab is pinned, and then the hotkey will only toggle pin/unpin for the last tab in the tab-bar.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Script to pin a tab in Firefox

26 Sep 2018, 12:22

TheDewd wrote:
tmplinshi wrote:4. I just updated the script to be more compatible, please try the new code.
The pin functionality works here (v62.0.2 64-bit), however the unpin only works on the last tab.

For example, pressing the hotkey will continue to pin all tabs until the last tab is pinned, and then the hotkey will only toggle pin/unpin for the last tab in the tab-bar.
Did you noticed some tab is in hover state after pin? If that's true then it will cause the problem you mentioned. I've updated the script with PostMessage, WM_MOUSEMOVE := 0x200, 0, 0,, % "ahk_class MozillaWindowClass", it works for me.
DutchPete

Re: Script to pin a tab in Firefox

26 Sep 2018, 12:36

@tmplinshi:
* do I understand you correctly that your script pins all tabs? I would like to be able to pin tabs individually because I don't want to pin all my open tabs
* unpinning only applies to the last tab? And what happens if there is only 1 tab pinned?
* where is the updated code? in your original answer above?
User avatar
TheDewd
Posts: 1507
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Script to pin a tab in Firefox

26 Sep 2018, 12:37

The behavior is the same after your update.

The hotkey will move the selected tab to the far left of the window's tab-bar, and pin it...

Each hotkey press will move another tab to the left, and pin it.

Selecting a pinned tab and pressing the hotkey does not unpin it, but instead just pins the next normal tab.

Only when ALL tabs are pinned will the hotkey actually unpin a tab, which only applies to the last pinned tab.

Firefox 62.0.2 (64-bit)
Windows 7 (64-bit)

Let me know if there's anything I can do to help. Any information that I can provide related to my setup.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Script to pin a tab in Firefox

26 Sep 2018, 12:44

@DutchPete If it's working correctly, it will only pin/unpin the current tab.
@TheDewd hmm.. I can't reproduce the issue. Maybe you can have some debug with the script :). Are you using the default firefox theme?
User avatar
TheDewd
Posts: 1507
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Script to pin a tab in Firefox

26 Sep 2018, 12:48

tmplinshi wrote:@TheDewd hmm.. I can't reproduce the issue. Maybe you can have some debug with the script :). Are you using the default firefox theme?
Thanks! I'll post any fix that I find.
TabPos.x & TabPos.y only find the last tab instead of the current tab according to my debugging.
User avatar
TheDewd
Posts: 1507
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Script to pin a tab in Firefox

26 Sep 2018, 13:07

That explains my issue. On Windows 7, the background of the "new tab" button is not a solid color. It's using the Aero effect.

Image
Attachments
Untitled.png
(14.17 KiB) Not downloaded yet
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Script to pin a tab in Firefox

26 Sep 2018, 13:33

This version uses the top blue line to detect the active 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 = 0xFF840A)
				{
					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."
	}
}
User avatar
TheDewd
Posts: 1507
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Script to pin a tab in Firefox

26 Sep 2018, 13:55

@tmplinshi,

Perfect!
DutchPete

Re: Script to pin a tab in Firefox

27 Sep 2018, 05:41

pinunpin.jpg
(77.67 KiB) Downloaded 71 times
@tmplinshi: many thanks for your help. It does not quite work.
let's say I have 5 tabs open, pressing F1 will pin tab 1. Staying on tab 1, then pressing F1 will not unpin tab 1 but pin tab 2, then tab3, then tab 4. After that pressing F1 will unpin tab 4, pressing F1 again will pin tab 4. Even if I go to tab 1 (pinned), pressing F1 will only affect tab 4.

I tried your last code, above which you told TheDrewd: "This version uses the top blue line to detect the active tab:"
That does not work at all, AHK throws up this message: https://imgur.com/gZjx5Cq
Last edited by DutchPete on 27 Sep 2018, 06:18, edited 4 times in total.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Script to pin a tab in Firefox

27 Sep 2018, 05:57

@DutchPete What firefox theme are you using? Could you upload a screenshot of the tab like TheDewd did?
DutchPete

Re: Script to pin a tab in Firefox

27 Sep 2018, 06:15

I am using the Firefox Quantum Nightly theme.
firefoxtabs.jpg
(63.39 KiB) Not downloaded yet
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: Script to pin a tab in Firefox

27 Sep 2018, 06:25

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

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 159 guests