VBScript Include

From NSIS Wiki
Jump to navigationJump to search
Author: Zinthose (talk, contrib)


About

This is mostly a proof of concept that demonstrates a way to use VBScripts within your NSIS packages. I primarily developed this as a way to keep all your code that is relevant to the current project inline. When compiled, the VBScripts are dynamically created and executed. Take a look at the example and use your imagination.

Example

## Execute a script at compile time and !include the output 
    ${VBS} NEW
    ${VBS} `PackageName = Left(CreateObject("Scriptlet.TypeLib").Guid, 38)`                     ## Create a new Guid
    ${VBS} `PackageName = InputBox("What is the Package Name?", "NSIS VBScript", PackageName)`  ## Prompt programmer for Package Name with a new guid as the default
    ${VBS} `WScript.Echo "!define PACKAGENAME '" & PackageName & "'"`                           ## Echo message to file to included
    ${VBS} !Include
 
## Execute a script at compile time
    ${VBS} NEW
    ${VBS} `WScript.echo "Hello Programmer!"`   ## Greet the programmer!
    ${VBS} !Execute
 
## Execute a vbscript at runtime
    ${VBS} NEW
    ${VBS} `UsersName = InputBox("What is your Name?", "${PACKAGENAME}")`                   ## Display an input box to the user to ask for name
    ${VBS} `WScript.Echo "User claims his/her name is " & UsersName`                        ## This will echo to the detail view
    ${VBS} `MsgBox "Hello " & UsersName, 0, "Hello User"`                                   ## Greet the user!
    ${VBS} `If MsgBox("Do you want to win the lottery?", 4, "${PACKAGENAME}") = 6 Then`     ## Ask the user a question
    ${VBS} `    WScript.Echo "User wants to win the lottery"`                               
    ${VBS} `Else`
    ${VBS} `    WScript.Echo "User is a liar!"`
    ${VBS} `End If`
    ${VBS} Execute
 
    ## Get the results from the script's execution (Uses nsExec::Exec syntax)
    Pop $0
    DetailPrint `RC:$0`

vbscript.nsi

!ifndef __VBSCRIPT__
!define __VBSCRIPT__ 1
 
/*  vbscript.nsi
    ----------------------------------------------------------------
    Simplifies execution of VBScripts from nsis.  
    This is targeted to be primarily used as part of the precompiler 
    but has limited support for runtim use although not recommended.
    ----------------------------------------------------------------*/
 
!macro _VBS_New
    !ifdef _VBScriptFile
        !delfile "${_VBScriptFile}"
        !undef _VBScriptFile
    !endif
    !tempfile _VBScriptFile
    !verbose 4
    !echo `VBS NEW = ${_VBScriptFile}`
    !verbose 3
!macroend
 
!macro _VBS_Append _ScriptLine
    !ifndef _VBScriptFile
        !insertmacro _VBS_New
    !endif
    !verbose 4
    !echo `VBS <-- ${_ScriptLine}`
    !verbose 3
    !appendfile "${_VBScriptFile}" `${_ScriptLine}$\n`
!macroend
 
!macro _VBS_Include
    !ifdef _OutFile
        !undef _OutFile
    !endif
    !ifndef _VBScriptFile
        !error "No script defined!"
    !endif
    !tempfile _OutFile
    !system `"%windir%\System32\CScript.exe" "${_VBScriptFile}" //E:VBS //NOLOGO > "${_OutFile}"`
    !include "${_OutFile}"
    !delfile "${_OutFile}"
    !verbose 4
    !echo `VBS INC ${_VBScriptFile}`
    !verbose 3
!macroend
 
!macro _VBS_Execute
    !ifndef _VBScriptFile
        !error "No script defined!"
    !endif
    !system `"%windir%\System32\wScript.exe" "${_VBScriptFile}" //E:VBS //NOLOGO`
!macroend
 
!macro _VBS _Operator
    !verbose push
    !verbose 3
    !if `${_Operator}` == `NEW`
        !insertmacro _VBS_New
    !else if `${_Operator}` == `!INCLUDE`
        !insertmacro _VBS_Include
    !else if `${_Operator}` == `!EXECUTE`
        !insertmacro _VBS_Execute
    !else if `${_Operator}` == `EXECUTE`
        !ifndef _VBScriptFile
            !error "No script defined!"
        !endif
        !ifndef __VBS__EXECUTE_FIRSTRUN__
            !define __VBS__EXECUTE_FIRSTRUN__
            !warning "It is not recomended to execute scripts at runtime as antivirus software will likley block script execution."
            InitPluginsDir
        !endif
        File /oname=$PLUGINSDIR\vbstmp.vbs `${_VBScriptFile}`
        nsExec::ExecToLog `cScript.exe "$PLUGINSDIR\vbstmp.vbs" //E:VBS //NOLOGO`
    !else
        !insertmacro _VBS_Append `${_Operator}`
    !endif
    !verbose pop
!macroend
!define VBS "!insertmacro _VBS"
 
!endif