Resource

Resource Basics

Resource Name File Name to Catalog Label
Resource Version 3.0
Category Catalog
Downloads 1083
Release Date 10/23/2014 10:49:25 PM
 

Resource Details

Description This script will create catalog labels for file names of every selected thumbnail and then assign the catalog label to the image. The catalog label will be stored in the Miscellaneous category.
Installation Instructions Download the script to a folder on your hard drive
Usage Instructions 1. Select one or more thumbnails
2. Select Tools->Scripter
3. Load this script
4. Run the script with the run button of the Scripter
Changes in this version Added option to tokenize file names using a specific separator
Special Requirements
 

Author Information

Author Name IDimager Support
 

Script

{
  Author: IDimager
  Initial Date: 2010-08-04
  Version: 3.0

  Update 3.0: Added option to tokenize file names
  Update 2.0: Made compatible with Photo Supreme
  Update 2.1: Added confirmation
              The label is now also assigned

  This script will create catalog labels for file names of every selected thumbnail
  and then assign the catalog label to the image.
}

var
  ATokenizeCheck: TvxCheckBox;
  ALabel: TvxLabel;
  ATokenizeEdit: TvxTextBox;

procedure _CheckChange(Sender: TObject);
begin
  ALabel.Visible := ATokenizeCheck.IsChecked;
end;

function BuildForm: TvxSceneForm;
var
  AOk: TvxButton;
  AMacroButton: TvxButton;
begin
  result := TvxSceneForm.Create(nil);
  result.Caption  := 'File Name to Catalog Label';
  result.Position := poScreenCenter;
  result.Width := 400 * result.DPIFactor;
  result.Height := 200 * result.DPIFactor;

  ATokenizeCheck := TvxCheckBox.Create(result.Root);
  result.Root.AddObject(ATokenizeCheck);
  ATokenizeCheck.Text := 'Tokenize file names';
  ATokenizeCheck.TextAlign := vgTextAlignNear;
  ATokenizeCheck.IsChecked := (ReadFromRegistry ('Scripts', 'FileNameToCatalogLabel_Check', 0) = 1)
  ATokenizeCheck.Position.Point := vgPoint(20, 20);
  ATokenizeCheck.OnChange := '_CheckChange';

  ALabel := TvxLabel.Create(result.Root);
  ATokenizeCheck.AddObject(ALabel);
  ALabel.Text := 'Use separator';
  ALabel.TextAlign := vgTextAlignNear;
  ALabel.Position.Point := vgPoint(20, 40);
  ALabel.Visible := ATokenizeCheck.IsChecked;

  ATokenizeEdit := TvxTextBox.Create(result.Root);
  ALabel.AddObject(ATokenizeEdit);
  ATokenizeEdit.Text := ReadFromRegistry ('Scripts', 'FileNameToCatalogLabel_Sep', '_');
  ATokenizeEdit.Position.Point := vgPoint(20, 20);

  AOk := TvxButton.Create(result.Root);
  result.Root.AddObject(AOk);
  AOk.ModalResult := mrOk;
  AOk.Position.Point := vgPoint(20, 200 - 30 - AOk.Height);
  AOk.Text := 'OK';
end;

procedure HandleItem (AImage: TImageItem);
var
  ACatItem: TCatalogItem;
  AProp: TCatalogItemProp;
  AName: WideString;
  ATokens: TTntStringList;
  i: Integer;
begin
  ACatItem := TCatalogItem.Create(nil);
  AProp := TCatalogItemProp.Create(nil);
  ATokens := TTntStringList.Create;
  try
    if PublicCatalog.FindImageCombined (AImage, ACatItem, True, vptNone) then // get the main version
    begin
      AName := WideChangeFileExt(AImage.FileNameOnly, '');

      if ATokenizeCheck.IsChecked then
        Tokenize(AName, ATokenizeEdit.Text, ATokens)
      else
        ATokens.Add(AName);

      for i := 0 to ATokens.Count - 1 do
      begin
        AName := ATokens.Strings[i];

        // is there an existing catalog label for this file name?
        if not PublicCatalog.FindPropByName (AName, AProp, '', True) then
        begin
          // doesn't exist; ok, create one in the Keywords category
          AProp.GUID := NewGUID;  // give a unique ID
          AProp.PropName := AName;

          PublicCatalog.StorePropToDatabase (AProp, KeywordsGUID, False);
        end;

        // now assign the prop to the catalog item
        PublicCatalog.AddPropToItem (ACatItem, AProp, False, False);
      end;
    end;
  finally
    ATokens.Free;
    AProp.Free;
    ACatItem.Free;
  end;
end;

var
  AForm: TvxSceneForm;
  i: Integer;
  AProgress: TxomProgress;
begin
  if Selected.Count = 0 then
  begin
    Say('No images selected');
    exit;
  end;

  AForm := BuildForm;
  AProgress := TxomProgress.Create(nil);
  try
    AForm.ShowModal;
    if (AForm.ModalResult = mrOk) then
    begin
      WriteToRegistry('Scripts', 'FileNameToCatalogLabel_Check', iif(ATokenizeCheck.IsChecked, 1, 0));
      WriteToRegistry('Scripts', 'FileNameToCatalogLabel_Sep', ATokenizeEdit.Text);

      if Ask('Are you sure you want to create Catalog Labels for the ' + IntToStr(Selected.Count) + ' selected images?') then
      begin
        AProgress.Cancel := False;
        AProgress.Max := Selected.Count;
        AProgress.Pos := 0;
        AProgress.Show;

        for i := 0 to Selected.Count - 1 do
        begin
          AProgress.Pos := i + 1;
          if AProgress.Cancel then
            break;

          HandleItem(Selected.Items[i]);
        end;

        if Progress.Cancel then
          Say ('Cancelled')
        else
          Say ('Finished');
      end;
    end;
  finally
    AProgress.Free;
    AForm.Free;
  end;
end;