Resource

Resource Basics

Resource Name Filter by File Mask
Resource Version 1.0
Category Filter Script
Downloads 1269
Release Date 5/10/2020 8:11:12 AM
 

Resource Details

Description This script allows filtering a collection using one or more file mask definitions
Installation Instructions n.a.
Usage Instructions Use in Photo Supreme;
1. Open a collection
2. Open the filter bar
3. Click the FILTER button
4. Select From Repository
5. Select Filter By File Mask
Changes in this version n.a.
Special Requirements
 

Author Information

Author Name IDimager Support
 

Available Attachments

Attachment #1 Filter By File Mask.png
 

Script

{
  Filter by File Mask
    This script allows filtering a collection using one or more file mask definitions

    Version 1.0
      Author: IDimager
      Date: 10 may 2020
}

var
  FMask: WideString;
  AMaskEdit: TvxTextBox; 

procedure _SaveClick (Sender: Tobject);
var
  AElem: TidElement;
begin
  SaveDialog.Filter := 'File Mask Save Files|*.fsf';
  SaveDialog.DefaultExt := '.fsf';
  SaveDialog.InitialDir := DefaultAppDataDir + WideIncludeTrailingBackslash('Filters');
  if not SaveDialog.Execute then
    exit;

  AElem := TidElement.Create(nil);
  AElem.Prop['FilterMask']          := AMaskEdit.Text;
  AElem.SaveAsXMLFile(SaveDialog.FileName);
  AElem.Free;
end;

procedure _LoadClick (Sender: Tobject);
var
  AElem: TidElement;
begin
  OpenDialog.Filter     := 'File Mask Save Files|*.fsf';
  OpenDialog.DefaultExt := '.fsf';
  OpenDialog.InitialDir := DefaultAppDataDir + WideIncludeTrailingBackslash('Filters');
  if not OpenDialog.Execute then
    exit;
  AElem := TidElement.Create(nil);
  AElem.LoadFromXMLFile(OpenDialog.FileName);
  FMask     := '';
  FMask    := Nvl(AElem.Prop['FilterMask'], FMask);
  AElem.Free;

  AMaskEdit.Text := FMask;
end;

function BuildForm: TvxSceneForm;
var
  ALabel: TvxLabel;
  AEdit: TvxTextBox;
  AOk: TvxButton;
  ASave: TvxButton;
  ALoad: TvxButton;
begin
  result := TvxSceneForm.Create(nil);
  result.Caption  := 'Filter by File Mask';
  result.Position := poScreenCenter;
  result.Height := Round(200 * result.DPIFactor);

  ALabel := TvxLabel.Create(result.Root);
  result.Root.AddObject(ALabel);
  ALabel.Text := 'File Mask (use commas to separate multiple masks)';
  ALabel.TextAlign := vgTextAlignNear;
  ALabel.Position.Point := vgPoint(20, 20);
  ALabel.Width := 300;

  AEdit := TvxTextBox.Create(result.Root);
  result.Root.AddObject(AEdit);
  AEdit.Text := FMask;
  AEdit.Width := 200;
  AEdit.Position.Point := vgPoint(20, 40);
  AMaskEdit := AEdit;

  AOk := TvxButton.Create(result.Root);
  result.Root.AddObject(AOk);
  AOk.ModalResult := mrOk;
  AOk.Position.Point := vgPoint(20, (result.Height / result.DPIFactor) - 10 - AOk.Height);
  AOk.Text := 'OK';
  AOk.Default := True;

  ASave := TvxButton.Create(result.Root);
  result.Root.AddObject(ASave);
  ASave.OnClick := '_SaveClick';
  ASave.Position.Point := vgPoint((result.Width / result.DPIFactor) - 10 - ASave.Width, (result.Height / result.DPIFactor) - 10 - ASave.Height);
  ASave.Text := 'Save';

  ALoad := TvxButton.Create(result.Root);
  result.Root.AddObject(ALoad);
  ALoad.OnClick := '_LoadClick';
  ALoad.Position.Point := vgPoint(ASave.Position.X - 5 - ALoad.Width, (result.Height / result.DPIFactor) - 10 - ALoad.Height);
  ALoad.Text := 'Load';
end;

function Initialize: Boolean;
var
  AForm: TvxSceneForm;
begin
  result := True;

  FHit := 0;
  FMask    := '*.*';
  FMask    := ReadFromRegistry ('Scripts', 'FilterFileMask', FMask);

  AForm := BuildForm;
  AForm.ShowModal;
  result := (AForm.ModalResult = mrOk); 
  if result then
  begin
    FMask     := Trim(AMaskEdit.Text);
  end;
  AForm.Free;
end;

procedure Finalize;
begin
  MacroValuesPopup.Component := nil;

  WriteToRegistry ('Scripts', 'FilterFileMask', FMask);
end;

function Filter(AItem: TImageItem): Boolean;
var
  AFileName: WideString;
  AMaskList: TTntStringList;
  AMask: WideString;
  i: Integer;
begin
  result := False;
  if FMask = '' then
    exit;

  AFileName := AItem.FileNameOnly;
  AMaskList := TTntStringList.Create;
  try
    Tokenize(FMask, ',', AMaskList);
    for i := 0 to AMaskList.Count - 1 do
    begin
      AMask := AMaskList.Strings[i];
      result := WideMatchesMask(AFileName, AMask);
      if result then 
        break;
    end;
  finally
    AMaskList.Free;
  end;
end;