Search results format lock

Overview

If you are in a folder and use the Windows Search field, the search results are usually displayed with different columns to those you were just using.

This script prevents the columns from changing, so the columns used to view the folder remain when you view the search results.

For example, you might have a music folder set to display Album and Artist tags, and Codec and Bitrate information, which you still want to see when you search within that folder. You might want something similar, but with different columns, in a pictures folder. (Since you want different columns in the search results for different places, you can't just add the columns you want to the global Search Results folder format.)

Note that the script only affects what happens when you use the Windows Search field. The separate Find panel is not affected (although the script can be edited to affect it if you wish).

Installation:

Open Preferences / Toolbars / Scripts, then download this and drag it to the list:

Search_Format_Lock.vbs.txt (3.6 KB)

Configuration:

After installing the script, select it and click Configure. You will see these options:

  • Add_Location_Column: If on (the default), the Location (Relative) column is added to the search results, so you can tell where things are if they are below the starting folder. (The option has no effect if the Location or Location (Relative) columns are already present when you search, so you won't end up with duplicate columns.)

  • Sort_Location_Column: If on (the default), the results will also be sorted by the Location (Relative) column, if the column was added according to the Add_Location_Column option. (The option has no effect if the column is not added.)

The script itself:

If you just want to use the script, the download above is easier.

The script code is reproduced here so that people looking for scripting techniques on the forum can browse the script code without having to download anything.

option explicit

' Search Format Lock
' (C) 2014 Leo Davidson
' 
' This is a script for Directory Opus.
' See http://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information.


' Called by Directory Opus to initialize the script
Function OnInit(initData)
	initData.name = "Search Format Lock"
	initData.desc = "Preserves the current columns when using the Search field instead of switching to the Search Results folder format."
	initData.copyright = "(C) 2014 Leo Davidson"
	initData.version = "1.0"
	initData.default_enable = true
	
	initData.config.Add_Location_Column = True
	initData.config.Sort_Location_Column = True
End Function


Function GetLockVarName(objTab)
	If (objTab.right) Then
		GetLockVarName = "SearchFormatLock_Right"
	Else
		GetLockVarName = "SearchFormatLock_Left"
	End If
End Function


' Called before a new folder is read in a tab
Function OnBeforeFolderChange(beforeFolderChangeData)

	OnBeforeFolderChange = False ' Allow the folder change to go ahead as normal.
	
	If (beforeFolderChangeData.initial) Then
		' Don't do anything if it's the first read of a new tab.
		Exit Function
	End If

	If (Not DOpus.FSUtil.ComparePath(beforeFolderChangeData.Path, "coll://%%Lister-Quick-Find-Results%%", "p")) Then
		' Don't do anything if we're not changing to some search results
		Exit Function
	End If

	Dim objTab
	Set objTab = beforeFolderChangeData.tab

	Dim objCmd
	Set objCmd = DOpus.CreateCommand
	objCmd.SetSourceTab(objTab)

	If (objCmd.IsSet("FORMATLOCK=Toggle")) Then
		' Don't do anything if the format lock is already on. (So we don't turn it off after changing.)
		Exit Function
	End If

	' Set a variable which tells us to undo the format lock after the folder change.
	' The format locks are per-lister, but there is a seperate one for the left and right
	' halves of a dual-display lister, so we need to make a note of which one is ours.

	objTab.Vars.Set GetLockVarName(objTab), True

	' Turn on the format lock, so whatever the format is now will stay after the folder change.
	objCmd.RunCommand "Set FORMATLOCK=On"

End Function


' Called after a new folder is read in a tab
Function OnAfterFolderChange(afterFolderChangeData)

	OnAfterFolderChange = False ' Ensure normal behaviour if the folder read failed.

	Dim objTab
	Set objTab = afterFolderChangeData.tab

	Dim objVars
	Set objVars = objTab.Vars
	
	Dim strVarName
	strVarName = GetLockVarName(objTab)
	
	If (Not objVars.Exists(strVarName)) Then
		' Variable wasn't set, which means the format lock was already turned on by the user.
		' We should leave both the lock and the format alone.
		Exit Function
	End If

	' Remove our variable.
	objVars.Delete strVarName

	Dim objCmd
	Set objCmd = DOpus.CreateCommand
	objCmd.SetSourceTab(objTab)
	
	' Turn off the format lock again.
	objCmd.RunCommand "Set FORMATLOCK=Off"

	If (afterFolderChangeData.result And Script.config.Add_Location_Column) Then

		' The folder read succeeded (i.e. we are now in a Search Results folder)
		' and we are configured to add the Location column.

		If ((Not objCmd.IsSet("COLUMNSADD=pathrel")) And (Not objCmd.IsSet("COLUMNSADD=path"))) Then

			' Neither Location column is present already.

			' Add Location (Relative) as the first column.
			objCmd.RunCommand "Set COLUMNSADD=pathrel(0)"
			
			If (Script.config.Sort_Location_Column) Then
				
				' Sort by the location column, if we're configured to and added it.
				objCmd.RunCommand "Set SORTBY=pathrel"
			End If
			
		End If

	End If

End Function

This is very useful, thanks. I am no use with code, but is it possible to change the Location_Column so that it appears last rather than first in the list of columns? Appearing first is a (minor) pain for my workflows when the focus is file names. Otherwise I have just turned it off.

Just change this line:

 objCmd.RunCommand "Set COLUMNSADD=pathrel(0)"

To this, removing the "(0)":

 objCmd.RunCommand "Set COLUMNSADD=pathrel"

This is a necro post but it was exactly what I was looking for, and I went and made some small additions, like a config option to make it also apply to the Find Panel results (as mentioned in the original post as a possibility). Also I added a config setting for the question by @phil_ga for the location column position, which takes either an index number or the string "last".

option explicit

' Search Format Lock
' (C) 2014 Leo Davidson
' 
' This is a script for Directory Opus.
' See http://www.gpsoft.com.au/DScripts/redirect.asp?page=scripts for development information.

' Thread: https://resource.dopus.com/t/search-results-format-lock/18061

' Called by Directory Opus to initialize the script
Function OnInit(initData)
	initData.name = "Search Format Lock"
	initData.desc = "Preserves the current columns when using the Search field instead of switching to the Search Results folder format."
	initData.copyright = "(C) 2014 Leo Davidson"
	initData.version = "1.1"
	initData.default_enable = true
	
	initData.config.Add_Location_Column = True
	initData.config.Sort_Location_Column = True
	initData.config.Apply_To_Find_Panel = True
	initData.config.Apply_To_Search_Field = True
	initData.config.Location_Column_Position = "0"
	
	' Config Descriptions
	 Dim config_desc: Set config_desc = DOpus.Create.Map
	 config_desc("Location_Column_Position") = 	"Sets the placement of the Location column" & vbNewLine & _
												" - For a specific column position, set the index to a number starting with 0 being first (far left)." & vbNewLine & _
												" - To place it last (far right), set the value as 'last'"
	
	' Create wildcard object in Script.Vars for "Find Fesults" collection path, because if there are multiple it may look like:  coll://Find Results (1)\
	initData.Vars.Set "wildObjFindResults", DOpus.FSUtil.NewWild("coll://Find Results*")
	initData.config_desc = config_desc

End Function


Function GetLockVarName(objTab)
	If (objTab.right) Then
		GetLockVarName = "SearchFormatLock_Right"
	Else
		GetLockVarName = "SearchFormatLock_Left"
	End If
End Function


' Called before a new folder is read in a tab
Function OnBeforeFolderChange(beforeFolderChangeData)
	OnBeforeFolderChange = False ' Allow the folder change to go ahead as normal.
	
	If (beforeFolderChangeData.initial) Then
		' Don't do anything if it's the first read of a new tab.
		Exit Function
	End If

	' Check for: Find Results
	If Script.Vars.Get("wildObjFindResults").Match(beforeFolderChangeData.Path) Then
		If Not script.config.Apply_To_Find_Panel Then
			' Don't do anything if we're changing to Find Results but the config is set to not apply there
			Exit Function
		End If
	' Check for: Quick Find Results
	ElseIf DOpus.FSUtil.ComparePath(beforeFolderChangeData.Path, "coll://%%Lister-Quick-Find-Results%%", "p") Then
		If Not script.config.Apply_To_Search_Field Then
			' Don't do anything if we're changing to Quick Find Results but the config is set to not apply there
			Exit Function
		End If
	' If path is neither Find nor Quick Find results
	Else
		' Don't do anything if we're not changing to any kind of search results
		Exit Function
	End If

	Dim objTab
	Set objTab = beforeFolderChangeData.tab

	Dim objCmd
	Set objCmd = DOpus.CreateCommand
	objCmd.SetSourceTab(objTab)

	If (objCmd.IsSet("FORMATLOCK=Toggle")) Then
		' Don't do anything if the format lock is already on. (So we don't turn it off after changing.)
		Exit Function
	End If

	' Set a variable which tells us to undo the format lock after the folder change.
	' The format locks are per-lister, but there is a seperate one for the left and right
	' halves of a dual-display lister, so we need to make a note of which one is ours.

	objTab.Vars.Set GetLockVarName(objTab), True

	' Turn on the format lock, so whatever the format is now will stay after the folder change.
	objCmd.RunCommand "Set FORMATLOCK=On"

End Function


' Called after a new folder is read in a tab
Function OnAfterFolderChange(afterFolderChangeData)

	OnAfterFolderChange = False ' Ensure normal behaviour if the folder read failed.

	Dim objTab
	Set objTab = afterFolderChangeData.tab

	Dim objVars
	Set objVars = objTab.Vars
	
	Dim strVarName
	strVarName = GetLockVarName(objTab)
	
	If (Not objVars.Exists(strVarName)) Then
		' Variable wasn't set, which means the format lock was already turned on by the user.
		' We should leave both the lock and the format alone.
		Exit Function
	End If

	' Remove our variable.
	objVars.Delete strVarName

	Dim objCmd
	Set objCmd = DOpus.CreateCommand
	objCmd.SetSourceTab(objTab)
	
	' Turn off the format lock again.
	objCmd.RunCommand "Set FORMATLOCK=Off"

	If (afterFolderChangeData.result And Script.config.Add_Location_Column) Then

		' The folder read succeeded (i.e. we are now in a Search Results folder)
		' and we are configured to add the Location column.

		If ((Not objCmd.IsSet("COLUMNSADD=pathrel")) And (Not objCmd.IsSet("COLUMNSADD=path"))) Then

			' Neither Location column is present already.
			
			' Set column position based on config
			Dim locationColPosition
			If (LCase(Trim(Script.Config.Location_Column_Position)) = "last") Then
				locationColPosition = ""
			Else
				locationColPosition = Trim(Script.Config.Location_Column_Position)
			End If

			' Add Location (Relative) as the first column.
			objCmd.RunCommand "Set COLUMNSADD=pathrel(" & locationColPosition & ")"
			
			If (Script.config.Sort_Location_Column) Then
				
				' Sort by the location column, if we're configured to and added it.
				objCmd.RunCommand "Set SORTBY=pathrel"
			End If
			
		End If

	End If

End Function
2 Likes