Resource

Resource Basics

Resource Name Macro Command to Catalog Label
Resource Version 2.0
Category Catalog
Downloads 1198
Release Date 8/2/2016 11:06:14 PM
 

Resource Details

Description This script will create catalog labels based on a macro command. For every selected thumbnail the macro is parsed and the value is assigned as a catalog label to the image.
Installation Instructions n.a.
Usage Instructions 1. Select the images that you'd like to process
2. Right click on a selected thumb
3. Select Run script from repository -> Macro Command to Catalog Label
Changes in this version Added split option
Special Requirements
 

Author Information

Author Name IDimager Support
 

Available Attachments

Attachment #1 MacroToCatalogLabel.png
 

Script

{
  Author: IDimager
  Initial Date: 2016-08-02

  This script will create catalog labels based on an XMP macro for every selected thumbnail
  and then assign the catalog label to the image.

  Version: 1.01
    Author: IDimager
    Date: 13 may 2020
    Changes: Made compatible with high res monitors

  Version: 2.00
    Author: IDimager
    Date: 14 may 2020
    Changes: Added an option to split the token using a separator.
}

var
  FMacro: WideString;
  FSplit: Boolean;
  FSplitSeparator: WideString;
  AMacroEdit: TvxTextBox;
  ASplitCheck: TvxCheckBox;
  ASplitEdit: TvxTextBox;

procedure _MacroButtonClick (Sender: TObject);
begin
  MacroValuesPopup.Component := AMacroEdit;
  MacroValuesPopup.Popup(Mouse.CursorPos.X, Mouse.CursorPos.Y);
end;

function BuildForm: TvxSceneForm;
var
  ALabel: TvxLabel;
  AEdit: TvxTextBox;
  AOk: TvxButton;
  AMacroButton: TvxButton;
begin
  result := TvxSceneForm.Create(nil);
  result.Caption  := 'Macro Command to Catalog Label';
  result.Position := poScreenCenter;
  result.Width := 400 * result.DPIFactor;
  result.Height := 200 * result.DPIFactor;

  ALabel := TvxLabel.Create(result.Root);
  result.Root.AddObject(ALabel);
  ALabel.Text := 'Macro Command';
  ALabel.TextAlign := vgTextAlignNear;
  ALabel.Position.Point := vgPoint(20, 20);

  AEdit := TvxTextBox.Create(result.Root);
  result.Root.AddObject(AEdit);
  AEdit.Text := FMacro;
  AEdit.Width := 300;
  AEdit.Position.Point := vgPoint(20, 40);
  AMacroEdit := AEdit;

  AMacroButton := TvxButton.Create(result.Root);
  result.Root.AddObject(AMacroButton);
  AMacroButton.Position.Point := vgPoint(AEdit.Position.X + AEdit.Width, AEdit.Position.Y);
  AMacroButton.Width  := AEdit.Height;
  AMacroButton.Text   := '...';
  AMacroButton.OnClick:= '_MacroButtonClick';

  ASplitCheck := TvxCheckBox.Create(result.Root);
  result.Root.AddObject(ASplitCheck);
  ASplitCheck.IsChecked := FSplit;
  ASplitCheck.Text := 'Split value';
  ASplitCheck.Position.Point := vgPoint(AEdit.Position.X, AEdit.Position.Y + AEdit.Height + 10);

  ALabel := TvxLabel.Create(result.Root);
  result.Root.AddObject(ALabel);
  ALabel.Text := 'Split separator';
  ALabel.TextAlign := vgTextAlignNear;
  ALabel.Position.Point := vgPoint(ASplitCheck.Position.X + 20, ASplitCheck.Position.Y + ASplitCheck.Height + 3);

  AEdit := TvxTextBox.Create(result.Root);
  result.Root.AddObject(AEdit);
  AEdit.Text := FSplitSeparator;
  AEdit.Width := 50;
  AEdit.Position.Point := vgPoint(ALabel.Position.X + 90, ASplitCheck.Position.Y + ASplitCheck.Height + 1);
  ASplitEdit := AEdit;

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

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

        if not FSplit then
          ATokens.Add(AValue)
        else
          Tokenize(AValue, FSplitSeparator, ATokens);

        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;
      finally
        ATokens.Free;
      end;
    end;
  finally
    AProp.Free;
    ACatItem.Free;
  end;
end;

var
  i: Integer;
  AForm: TvxSceneForm;
  AProgress: TxomProgress;
begin
  FMacro    := '%FileName';
  FMacro    := ReadFromRegistry ('Scripts', 'MacroToCatLabel', FMacro);

  FSplit := False;
  FSplit := ReadFromRegistry ('Scripts', 'MacroToCatLabel_Split', Ord(FSplit)) = Ord(True);

  FSplitSeparator := ',';
  FSplitSeparator := ReadFromRegistry ('Scripts', 'MacroToCatLabel_SplitSep', FSplitSeparator);

  AForm := BuildForm;
  try
    AForm.ShowModal;
    if (AForm.ModalResult = mrOk) then
    begin
      FMacro          := Trim(AMacroEdit.Text);
      FSplit          := ASplitCheck.IsChecked;
      FSplitSeparator := Trim(ASplitEdit.Text);

      if FMacro = '' then
      begin
        Say('No macro selected');
        exit;
      end;

      if (FSplit) and (FSplitSeparator = '') then
      begin
        Say('No separator specified for splitting');
        exit;
      end;

      if Ask2('Are you sure that you want to create and assign Catalog Labels for every selected thumbnail based on the Macro Command:', FMacro) then
      begin
        WriteToRegistry ('Scripts', 'MacroToCatLabel', FMacro);
        WriteToRegistry ('Scripts', 'MacroToCatLabel_Split', Ord(FSplit));
        WriteToRegistry ('Scripts', 'MacroToCatLabel_SplitSep', FSplitSeparator);

        AProgress := TxomProgress.Create(nil);
        try
          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;

          AProgress.Hide;

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