AutoHotkey: Show the active path in the title bar extended

From TotalcmdWiki
Jump to navigation Jump to search

Based on AutoHotkey: Show the active path in the title bar (by SanskritFritz).

This version keeps important information from the original window title (TC instance number, RunAs-user, TC version).
To get more space it can shorten the program name from "Total Commander" to "TC" (optionally).

#SingleInstance, Force
#Persistent
#NoEnv
#Warn ;All, Off
	; ////////////////////////////////////////////////////////////////////////////
	; // TC_ActivePathInWinTitle.ahk V1.06 - (W) StatusQuo 2009-2025
	; // 
	; // show TC's current path in WinTitle
	; ////////////////////////////////////////////////////////////////////////////

if FileExist("TC_ActivePathInWinTitle.ico")
	Menu, Tray, Icon, TC_ActivePathInWinTitle.ico
Menu, Tray, Tip, TC - Path In WinTitle V1.06 - (W) STQ 2009-2025

boAppendToOrgTitle := 1	; *** keep RunAs-user, "TC" name and version in title
boShortenTcName    := 1	; *** replace "Total Commander" with "TC"
boShowPath         := 1 ; *** add path to title
boDebug            := 0

sTcTitleName1 := "Total Commander"
sTcTitleName2 := "TC"
sTcTitleName  := sTcTitleName1
sWinTitleR    := ""	; *** init
sChkTitle     := ""	; *** init

SetTimer TaskSched_PathInTitle, 100
Return


TaskSched_PathInTitle:
if WinActive( "ahk_class TTOTAL_CMD" )
{
	sPath := ""
	ControlGetText,    sPath, Window5	; *** TC9.22a 64bit w/o FTP-connection
	If (not (InStr(sPath, "\"))) and (not (InStr(sPath, "/")))	; If (sPath = "")
		ControlGetText, sPath, Window6	; *** TC9.50 64bit w/o FTP-connection / TC9.22a w/  FTP-Connection
	If (not (InStr(sPath, "\"))) and (not (InStr(sPath, "/")))
		ControlGetText, sPath, Window7	; *** TC9.50 64bit w/  FTP-connection
	If (not (InStr(sPath, "\"))) and (not (InStr(sPath, "/")))
		ControlGetText, sPath, TMyPanel3	; *** TC9.50 32bit w/o FTP-connection / TC9.22a regardless of FTP-connection
	If (not (InStr(sPath, "\"))) and (not (InStr(sPath, "/")))
		ControlGetText, sPath, TMyPanel4	; *** TC9.50 32bit w/  FTP-connection
	If (not (InStr(sPath, "\"))) and (not (InStr(sPath, "/")))
		ControlGetText, sPath, TMyPanel2
	;
		; *** empty path after feed to listbox => show path "..."
	If (sPath = "")
		sPath := "...\"
	If (not (InStr(sPath, "\"))) and (not (InStr(sPath, "/"))) ; *** abort, if getting path failed (e.g. ANR)
		Return
	if (boShowPath)
		sPath := SubStr(sPath, 1, StrLen(sPath) - 1)
	else
		sPath := ""
	;
	WinGetTitle, sWinTitle1
	If (not (InStr(sPath, "\"))) and (not (InStr(sPath, "/")))	; *** corrupt WinTitle	; 2do: Return on ANR: "(not responding)" / "(keine Rückmeldung)"
	{
	}
	if (sWinTitle1 = "")	; *** could not get current title - wait for next run
		Return
	sWinTitle := sWinTitle1
	
		; *** title is reset after minimizing - find out current state
	if (iTitlePos      := InStr(sWinTitle, sTcTitleName1 . " ")) ; "[n] TC - ..."
		sTcTitleName    := sTcTitleName1
	else
		if (iTitlePos   := InStr(sWinTitle, sTcTitleName2 . " ")) ; "[n] TC - ..."
			sTcTitleName := sTcTitleName2
	;if (iTitlePos < 1) and !(boAppendToOrgTitle & 2)	; *** could not get current title - wait for next run; 
	;	Return                                          ; *** do not use: boAppendToOrgTitle=2 means no TC name in title! => ignore for updating unknown title states!

		; *** shorten original program name
		;     "user - Total Commander... - regname or path"
	if (boShortenTcName)
	{
		if (iTitlePos)	; if (iTitlePos := InStr(sWinTitle, sTcTitleName . " "))	; "Total Commander " / "TC "
		{
			sWinTitle := SubStr(sWinTitle, 1, iTitlePos - 1) . sTcTitleName2 . SubStr(sWinTitle, iTitlePos + StrLen(sTcTitleName)) ; "[n] TC - ..."
			sTcTitleName := sTcTitleName2
		}
	}
	
		; *** set title right part
	if (boAppendToOrgTitle & 2) and (!StrLen(sWinTitleR))	; *** get full title
		if ( (sWinTitleR := RTrim(SubStr( sWinTitle, iEndStr := InStr(sWinTitle, " - ", false, 0) ))) = " - " . sPath ) or !(InStr(sWinTitle, " - ", false, 0))
			sWinTitleR := " "
	
		; *** set title left part
	if (boAppendToOrgTitle & 1)
	{
		if !(boShowPath) and !(InStr(sWinTitle, " - ", false, 0))
		{	; *** fix for starting with minimum title (no " - ")
			sWinTitle .= " - "
			iWinTitleEnd  := StrLen(sWinTitle)
		}
		else if (iWinTitleEnd := InStr(sWinTitle, sTcTitleName . " ", true))	; *** "Total Commander " / "TC "
			iWinTitleEnd  := InStr(sWinTitle, " - ", false, iWinTitleEnd) + 2	; *** end of left part = program name + version
		;
		if (boShowPath)
			sNewTitle := RTrim((sWinTitleL := SubStr(sWinTitle, 1, iWinTitleEnd + 0)) . sPath . sWinTitleR)
		else
			sNewTitle := RTrim((sWinTitleL := SubStr(sWinTitle, 1, iWinTitleEnd - 3)) . sWinTitleR)
	} else {	; *** set title to path only
		if (boShowPath)
			sNewTitle := RTrim(sPath . sWinTitleR)
		else
			sNewTitle := "TC"
	}

	if (sWinTitle1 != sNewTitle) and (StrLen(sNewTitle) > 2)
	{
		IfWinActive, ahk_class TTOTAL_CMD	; *** prevent affecting other windows (e.g. after minimizing)
		{
				; *** empty path after feed to listbox => show path "..."
			If (not (InStr(sNewTitle, "\"))) and (not (InStr(sNewTitle, "/"))) and (not (InStr(sNewTitle, "..."))) ; *** no path in new WinTitle? Abort, cannot do anything useful!
				Return
				; *** title may get set to empty while waiting for slow storage driver (USB/...)? => try twice w/ high priority
			Process, priority, , AboveNormal  ; Have the script set itself to high(er) priority
			Loop, 100	; *** watchdog: retry for max 10s
			{
				;WinSetTitle, ahk_class TTOTAL_CMD,, %sNewTitle%
				WinSetTitle, %sNewTitle%
				WinGetTitle, sChkTitle
				if (sChkTitle = sNewTitle)
					Break
				Sleep, 100
			} ;Until (sChkTitle = sNewTitle)
			Process, priority, , Normal  ; script priority back to normal
		}
	}
}
Return 


Back to AutoHotkey