{
    Initial Date: 12 may 2020

    Version history:
        1.1
            Author: Hert Vanzwietering
            18 dec 2023;
            Also updates photoshop:DateCreated
        1.0
            Author: Hert Vanzwietering
            12 may 2020;
            initial version
}

  function HandleImageItem(AImageItem: TImageItem): Boolean;
  var
    AXmp: TXMP;
  begin
    result := False;
    AXMp := TXMP.Create(nil)
    try
      if PublicCatalog.LoadXMPForImage(AImageItem, AXmp, PublicOptions.CachedXMP) then
      begin
        AXmp.QuickSetProperty('http://ns.adobe.com/xap/1.0/', 'xmp:CreateDate', AXmp.PhotoDate);
        AXmp.QuickSetProperty('http://ns.adobe.com/photoshop/1.0/', 'photoshop:DateCreated', AXmp.PhotoDate);

        PublicCatalog.SaveXMPForImage(AImageItem, AXmp, PublicOptions.CachedXMP);
        result := True;
      end;
    finally
      AXmp.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 writes the photo date as "xmp:CreateDate" and "photoshop:DateCreated" 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 xmp:CreateDate';
    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;


