How do I know when a script has completed?

Another option is to use the progress dialogue. I used it in this script.
The script run a lengthy process on a list of folders. The way I have written it you cant stop the current folder being processed but if you click pause or stop then this will be taken in to account before processing the next one.
So in this way if I selected a lot of folders I can see the progress of them as each folder finishes being processed, and then I can pause or about.

The code looks something like this.

  var folderCount = 1;
  while (!objEnum.atEnd())
  {
    if(progress.GetAbortState=="a") break;
    
    if(progress.GetAbortState!="p")
    {
      var folder = objEnum.item();
      SetCurrentStatus(folder);
      progress.SetTitle("Creating thumbnails and folder images ("+folderCount+"/" + funcData.func.sourcetab.selected_dirs.count + ")");
      //Do the work
      CreateAllThumbnails(folder);
      objEnum.moveNext();    
      folderCount++;
      progress.StepFiles();
    }
  }
  progress.hide();