From $ROOTSYS/tutorials/gui/exec_macro.C

#include "TSystem.h"
#include "TString.h"
#include "TGClient.h"
#include "TGWindow.h"
#include "TClass.h"
#include "THashList.h"
#include "TROOT.h"
#include "TInterpreter.h"
#include "TEnv.h"
#include "TVirtualX.h"
#include "TImage.h"

//______________________________________________________________________________
Int_t exec_macro(const char *macro, Bool_t comp = kFALSE, Bool_t save = kTRUE)
{
   // Execute the macro and save a capture in a png file.
   // This macro is used by stressGUI to execute and compare
   // the output of the GUI tutorials.

   enum EErrorCodes {
      kSuccess,
      kScriptDirNotFound,
      kCannotRunScript,
      kNumErrorCodes
   };

   if (gROOT->IsBatch() || !(gClient))
      return kCannotRunScript;
   TString pwd(gSystem->pwd());
   if (!gSystem->cd(gSystem->DirName(macro)))
      return kScriptDirNotFound;
   Int_t err = 0;
   TString cmd(".x ");
   cmd += gSystem->BaseName(macro);
   if (comp) cmd += "+";
   gVirtualX->Sync(1);
   gROOT->ProcessLine(cmd, &err);
   if (err != TInterpreter::kNoError)
      return kCannotRunScript;
   gSystem->cd(pwd);

   UInt_t nMainFrames = 0;
   TClass* clGMainFrame = TClass::GetClass("TGMainFrame");
   TGWindow* win = 0;
   TIter iWin(gClient->GetListOfWindows());
   while ((win = (TGWindow*)iWin())) {
      const TObject* winGetParent = win->GetParent();
      Bool_t winIsMapped = kFALSE;
      if (winGetParent == gClient->GetDefaultRoot())
         winIsMapped = kTRUE;//win->IsMapped();
      if (winIsMapped && win->InheritsFrom(clGMainFrame)) {
         win->MapRaised();
         if (save) {
            TString outfile = gSystem->BaseName(macro);
            outfile.ReplaceAll(".C", TString::Format("_%d.png",
                               ++nMainFrames));
            TImage *img = TImage::Create();
            win->RaiseWindow();
            img->FromWindow(win->GetId());
            img->WriteImage(outfile.Data());
            delete img;
         }
      }
   }
   if (!gEnv->GetValue("X11.Sync", 0))
      gVirtualX->Sync(0);
   return kSuccess;
}
 exec_macro.C:1
 exec_macro.C:2
 exec_macro.C:3
 exec_macro.C:4
 exec_macro.C:5
 exec_macro.C:6
 exec_macro.C:7
 exec_macro.C:8
 exec_macro.C:9
 exec_macro.C:10
 exec_macro.C:11
 exec_macro.C:12
 exec_macro.C:13
 exec_macro.C:14
 exec_macro.C:15
 exec_macro.C:16
 exec_macro.C:17
 exec_macro.C:18
 exec_macro.C:19
 exec_macro.C:20
 exec_macro.C:21
 exec_macro.C:22
 exec_macro.C:23
 exec_macro.C:24
 exec_macro.C:25
 exec_macro.C:26
 exec_macro.C:27
 exec_macro.C:28
 exec_macro.C:29
 exec_macro.C:30
 exec_macro.C:31
 exec_macro.C:32
 exec_macro.C:33
 exec_macro.C:34
 exec_macro.C:35
 exec_macro.C:36
 exec_macro.C:37
 exec_macro.C:38
 exec_macro.C:39
 exec_macro.C:40
 exec_macro.C:41
 exec_macro.C:42
 exec_macro.C:43
 exec_macro.C:44
 exec_macro.C:45
 exec_macro.C:46
 exec_macro.C:47
 exec_macro.C:48
 exec_macro.C:49
 exec_macro.C:50
 exec_macro.C:51
 exec_macro.C:52
 exec_macro.C:53
 exec_macro.C:54
 exec_macro.C:55
 exec_macro.C:56
 exec_macro.C:57
 exec_macro.C:58
 exec_macro.C:59
 exec_macro.C:60
 exec_macro.C:61
 exec_macro.C:62
 exec_macro.C:63
 exec_macro.C:64
 exec_macro.C:65
 exec_macro.C:66
 exec_macro.C:67
 exec_macro.C:68
 exec_macro.C:69