Search:   Help

Developers' side bar

Selected categories

Edit

Shared groups

Links

WindowsInstaller-pdfview

<< | Page list | >>

A modified version of pdfview.nsi

# 
# Windows PDF view helper
# Author: Joost Verburg
#
# This will be installed as pdfview.exe.
#
# This application is not an installer. It is a helper application for
# LyX that will launch the default PDF viewer to display a PDF file,
# but works around the file locking problems of Adobe Reader.
#
# Source code of pdfopen/pdfclose is available at:
#	http://magic.aladdin.cs.cmu.edu/2005/07/15/pdfopen-and-pdfclose/
#

# Include standard NSIS libraries
!include "LogicLib.nsh"	# http://nsis.sourceforge.net/LogicLib
!include "FileFunc.nsh"	# http://nsis.sourceforge.net/Docs/AppendixE.html#E.1

!insertmacro GetParameters	# Insert definition of function GetParameters
!insertmacro GetFileName	# Insert definition of function GetFileName ??

# --------------------------------
# Settings

Caption "PDF Viewer" 	# Where will this caption be shown????
OutFile pdfview.exe                     # Set name of resulting binary
Icon "..\packaging\icons\lyx_32x32.ico" # Icon for start menu. Eh, same icon?
SilentInstall silent                    # The "installer" should be silent ??

# --------------------------------
# Constants

# ?? Why not use from LogicLib? 	- are these really used?
#!define FALSE 0		# Can they be removed?
#!define TRUE 1			# Can they be removed?

# --------------------------------
# Variables

Var Dummy 		# For temporary use.  ?? Why not use $R1 etc? ??
Var OriginalFile	# Path to PDF file from LyX to be viewed
Var OriginalFileName	# ???
Var PDFFile		# Path to temporary copy of PDF used with Adobe viewer
Var Viewer		# Name of viewer for PDF files
Var OriginalTimeHigh 	# Part of latest modificaion time of shown file
Var OriginalTimeLow	# Part of latest modificaion time of shown file

#--------------------------------
#PDF vieweing

Section "View PDF file"

  InitPluginsDir #Temporary directory for PDF file

  #Command line parameters
  Call GetParameters
  Pop $OriginalFile

  #Trim quotes
  StrCpy $Dummy $OriginalFile 1
  ${if} $Dummy == '"'
    StrCpy $OriginalFile $OriginalFile "" 1
  ${endif}
  StrCpy $Dummy $OriginalFile 1 -1
  ${if} $Dummy == '"'
    StrCpy $OriginalFile $OriginalFile -1
  ${endif}

  GetFullPathName $OriginalFile $OriginalFile
  Push $OriginalFile
  Call GetFileName
  Pop $OriginalFileName

  SetOutPath $TEMP #The LyX tmpbuf should not be locked

  StrCpy $PDFFile $PLUGINSDIR\$OriginalFileName

  # Check whether the file will be opened with Adobe Reader or Adobe Acrobat
  Push $OriginalFile
  Push "shell32::FindExecutable(t s, t '', t .s)"
  CallInstDLL "$EXEDIR\System.dll" Call
  Call GetFileName
  Pop $Viewer

  ${if} $Viewer == ""
    MessageBox MB_OK|MB_ICONEXCLAMATION "No PDF viewer is installed. \
        Please install a PDF viewer such as Adobe Reader."
    Quit        
  ${endif}

  ${if} $Viewer != "AcroRd32.exe"
  ${andif} $Viewer != "Acrobat.exe"	# Non-Adobe PDF viewer used?
    # No need for special actions, just forward to ShellExecute
    # ??? ShellExecute or ExecShell ???
    ExecShell open $OriginalFile

  ${else}                               # Using Adobe PDF viewer?

    # Store modification time of the original file
    GetFileTime $OriginalFile $OriginalTimeHigh $OriginalTimeLow

    Call Open_copy_with_Adobe 		# Open a copy with Adobe

    ${do}                               # Loop until Adobe closes
      Sleep 500                         # Wait 0.5 s ???

      # What does 'a' mean??
      FileOpen $Dummy $PDFFile a	# Try opening temporary copy

      ${if} $Dummy != ""                # File no longer locked, reader closed?
        FileClose $Dummy
        Delete $PDFFile                 # Remove temporary copy
        Quit                            # and quit
      ${endif}

      # Why would the original file disappear? Maybe close Adobe then?

      # !! I wouldn't mind changing $Current... to e.g. $R0, $R1
      # !! Setting $OriginalTime.. could for clarity be done outside function.
      ${if} ${fileexists} $OriginalFile

        # Check if the original file has been modified
        GetFileTime $OriginalFile $R0 $R1 
        ${if} $OriginalTimeHigh != $R0
        ${orif} $OriginalTimeLow != $R1

          # Store modification time of the original file
          GetFileTime $OriginalFile $OriginalTimeHigh $OriginalTimeLow

          Call Open_copy_with_Adobe 	# Open a copy with Adobe

        ${endif}
      ${endif}
    ${loop}				# End of loop until Adobe closes
  ${endif}

SectionEnd

#--------------------------------
#Macros
#
# Macro: Hide the console (which console?)
# In:	COMMAND_LINE = Command that is to be executed (as a command line)
# Out:	-
#
# !! If this doesn't have to be a macro, I think it could be
# !! expanded in the new function Open_copy_with_Adobe()
# 
!macro HideConsole COMMAND_LINE

  Push `${COMMAND_LINE}`
  CallInstDLL "$EXEDIR\Console.dll" Exec
  Pop $Dummy

  ${if} $Dummy == "error"
    MessageBox MB_OK|MB_ICONSTOP "Error opening PDF file $R0."
  ${endif}

!macroend

#
# Copy a file from LyX, $OriginalFile, to a temporary file, $PDFFile,
# and then open the temporary file using pdfopen.exe. This leaves the
# original file from LyX unlocked, so that LyX can later overwrite it.
# In addition, set the variables keeping track of the modification time
# of the original file.
#
# In:  $OriginalFile	= Name of file to be copied to $PDFFile
#      $PDFFile         = Name of temporary file that is viewed
#	 $Viewer		= ?
# Out: $OriginalTimeHigh = Modification time of $OriginalFile, high part
# 	 $OriginalTimeLow  = Modification time of $OriginalFile, low part
# Note: What if LyX modifies $OriginalFile while it is copied?
# 
Function Open_copy_with_Adobe 
  ${if} ${fileexists} $PDFFile  	# Close any existing view
    !insertmacro HideConsole '"$EXEDIR\pdfclose.exe" --file "$PDFFile"'
  ${endif}

  # Copy $OriginalFile to $PDFFile
  CopyFiles /SILENT $OriginalFile $PDFFile

  # Open a new view
  !insertmacro HideConsole '"$EXEDIR\pdfopen.exe" --back --file "$PDFFile"'

FunctionEnd

#
# Remarks (m.w.) 
# In Nsis Version 2.45 functions from "FileFunc.nsh" are called differently
# change syntax from lines using 
#    push, call, pop 
# by 
#    ${GetFileName} $OriginalFile $Viewer
#

Category: Development, Windows installer

Edit - History - Print - Recent Changes - All Recent Changes - Search
Page last modified on 2009-10-02 15:26 UTC