{
    Initial Date: 28 apr 2020

    Version history:
        1.0
            Author: Hert van Zwietering
            28 apr 2020;
            initial version
}

  function HandleImageItem(AImageItem: TImageItem): Boolean;
  var
    AItem: TCatalogItem;
  begin
    result := False;
    if not AImageItem.MediumLoaded then exit;

    AItem := TCatalogItem.Create(nil);
    try
      if PublicCatalog.FindImageCombined(AImageItem, AItem, False, vptNone) then
      begin
        SetFileDate(AItem.FileName, AItem.DateTimeStamp);
        result := True;
      end;
    finally
      AItem.Free;
    end;
  end;

var
  i, AHit: Integer;
  AProgress: TxomProgress;
begin
  if Selected.Count = 0 then
  begin
    Say ('No images selected');
    exit;
  end;

  if not Ask('This script changes the file stamp (created, modified) to the date as available in the photo metadata for the ' + IntToStr(Selected.Count) + ' selected thumbnails.' + CrLf2 + 'Are you sure you want to continue?') then
    exit;

  AProgress := TxomProgress.Create(nil);
  try
    AProgress.Caption := 'Set File Dates to Photo Date';
    AProgress.Cancel := False;
    AProgress.ProgressBar := True;
    AProgress.Max := Selected.Count;
    AProgress.Show;

    AHit := 0;
    for i := 0 to Selected.Count - 1 do
    begin
      AProgress.ProgressText := Selected.Items[i].FileName;
      AProgress.Pos := i + 1;
      if AProgress.Cancel then
        break;

      if HandleImageItem(Selected.Items[i]) then
        Inc(AHit);
    end;

    AProgress.Hide;
    if AProgress.Cancel then
      Say2('Cancelled', 'Dates for ' + IntToStr(AHit) + ' files are changed.')
    else
      SendVisualFeedback('Dates for ' + IntToStr(AHit) + ' files are changed.', 3000);
  finally
    AProgress.Free;
  end;
end;

