6 Windows,SysUtils,ShellApi,Forms;
8 var Input,InputNew : string;
12 function ExecWait(const CommandLine: string;
13 const Visible: boolean = false;
14 const MaxSeconds: integer = 60): boolean;
15 //Executes programs and waits until they are terminated
18 PI: TProcessInformation;
25 SI.dwFlags := STARTF_USESHOWWINDOW;
26 SI.wShowWindow := SW_HIDE;
28 if (CreateProcess(nil, pchar(CommandLine), nil, nil, False, 0, nil, nil, SI, PI)) then
30 case WaitForSingleObject(PI.hProcess, MaxSeconds * 1000) of
31 WAIT_OBJECT_0: GetExitCodeProcess(PI.hProcess, ExitCode);
32 WAIT_ABANDONED: TerminateProcess(PI.hProcess, ExitCode);
33 WAIT_TIMEOUT: TerminateProcess(PI.hProcess, ExitCode);
35 result := ExitCode = 0;
36 CloseHandle(PI.hProcess);
37 CloseHandle(PI.hThread);
42 function RenameFile(const OldName, NewName: string): boolean;
47 sh.Wnd := Application.Handle;
48 sh.wFunc := fo_Rename;
49 //terminate with null byte to set list ending
50 sh.pFrom := PChar(OldName + #0);
51 sh.pTo := PChar(NewName + #0);
52 sh.fFlags := fof_Silent or fof_MultiDestFiles;
53 Result:=ShFileOperation(sh)=0;
61 //InputNew = original filename with ending "-preview" (e.g. test-preview.pdf)
62 InputNew:= copy(Input,1,Length(Input)-4); //remove ".pdf"
63 InputNew:= InputNew+'-preview.pdf';
64 //check if renamed file exists
65 FileTest:= FileExists(InputNew);
66 if FileTest = true then
69 ExecWait('pdfclose --file "'+InputNew+'"');
74 RenameFile(Input,InputNew);
75 ExecWait('pdfopen --file "'+InputNew+'" --back');