3 Windows PDF view helper
6 This will be installed as pdfview.exe.
8 The application will launch the default PDF viewer to display the PDF file,
9 but works around the file locking problems of Adobe Reader.
11 Source code of pdfopen/pdfclose is available at:
12 http://magic.aladdin.cs.cmu.edu/2005/07/15/pdfopen-and-pdfclose/
19 # Functions from FileFunc.nsh
20 !insertmacro GetParameters
21 !insertmacro GetFileName
22 !insertmacro GetParent
24 #--------------------------------
29 Icon "..\packaging\icons\lyx.ico"
32 #--------------------------------
33 # Windows Vista settings
35 RequestExecutionLevel user
37 #--------------------------------
43 !define FILE_NOTIFY_CHANGE_LAST_WRITE 0x00000010
44 !define WAIT_TIMEOUT 0x00000102
46 #--------------------------------
59 Var ChangeNotification
68 #--------------------------------
71 !macro SystemCall STACK
73 # Call a Windows API function
76 CallInstDLL "$EXEDIR\System.dll" Call
80 !macro HideConsole COMMAND_LINE
82 # Run an application and hide console output
84 Push `${COMMAND_LINE}`
85 CallInstDLL "$EXEDIR\Console.dll" Exec
88 ${If} $RunAppReturn == "error"
89 MessageBox MB_OK|MB_ICONSTOP "Error opening PDF file $PDFFile."
94 #--------------------------------
97 Section "View PDF file"
99 InitPluginsDir # Temporary directory for PDF file
101 # Command line parameters
102 ${GetParameters} $OriginalFile
105 StrCpy $Character $OriginalFile 1
106 ${If} $Character == '"'
107 StrCpy $OriginalFile $OriginalFile "" 1
109 StrCpy $Character $OriginalFile 1 -1
110 ${If} $Character == '"'
111 StrCpy $OriginalFile $OriginalFile -1
114 GetFullPathName $OriginalFile $OriginalFile
115 ${GetFileName} $OriginalFile $OriginalFileName
116 ${GetParent} $OriginalFile $OriginalDir # tmpbuf
117 ${GetParent} $OriginalDir $OriginalDir # tmpdir
119 SetOutPath $TEMP # The LyX tmpbuf should not be locked
121 StrCpy $PDFFile $PLUGINSDIR\$OriginalFileName
123 # Check whether the file will be opened with Adobe Reader or Adobe Acrobat
124 !insertmacro SystemCall "shell32::FindExecutable(t '$OriginalFile', t '', t .s)"
129 MessageBox MB_OK|MB_ICONEXCLAMATION "No PDF viewer is installed. \
130 Please install a PDF viewer such as Adobe Reader."
134 ${If} $Viewer == "AcroRd32.exe"
135 ${OrIf} $Viewer == "Acrobat.exe"
139 # Close existing view
140 ${If} ${FileExists} $PDFFile
141 !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --file "$PDFFile"'
144 # Copy PDF to temporary file to allow LyX to overwrite the original
145 CopyFiles /SILENT $OriginalFile $PDFFile
148 !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --back --file "$PDFFile"'
150 # Monitor for updates of the original file
151 GetFileTime $OriginalFile $OriginalTimeHigh $OriginalTimeLow
152 !insertmacro SystemCall "kernel32::FindFirstChangeNotification(t '$OriginalDir', \
153 i 1, i ${FILE_NOTIFY_CHANGE_LAST_WRITE}) i.s"
154 Pop $ChangeNotification
158 !insertmacro SystemCall "kernel32::WaitForSingleObject(i $ChangeNotification, i 10000) i.s"
161 # Check whether the lock is still active (if not, Adobe Reader is closed)
163 FileOpen $LockedFile $PDFFile a
165 ${If} $LockedFile != ""
166 # Quit this application
167 FileClose $LockedFile
169 !insertmacro SystemCall "kernel32::FindCloseChangeNotification(i $ChangeNotification)"
173 ${IfNot} $WaitReturn = ${WAIT_TIMEOUT}
175 # The LyX temporary directory has been updated
176 # Check whether it's the PDF file that has been updated
178 GetFileTime $OriginalFile $CurrentTimeHigh $CurrentTimeLow
180 ${If} $OriginalTimeHigh != $CurrentTimeHigh
181 ${OrIf} $OriginalTimeLow != $CurrentTimeLow
182 # PDF has been modified, update view
183 !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --file "$PDFFile"'
184 CopyFiles /SILENT $OriginalFile $PDFFile
185 !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --back --file "$PDFFile"'
188 StrCpy $OriginalTimeHigh $CurrentTimeHigh
189 StrCpy $OriginalTimeLow $CurrentTimeLow
193 !insertmacro SystemCall "kernel32::FindNextChangeNotification(i $ChangeNotification)"
201 # Another PDF viewer like GSView is used
202 # No need for special actions, just forward to ShellExecute
203 ExecShell open $OriginalFile