{
    Initial Date: 15 jul 2026

    Version history:
        1.0
            Author: IDimager Systems
            15 jul 2026;
            initial version
}

  function HandleImageItem(AImageItem: TImageItem): Boolean;
  var
    AXmp: TXMP;
    AStamp: TDateTime;
    ACatItem: TCatalogItem;
  begin
    result := False;
    ACatItem := TCatalogItem.Create(nil);
    AXmp := TXMP.Create(nil)
    try
      if PublicCatalog.FindImageCombined(AImageItem, ACatItem, False, vptNone) then
      begin
        if WideFileExists(ACatItem.FileName) then
        begin
          AStamp := FileCreated(ACatItem.FileName);

          PublicCatalog.LoadXMPForItem(ACatItem, AXmp, PublicOptions.CachedXMP);
          AXmp.QuickSetProperty('http://ns.adobe.com/exif/1.0/', 'exif:DateTimeOriginal', AStamp);
          AXmp.QuickSetProperty('http://ns.adobe.com/exif/1.0/', 'exif:DateTimeDigitized', AStamp);

          PublicCatalog.SaveXMPForImage(AImageItem, AXmp, PublicOptions.CachedXMP);

          ACatItem.DateTimeStamp := AXmp.PhotoDate;
          PublicCatalog.StoreItemToDataBase(ACatItem, False);
          result := True;
        end;
      end;
    finally
      AXmp.Free;
      ACatItem.Free;
    end;
  end;

var
  i: Integer;
  AProgress: TxomProgress;
begin
  if Selected.Count = 0 then
  begin
    Say ('No images selected');
    exit;
  end;

  if not Ask ('This script will write the file its "created" file stamp as exif:DateTimeOriginal and exif:DateTimeDigitized for the ' + IntToStr(Selected.Count) + ' selected thumbnails.' + CrLf2 + 'Are you sure you want to continue?') then
    exit;

  AProgress := TxomProgress.Create(nil);
  try
    AProgress.Caption := 'Update Exif dates';
    AProgress.Cancel := False;
    AProgress.ProgressBar := True;
    AProgress.Max := Selected.Count;
    AProgress.Show;

    for i := 0 to Selected.Count - 1 do
    begin
      AProgress.ProgressText := Selected.Items[i].FileName;
      AProgress.Pos := i + 1;
      if AProgress.Cancel then
        break;

      HandleImageItem(Selected.Items[i]);
    end;

    AProgress.Hide;
    if AProgress.Cancel then
      Say('Cancelled')
    else
      Say('Done');
  finally
    AProgress.Free;
  end;
end;


