]> git.lyx.org Git - features.git/blob - development/Win32/packaging/AltInstaller/specials/PDFViewWin/PDFViewWin.dpr
a316b3f4e1f9a42cb77f3b980830b4a5eb687dc3
[features.git] / development / Win32 / packaging / AltInstaller / specials / PDFViewWin / PDFViewWin.dpr
1 program PDFViewWin;
2
3 {$APPTYPE CONSOLE}
4
5 uses
6   Windows,SysUtils,ShellApi,Forms;
7
8 var Input,InputNew : string;
9     FileTest : boolean;
10
11     
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
16 var
17 SI: TStartupInfo;
18 PI: TProcessInformation;
19 ExitCode: DWORD;
20 begin
21  result := false;
22  GetStartupInfo(SI);
23  if not Visible then
24  begin
25   SI.dwFlags := STARTF_USESHOWWINDOW;
26   SI.wShowWindow := SW_HIDE;
27  end;
28  if (CreateProcess(nil, pchar(CommandLine), nil, nil, False, 0, nil, nil, SI, PI)) then
29  begin
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);
34   end;
35   result := ExitCode = 0;
36   CloseHandle(PI.hProcess);
37   CloseHandle(PI.hThread);
38  end;
39 end; // end function
40
41
42 function RenameFile(const OldName, NewName: string): boolean;
43 //renames files
44 var
45   sh: TSHFileOpStruct;
46 begin
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;
54 end; //end function
55
56
57 begin //begin program 
58
59  //Read given filename
60  Input:= ParamStr(1);
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
67  begin
68   //close old file
69   ExecWait('pdfclose --file "'+InputNew+'"');
70   //delete old file
71   DeleteFile(InputNew);
72  end;
73  //rename file
74  RenameFile(Input,InputNew);
75  ExecWait('pdfopen --file "'+InputNew+'" --back');
76
77 end. //end program