{
  Update 2.0; 2014-08-14; Made compatible with Photo Supreme
  Update 2.1; 2017-11-22; Added confirmation question
}
procedure HandleImage (AImage: TImageItem; AMain: TCatalogItem);
var
  APath: TTntStringList;
  APathName: WideString;
  AProp: TCatalogItemProp;
  APathHierarchy, ASeparator: WideString;
begin
  APath := TTntStringList.Create;
  APathName := WideExcludeTrailingBackslash(WideExtractFilePath (AImage.FileName));
  if LeftStr(APathName, 2) = '\\' then
    APathName := 'x\' + MidStr(APathName, 3, Length(APathName));

  if IsWindows then
    ASeparator := '\'
  else
    ASeparator := '/';
  Tokenize (APathName, ASeparator, APath);

  AProp := TCatalogItemProp.Create(nil);
  APathHierarchy := '';
  // skip the first item in the list; it is the drive/volume
  for i := 1 to APath.Count - 1 do
    APathHierarchy := APathHierarchy + iif(APathHierarchy = '', '', '::') + APath.Strings[i];

  if Catalog.FindPropByPathName (APathHierarchy, AProp, '::', True) then
    Catalog.AddPropToItem (AMain, AProp, False, False);
  AProp.Free;

  APath.Free;
end;

var
  i: Integer;
  ACatItem: TCatalogItem;
begin
  if Selected.Count = 0 then
  begin
    Say('No images selected');
    exit;
  end;

  if not Ask('Are you sure you want to create Catalog Labels for the ' + IntToStr(Selected.Count) + ' selected images?') then
    exit;

  Progress.Cancel := False;
  Progress.Max := Selected.Count;
  Progress.Pos := 0;
  Progress.Show;

  for i:= 0 to Selected.Count - 1 do
  begin
    Progress.Pos := i + 1;

    ACatItem := TCatalogItem.Create(nil);
    // find the main item in the catalog; this is the one that gets the assigned label
    if Catalog.FindImageCombined (Selected.Items[i], ACatItem, True, vptNone) then
      HandleImage (Selected.Items[i], ACatItem);

    ACatItem.Free;

    if Progress.Cancel then
      break;
  end;

  Progress.Hide;
end;
