AutoHotkey: Automatic directory-specific configuration / actions

From TotalcmdWiki
Jump to: navigation, search

Automatic script

This script periodically "looks" into Total Commander to see what the current directory is and based on that information it sends user-defined commands to TC. These commands can be whatever you desire them to be, starting from sorting by date, invoking a custom columns view, to starting your media player.


The first part of the script defines the interval at which AutoHotkey checks the path in the active panel of Total Commander. The default is 1000 milliseconds (1 second). Smaller values increase reaction speed, however, they might slow things down if you are just passing through the directory. Larger values will make the action trigger later.


Place this at the beginning of the script:

#Persistent
SetTimer, TCDirSpecificActions, 1000
return


The second part of the script defines what actions belong to what directories, and at the end you define the "default" action, that is what to do when you are in neither of the "special" directories.


Place this anywhere in the script:

TCDirSpecificActions:
	IfWinActive, ahk_class TTOTAL_CMD
	{
		WinGetText, Text, A
		Loop, Parse, Text, `n
		{
			; The path is not always on third line - sometimes it occurs on second line. 
			; But it always ends with ">" ....
			IfInString A_LoopField , >
			{
				If A_Index <= 3
				{
					StringTrimRight, Dir, A_LoopField, 2
					Break
				}
			}
 		}


		If Dir <> %PreviousDir%
		{

 			; This is the first section
			; On the line below you define what directory this section is triggered by
			; (directory names with spaces must be typed without quotes)
			If Dir = c:\Music
			{
				; Value "271" means activate custom columns view #1
				; (these values correspond to the values in your totalcmd.inc)
				PostMessage, 1075, 271
				PreviousDir = %Dir%
				return
			}

			; This is the second section
			; If you want actions to be done recursively,
			; for instance for "c:\Images and Photos" and all its subdirs
			; use the IfInString format
			IfInString, Dir, c:\Images and Photos
			{
				; Value "272" means activate custom columns view #2
				PostMessage, 1075, 272
				PreviousDir = %Dir%
				return
			}

			; This is the third section
			If Dir = c:\Downloads
			{
				; Value "324" means sort by date
				PostMessage, 1075, 324
				PreviousDir = %Dir%
				return
			}

			; This is the DEFAULT section
			; Here you must place all the actions to get back your "normal" view
			If PreviousDir <> DEFAULT
			{
				; Activate Brief view
				PostMessage, 1075, 301
				; Sort by Name
				PostMessage, 1075, 321
				PreviousDir = DEFAULT
			}
		}
 	}
return


Hotkey-triggered script

This script is only triggered when a hotkey is pressed (left Winkey and left shift at the same time in this example). This means that the action (such as changing the current view) is only triggered when the hotkey is pressed. The advantage of this approach is that the actions are only performed when you want them. Useful if you find the automatic actions annoying.

For detailed explanations of what each command does, please see the script above.

$<#LShift::
IfWinActive, ahk_class TTOTAL_CMD
{
	WinGetText, Text, A
	Loop, Parse, Text, `n
	{
		IfInString A_LoopField , >
		{
			If A_Index <= 3
			{
				StringTrimRight, Dir, A_LoopField, 2
				Break
			}
		}
	}


	IfInString, Dir, c:\Music
	{
		; Activate Custom columns view #2
		PostMessage, 1075, 272
		return
	}

	If Dir = c:\Images and Photos
	{
		; Activate Custom columns view #1
		PostMessage, 1075, 271
		return
	}

	; Default - activate Brief view
	PostMessage, 1075, 301

}
else
	Send, {LWin down}{LShift}{LWin up}
return


Back to AutoHotkey