Auto-number a field

If you made a script you can offer it to the others here, or ask help to improve it. You can also report here bugs & problems with existing scripts.
Post Reply
CathodeRay
Posts: 3
Joined: 2014-02-13 12:31:36

Auto-number a field

Post by CathodeRay »

Hi

I am trying to write a script to add the next highest unique number to a field. Say I have 100 movies, half online (on HDD), half offline (DVDs). I want to use a field (say source) to add the next highest unique number to that field so I can use that number to ID the DVD so I can find it when I want it. In this case, the next movie number would be 101, but the next DVD number would be 51.

Getting the next highest unique number (no sorting needed) is easy enough eg:

Code: Select all

var
  curNum: Integer;
  maxNum: Integer;
  nextNum: Integer;

begin

    begin
      curNum := StrToInt(GetField(fieldSource), 0);
      if curNum > maxNum then maxNum := curNum;
      nextNum := maxNum + 1;
      if GetIteration() = GetIterationCount() - 1 then ShowMemo('nextNum: ' + IntToStr(nextNum));
    end;

//write the nextNum to the current movie 

end.
However this requires (obviously!) 'Movies to include' set to 'All' but I only want one selected, the one I want to write the next highest unique number to...

I have considered but been unable to achieve any of these solutions:

(a) select only one movie but have the above (as a function) run on all movies (add a line to tell it to run on all movies, not just the selected movie?)

(b) select all movies but somehow mark or flag or store in a variable the one which is to be written to

(3) select one movie but then read the (xml format) catalogue file directly and put the source field entries into an array and sort/get highest value. Have tried to make sense of how to do xml in Pascal but it makes my head hurt...

Any ideas?

Thanks in advance.
soulsnake
Posts: 756
Joined: 2011-03-14 15:42:20
Location: France

Post by soulsnake »

Hi,

You can not do this in one time with a script.
The idea is to store the max ID of movies in a static variable by running the script on all movies the first time (Mode 1).
Static variables are saved with script info so they are always available, even if you close and open AMC.
Then, the next times (Mode 2), you only have to run the script on a selected movie to set the next ID and update the static variable with the current max ID.

See the script below for a better explain ;) :

Code: Select all

program SetNextID;
var
  curId: Integer;
  maxId: Integer;
  nextId: Integer;
begin
  // Mode 1 (All movies selected) : Find max ID and store it in static variable
  // Call this script with all movies selected only the first time
  // or if IDs of movies have been changed manually
  if GetIterationCount > 1 then
  begin
    if GetIteration() = 0 then
      maxId := 0;
    curId := StrToInt(GetField(fieldSource), 0);
    if curId > maxId then
      maxId := curId;
    if GetIteration() = GetIterationCount() - 1 then
    begin
      SetStatic('MaxId', IntToStr(maxId));
      ShowMessage('MaxId = ' + IntToStr(maxId));
    end;
  end
  // Mode 2 (One movie selected) : Set next movie ID and update static variable
  // Call this script with one movie selected to set the next ID on selected movie
  else
  begin
    nextId := StrToInt(GetStatic('MaxId'), 0) + 1;
    SetField(fieldSource, IntToStr(nextId));
    SetStatic('MaxId', IntToStr(nextId));
  end;
end.
Soulsnake.
CathodeRay
Posts: 3
Joined: 2014-02-13 12:31:36

Post by CathodeRay »

soulsnake

Cool. A static variable that behaves as you describe is just what I was looking for. Although I had seen them mentioned, I didn't appreciate they were so persistent. I had the related idea (not mentioned earlier) of setting one in a text file, but that seemed a very clunky way of doing things.

Mode (1): neat code but I guess this could be set semi-manually as it is a one off event (unless I've missed something): grid view, sort by offline media number, make a note of the highest one, open an edit script window and run (no need to save it):

Code: Select all

maxId: integer;
SetStatic('maxId', x);
where x is the highest number noted after the grid > sort.

Mode (2) would then run each time you add a (single) movie, incrementing the maxId by 1 each time as it ran.

Very many thanks for your help.

CathodeRay
CathodeRay
Posts: 3
Joined: 2014-02-13 12:31:36

Post by CathodeRay »

soulsnake

Whoops - got ahead of myself there! Works much better with your full code. The static variable appears to be stored in scripts.ini, so I suppose you could set it manually there but why bother when your script does it automatically!

Thanks again

CathodeRay
Post Reply