"Select By Filter" menu button

This button allows you to select files according to the checked filters that are automatically read from the folder "/dopusdata\Filters". You can use any combination of filters.

(Broken imgur link removed.)

Option Explicit

Function OnClick(ByRef ClickData)
   Dim dlg, File, Folder, FSO, i, k, list, path
   path = DOpus.FSUtil.Resolve("/dopusdata\Filters\")
   Set list = DOpus.NewVector
   Set FSO = CreateObject("Scripting.FileSystemObject")
   If FSO.FolderExists(path) Then
      Set Folder = FSO.GetFolder(path)
      i = 0
      For Each File In Folder.Files
         If LCase(FSO.GetExtensionName(File.Name)) = "ofi" Then
            list(i) = FSO.GetBaseName(File.Name)
            i = i + 1
         End If
   End If
   Set dlg = ClickData.Func.Dlg
   dlg.title = "Select By Filter"
   If list.count > 0 Then
      dlg.message = "Check filter(s) to select files and/or folders:"
      dlg.buttons = "OK|Cancel"
      dlg.choices = list
      dlg.list = DOpus.NewVector()
      dlg.message = "No ofi-files found in the filters directory."
      dlg.icon = "info"
      dlg.buttons = "OK"
   End If
   If dlg.result = 1 And list.count > 0 Then
      For k = 0 To dlg.choices.size - 1
         If dlg.list(k) Then
            ClickData.Func.Command.RunCommand("Select FILTER=""" & dlg.choices(k) & "")
         End If
   End If
End Function

Script button:
Select By Filter.dcf (2.57 KB)

If you do not have filters, you can use these:
Filters.zip (8.65 KB)

1 Like

Thanks for sharing. I would love to see a list like this added to the filter bar drop down.

I have taken the liberty of adapting YankeeZulu's idea and turning it into a pop-up filter menu. The following code creates a $FilterMenu command which I usually access from my lister context (right click) menu. The screen grabs show how it works.

FilterMenu.vbs.txt (3.9 KB)

' $FilterMenu [filterflags] [clear]
' v1.0 (2014/06/04) Pop-up menu of user defined saved filters. Sample calls..
' 1. $FilterMenu [select]
' 2. $FilterMenu clear
' 3. $FilterMenu deselect
' 4. $FilterMenu hidenomatch,deselect
' 5. $FilterMenu hide,deselect
' Pops up a menu of saved filters then executes the selected filter or does nothing if escape is pressed.
' The default action is "select" if no filterflags are specified.
' The "clear" option is a special case, used to clear all active filters. No menu is displayed.
' See https://resource.dopus.com/t/select-by-filter-menu-button/18483/1 for YankeeZulu's original button code.
' Adapted by AussieBoykie to use a single choice pop-up menu rather than a multiple choice checklist.

option explicit

Function OnInit(ByRef initData) ' OnInit is called by DOpus to initialize the script
   initData.name = "Choose Filter From Menu"
   initData.version= "1.0"
   initData.desc = "Adds the $FilterMenu command to Directory Opus"
   initData.copyright = "Adapted by AussieBoykie from original code by YankeeZulu"
   initData.default_enable = true
   dim cmd : set cmd = initData.AddCommand()
   cmd.name = "$FilterMenu" ' This is the name of the command being added
   cmd.method = "Main" ' This is the routine to execute when the command is invoked 
   cmd.desc = initData.desc
   cmd.label = initdata.name
   cmd.template = "Args/M,Clear/S"
End Function

Function Main(ByRef scriptCmdData) ' This is the executable code
   Dim argstring, cdf, dlg, File, Filter, Filters, Folder, FSO, i, list, menu, path, result, s
   Set list = DOpus.NewVector : Set menu = DOpus.NewVector
   Set cdf = scriptCmdData.Func
   If cdf.args.got_arg.Clear Then ' Special case
      Exit Function
   End If
   If cdf.args.got_arg.Args Then ' Build an argstring for FILTERFLAGS - e.g. select,hidenomatch
      argstring = trim(cdf.args.Args(0))
      For i=2 to cdf.args.Args.count
         argstring = argstring & " " & trim(cdf.args.Args(i-1))
      argstring = replace(argstring," ",",",1,-1,1) ' Eliminate embedded spaces (change to inoffensive commas)
      argstring = "select" ' Default argstring is select
   End If
   path = DOpus.FSUtil.Resolve("/dopusdata\Filters\") ' This is where Opus user defined filters are stored
   Set Filters = DOpus.FSUtil.ReadDir(path) ' This is the filters folder object
   list(0) = "Clear all filters" : menu(0) = 0 ' No default selection
   i = 0
      Set Filter = Filters.next
      If Filters.error Then Exit Do
      If LCase(Filter.ext) = ".ofi" Then
         i = i + 1
         If i = 1 Then ' If there is at least one filter then insert a separator
            list(i) = "-" : menu(i) = 0
            i = i + 1
         End If
         list(i) = Filter.name_stem : menu(i) = 0
      End If
   Loop While Not Filters.complete
   set dlg = scriptCmdData.Func.Dlg
   With dlg
      .choices = list
      .menu = menu
      i = dlg.result
   End With
   If i = 0 Then ' Do nothing
   ElseIf i = 1 Then ClearAll ' Clear all filters      
      s = "Select """ & dlg.choices(i-1) & """ FILTER FILTERFLAGS=" & argstring
      DOpus.output s
      With scriptCmdData.Func.Command
         .addline("Set AUTOSIZECOLUMNS")
      End With
   End If
End Function

Function ClearAll
   Dim cmd
   Set cmd = DOpus.NewCommand
   With cmd
      .addline "Set SHOWFILTERFILENAME="""""
      .addline "Set HIDEFILTERFILENAME="""""
      .addline "Set SHOWFILTERFOLDERS="""""
      .addline "Set HIDEFILTERFOLDERS="""""
      .addline "Set HIDEFILTERATTR="""""
      .addline "Set SHOWFILTERATTR="""""
      .addline "Set QUICKFILTERCLEAR"
      .addline "Select NOPATTERN SHOWHIDDEN"
      .addline("Set AUTOSIZECOLUMNS")
   End With
End Function

I added some buttons to my lister context menu to provide a right mouse click interface.
These could just as easily be added to a toolbar.

This is part of the filters menu that pops up when I right mouse click.

Here is the result of clicking Filter (Select) from the context menu and then Last5Days.
Button code is $FilterMenu

Here is the result of clicking Show Filtered (Matched) from the context menu and then Last5Days.
Button code is $FilterMenu hidenomatch,deselect

Here is the result of clicking Show Filtered (UnMatched) from the context menu and then Last5Days.
Button code is $FilterMenu hide,deselect

You can also use $FilterMenu clear to clear all filters.

Regards, AB

1 Like

Wanted to thank you guys for doing this, I use filters very more often, as they are very easy to use now with these dynamic menus! o)
Also thanks for supplying a bunch of predefined filters, really useful!

I was still missing a full blown menu, to easily activate all possible things you can do with a filter, so here it is, just remove the entries you don't like.
The menu button uses aussieboykies popup-menu script-command version!

select.dcf (3.31 KB)

1 Like

How do I install the FilterMenu Command?
Copying and pasting The script above to the scripts directory didn't add it.

I used @tbone buttons, but they don't work because it can't find the command

If you have the SelectEx script command working, it can basically do the same (menu and things) - it's based of this FilterMenu script mixture.

Copying and pasting the code of the FilterMenu command should work, check the "Other Logs" console to see if there are any errors, also make sure the new script is activated. Some need activation after being inserted into the script addins folder.

Download the .dcf file, go into Customize mode, then drag it to your toolbar or into a menu.

The root post contains a toolbar button (which contains a script). It isn't a script add-in, just a standalone button.

Where is this?

@leo even after adding the original button, the SCRIPT command isn't created for tbones buttons.
I've imported tbone's buttons and the original.
The script commands does show something as you can see in my previous screenshot, but it doesn't work. I'm very confused about the Custom Commands button.

@tbone, can you give me a simple way to install both?

It seems my *.dcf file uses the command "FilterMenu" for each entry, while the second script (the script addin version offering the FilterMenu command), adds a command called "$FilterMenu" - notice the leading dollar char.

Two options to solve this, change the string "$FilterMenu" to "FilterMenu" in the *.vbs script file you pasted into "script addins", or edit each entry of the button I uploaded (add the dollar). I'm sorry for adding to the confusion by using a different command name, but since script commands on my system do not have the leading dollar, I obviously changed the script behind to register the command with the naming policy I use. o) My tidyness, your mess. o)

For SelectEx, use the magnifier in the top, enter "SelectEx" and you will find two top search entries with all you need.

I've placed the script into an attachment in AussieBoykie's post to make things easier.

Download that .vbs.txt file and then drag it to Preferences / Toolbars / Scripts to install it.

You can then create a button, menu item or hotkey somewhere which runs the $FitlerMenu command which the script adds. See How to use buttons and scripts from this forum for how to create a toolbar button which runs a command.

When the menu appears, most of what it lists are your saved filters, which you can edit via Preferences / File Operations / Filters. You probably won't see the same thing as in the screenshots, since you'll have different filters saved (or may not have any saved yet).

1 Like

Thank you for your patient reply

1 Like

4 posts were split to a new topic: Help using "Select By Filter" script/menu