Reading and Writing Color Labels

Post Reply
Ulrich
Posts: 30
Joined: 14 Jul 21 11:59

Reading and Writing Color Labels

Post by Ulrich »

Hi,
another question.

I would like to set the color label of a photo if some conditions are true, as part of a bigger script that does some more checks.

Therefore if would like to first check the conditions (that's easily done for me), if the conditions are true, I would like to read the current assigned color label and if it is not the expected one, I would like to write a new color label to the file.
By reading the current color label before assigning it, I avoid to write the same color label over and over.

Can someboaby help how to read and write these labels?

Greetings
Ulrich
G8DHE
Posts: 707
Joined: 21 Aug 17 12:58

Re: Reading and Writing Color Labels

Post by G8DHE »

Is it not simply %xmp:xmp:Label as can be seen below by examining the field description of an image with the label set ?
colour_label.JPG
colour_label.JPG (174.75 KiB) Viewed 2820 times
Geoff Mather (G8DHE)
Ulrich
Posts: 30
Joined: 14 Jul 21 11:59

Re: Reading and Writing Color Labels

Post by Ulrich »

Hi G8DHE,

yes, this is the necessary field, but how can you read and write this field via script ??
Ulrich
Posts: 30
Joined: 14 Jul 21 11:59

Re: Reading and Writing Color Labels

Post by Ulrich »

May I ask again I anyboady can help how to read and WRITE Color Labels via script?
mjbiggs
Posts: 22
Joined: 01 Oct 10 20:50

Re: Reading and Writing Color Labels

Post by mjbiggs »

Hello

These might be of help. Just cobbled together but work on my setup (Windows 10 PSU 6.4.0.3863 64bit).


Custom Thumbnail Info to read and display color label:

Code: Select all

%code
    var
      ACatItem: TCatalogItem;
    begin
      ACatItem := TCatalogItem.Create(nil);
      if PublicCatalog.FindImageCombined (ImageItem, ACatItem, False, vptNone) then
      begin
        result := '<font color="#CF000000><ind y="2">Current Color Label is ' + ACatItem.ColorLabel + '</ind></font>';
      end;
      ACatItem.Free;
    end;
%/code

PSU Script to read and write color label for currently selected image:
(PSU->select an image->Tools->Scripter->paste into editor window->Run)

Code: Select all

  // Color label names in string list must match those set in PSU Preferences,
  // so edit as needed.
  function GetColorLabel;
  var
    AStringList: TTntStringList;
    AChecked: Boolean;
    ASelect: Integer;
  begin
    AChecked := True;
    AStringList := TTntStringList.Create;
    AStringList.Add('No Label'); // returns ASelect = 0 // white  #CFffffff
    AStringList.Add('Select');   // returns ASelect = 1 // red    roughly #CFfca19a
    AStringList.Add('Second');   // returns ASelect = 2 // yellow roughly #CFfaf297
    AStringList.Add('Approved'); // returns ASelect = 3 // green  roughly #CFc9e768
    AStringList.Add('Review');   // returns ASelect = 4 // blue   roughly #CFa7d0fe
    AStringList.Add('To Do');    // returns ASelect = 5 // purple roughly #CFe0beea
    ASelect := AskCustom(
              'Select New Color Label',
              '', // 'sub text'
              '', // 'info text'  // hidden details
              '', // 'check this' // check box
              AChecked,
              AStringList
            );
    // if button OK clicked        ASelect = -99
    // if button Cancel clicked    ASelect = -1
    // if button list item clicked ASelect = as above
    if (ASelect <> -99) and (ASelect <> -1) then
    // returns ASelect as above
    begin
      Result := AStringList.Strings[ASelect];
    end;
    AStringList.Free;
  end;

var
  ACatItem: TCatalogItem;
  AColorLabel: Widestring;
begin
  if Selected.Count = 0 then
  begin
    Say ('No image selected');
    exit;
  end;
  ACatItem := TCatalogItem.Create(nil);
  if Selected.Count > 0 then
  begin
    ImageItem := Selected.Items[0]; // only first selected item is affected
    if PublicCatalog.FindImageCombined (ImageItem, ACatItem, False, vptNone) then
    begin
      AColorLabel := ACatItem.ColorLabel;
      if AColorLabel = '' then AColorLabel := 'No Label';
      Say('Current Color Label is ' + AColorLabel);
      ACatItem.ColorLabel := GetColorLabel;
      Say('New Color Label is ' + ACatItem.ColorLabel);
      if PublicCatalog.StoreItemToDataBase(ACatItem, False) then PublicCatalog.UpdateItemSyncState(ACatItem, [], [idcsDBXmp], False, True);
    end;
  end;
  ACatItem.Free;
end;

mjbiggs
Posts: 22
Joined: 01 Oct 10 20:50

Re: Reading and Writing Color Labels

Post by mjbiggs »

Oops! Here's an update to the script with correction for writing 'No Label' by inserting following at line 56

Code: Select all

if ACatItem.ColorLabel = 'No Label' then ACatItem.ColorLabel := '';


Corrected script:

Code: Select all

  // Color label names in string list must match those set in PSU Preferences,
  // so edit as needed.
  function SelectColorLabel;
  var
    AStringList: TTntStringList;
    AChecked: Boolean;
    ASelect: Integer;
  begin
    AChecked := True;
    AStringList := TTntStringList.Create;
    AStringList.Add('No Label'); // returns ASelect = 0 // white  #CFffffff
    AStringList.Add('Select');   // returns ASelect = 1 // red    roughly #CFfca19a
    AStringList.Add('Second');   // returns ASelect = 2 // yellow roughly #CFfaf297
    AStringList.Add('Approved'); // returns ASelect = 3 // green  roughly #CFc9e768
    AStringList.Add('Review');   // returns ASelect = 4 // blue   roughly #CFa7d0fe
    AStringList.Add('To Do');    // returns ASelect = 5 // purple roughly #CFe0beea
    ASelect := AskCustom(
              'Select New Color Label',
              '', // 'sub text'
              '', // 'info text'  // hidden details
              '', // 'check this' // check box
              AChecked,
              AStringList
            );
    // if button OK clicked        ASelect = -99
    // if button Cancel clicked    ASelect = -1
    // if button list item clicked ASelect = as above
    if (ASelect <> -99) and (ASelect <> -1) then
    // returns ASelect as above
    begin
      Result := AStringList.Strings[ASelect];
    end;
    AStringList.Free;
  end;

var
  ACatItem: TCatalogItem;
  AColorLabel: WideString;
begin
  if Selected.Count = 0 then
  begin
    Say ('No image selected');
    exit;
  end;
  ACatItem := TCatalogItem.Create(nil);
  if Selected.Count > 0 then
  begin
    ImageItem := Selected.Items[0]; // only first selected item is affected
    if PublicCatalog.FindImageCombined (ImageItem, ACatItem, False, vptNone) then
    begin
      AColorLabel := ACatItem.ColorLabel;
      if AColorLabel = '' then AColorLabel := 'No Label';
      Say('Current Color Label is ' + AColorLabel);
      ACatItem.ColorLabel := SelectColorLabel;
      Say('New Color Label is ' + ACatItem.ColorLabel);
      if ACatItem.ColorLabel = 'No Label' then ACatItem.ColorLabel := '';
      if PublicCatalog.StoreItemToDataBase(ACatItem, False) then PublicCatalog.UpdateItemSyncState(ACatItem, [], [idcsDBXmp], False, True);
    end;
  end;
  ACatItem.Free;
end;
Ulrich
Posts: 30
Joined: 14 Jul 21 11:59

Re: Reading and Writing Color Labels

Post by Ulrich »

Thank you mjbiggs!!
I have to edit your code a little bit to work for my needs, but now I have the missing codesnips.
Great!
Post Reply