Resource

Resource Basics

Resource Name Swap Place Holders for Version Set for Photo Supreme
Resource Version 2.1
Category Version
Downloads 1076
Release Date 12/27/2013 10:37:49 AM
 

Resource Details

Description This script allows you to exchange images assigned to a specific place holder in a version set to another place holder.
Installation Instructions Download to file to your hard drive
Usage Instructions 1. Select thumbnails for the version sets to process (one thumb per version set)
2. Select Tools->Scripter
3. Select File -> Open and select the downloaded file
4. Run the script with the Run button

Then specify which two version place holders should be swapped
Changes in this version Support for Photo Supreme
Special Requirements
 

Author Information

Author Name IDimager Support
 

Available Attachments

Attachment #1 SwapPH.png
 

Script

{
  Version 2.1; 17 apr 2020; changed the interface to Supreme style
}

var
  AForm: TvxSceneForm;
  APhs: TCatalogPlaceHolders;
  cmbSourcePH: TvxPopupBox;
  cmbTargetPH: TvxPopupBox;
  ASourceList: TTntStringList;
  ATargetList: TTntStringList;

  procedure ReadPlaceHolders;
  var
    i: Integer;
  begin
    PublicCatalog.EnumPlaceHolders (APhs, False);

    ASourceList.AddObject ('Main Version', nil);
    ATargetList.AddObject ('Main Version', nil);

    for i := 0 to APhs.Count - 1 do
    begin
      if APhs.Items[i].Visible then
      begin
        ASourceList.AddObject (APhs.Items[i].PlaceHolder, APhs.Items[i]);
        ATargetList.AddObject (APhs.Items[i].PlaceHolder, APhs.Items[i]);
      end;
    end;

    for i := 0 to ASourceList.Count - 1 do
      cmbSourcePH.Items.Add(ASourceList.Strings[i]);
    for i := 0 to ATargetList.Count - 1 do
      cmbTargetPH.Items.Add(ATargetList.Strings[i]);
  end;

  function CreateForm: TvxSceneForm;
  var
    ALabel: TvxLabel;
    ACombo: TvxPopupBox;
    AButton: TvxButton;
  begin
    result := TvxSceneForm.Create(nil);
    result.Caption := 'Swap Place Holders';
    result.Position := poScreenCenter;
    result.Width := 365 * result.DPIFactor;
    result.Height := 240 * result.DPIFactor;

    ALabel := TvxLabel.Create(result);
    ALabel.Text := 'Swap Version Place Holder';
    ALabel.Position.X := 21; ALabel.Position.Y := 29;
    ALabel.Width := 300;
    ALabel.TextAlign := vgTextAlignNear;
    ALabel.Parent := result.Root;
    ACombo := TvxPopupBox.Create(result);
    ACombo.Position.X := 21; ACombo.Position.Y := 48;
    ACombo.Width := 300;
    ACombo.TextAlign := vgTextAlignNear;
    ACombo.Parent := result.Root;
    cmbSourcePH := ACombo;

    ALabel := TvxLabel.Create(result);
    ALabel.Text := 'With Place Holder';
    ALabel.Position.X := 21; ALabel.Position.Y := 100;
    ALabel.Width := 300;
    ALabel.TextAlign := vgTextAlignNear;
    ALabel.Parent := result.Root;
    ACombo := TvxPopupBox.Create(result);
    ACombo.Position.X := 21; ACombo.Position.Y := 120;
    ACombo.Width := 300;
    ACombo.TextAlign := vgTextAlignNear;
    ACombo.Parent := result.Root;
    cmbTargetPH := ACombo;

    AButton := TvxButton.Create(result);
    AButton.Text := 'Go';
    AButton.ModalResult := mrOk;
    AButton.Position.X := 21; AButton.Position.Y := 160;
    AButton.Parent := result.Root;

    ReadPlaceHolders;
  end;

  procedure SwapPlaceHolders (AImage: TImageItem; ASourcePH, ATargetPH: TCatalogPlaceHolder);
  var
    AMain: TCatalogItem;
    ASourceItem, ATargetItem: TCatalogItem;
    AHasSource, AHasTarget: Boolean;
  begin
    AMain := TCatalogItem.Create(nil);

    ASourceItem := TCatalogItem.Create(nil);
    ATargetItem := TCatalogItem.Create(nil);

    // is image in catalog? If so, retrieve the main version
    if not PublicCatalog.FindImageCombined (AImage, AMain, True, vptNone) then
      exit;

    if (ASourcePH = nil) then    // the source is the main version
    begin
      // get the existing version in the target placeholder
      if not PublicCatalog.EnumVersionForPlaceHolderInMain (AMain, ATargetPH, ATargetItem) then
        exit;

      // set this target place holder as the new main version
      PublicCatalog.UpdateMainVersion (AMain, ATargetItem);

      // refind the target item
      if not PublicCatalog.FindItemCombined2 (ATargetItem, ATargetItem, False, vptNone) then
        exit;

      // refind the main item
      if not PublicCatalog.FindImageCombined (AImage, AMain, True, vptNone) then
        exit;

      PublicCatalog.AssignPlaceHolderToVersionInMain (AMain, ATargetItem, ATargetPH);
    end;
    else if (ATargetPH = nil) then    // the target is the main version
    begin
      // get the existing version in the target placeholder
      if not PublicCatalog.EnumVersionForPlaceHolderInMain (AMain, ASourcePH, ASourceItem) then
        exit;

      // set this source place holder as the new main version
      PublicCatalog.UpdateMainVersion (AMain, ASourceItem);

      // refind the source item
      if not PublicCatalog.FindItemCombined2 (ASourceItem, ASourceItem, False, vptNone) then
        exit;

      // refind the main item
      if not PublicCatalog.FindImageCombined (AImage, AMain, True, vptNone) then
        exit;

      PublicCatalog.AssignPlaceHolderToVersionInMain (AMain, ASourceItem, ASourcePH);
    end
    else
    begin
      // get the existing version in the source placeholder
      AHasSource := PublicCatalog.EnumVersionForPlaceHolderInMain (AMain, ASourcePH, ASourceItem);

      // get the existing version in the target placeholder
      AHasTarget := PublicCatalog.EnumVersionForPlaceHolderInMain (AMain, ATargetPH, ATargetItem);

      if (not AHasTarget) and (not AHasSource) then
        exit;

      // now assign the new place holders
      if AHasSource then
        PublicCatalog.AssignPlaceHolderToVersionInMain (AMain, ASourceItem, ATargetPH);

      if AHasTarget then
        PublicCatalog.AssignPlaceHolderToVersionInMain (AMain, ATargetItem, ASourcePH);

      if not AHasSource then
        PublicCatalog.RemoveVersionForPlaceHolderInMain (AMain, ATargetPH);
      if not AHasTarget then
        PublicCatalog.RemoveVersionForPlaceHolderInMain (AMain, ASourcePH);
    end;

    ASourceItem.Free;
    ATargetItem.Free;

    AMain.Free;
  end;

  procedure StartWorking;
  var
    ASourcePH, ATargetPH: TCatalogPlaceHolder;
  begin
    if (cmbSourcePH.ItemIndex = -1) then
    begin
      Say ('No source place holder selected');
      exit;
    end;

    if (cmbTargetPH.ItemIndex = -1) then
    begin
      Say ('No target place holder selected');
      exit;
    end;

    ASourcePH := ASourceList.Objects[cmbSourcePH.ItemIndex];
    ATargetPH := ATargetList.Objects[cmbTargetPH.ItemIndex];

    if (ASourcePH = nil) and (ATargetPH = nil) then    // both selected are main versions
    begin
      Say ('Please select two different place holders to swap.');
      exit;
    end;

    if (ASourcePH <> nil) and (ATargetPH <> nil) then
    begin
      if (ASourcePH.GUID = ATargetPH.GUID) then    // both selected are main versions
      begin
        Say ('Please select two different place holders to swap.');
        exit;
      end;
    end;

    Progress.Cancel := False;
    Progress.Pos := 0;
    Progress.Max := Selected.Count;
    Progress.Show;
    Progress.ProgressText := 'Updating place holders...';

    for i := 0 to Selected.Count - 1 do
    begin
      Progress.Pos := i + 1;

      SwapPlaceHolders (Selected.Items[i], ASourcePH, ATargetPH);

      if Progress.Cancel then
        break;
    end;

    if Progress.Cancel then
      Say ('Cancelled');
    Progress.Hide;
  end;

begin
  APhs := TCatalogPlaceHolders.Create (TCatalogPlaceHolder, '');
  ASourceList := TTntStringList.Create;
  ATargetList := TTntStringList.Create;
  try
    AForm := CreateForm;
    try
      AForm.ShowModal;
      if AForm.ModalResult = mrOK then
        StartWorking;
    finally
      AForm.Free;
    end;
  finally
    ATargetList.Free;
    ASourceList.Free;
    APhs.Free;
  end;
end;