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"