Search code examples
nsiselectron-builder

NSIS - Get command line argument and store it in a text file


I build an app in electron and making exe using electron-builder. I need a URL field during installation so I wrote a script that ask for URL field and store it in a credentials.txt file. Installing manually(double clicking) is working fine. enter image description here

Now what I want to achieve is, install exe using command line and pass URL as parameter and create credentials.txt and store URL value in it.

setup.exe /S /URL="https://hello.com"

Here is the script

!include nsDialogs.nsh
!include LogicLib.nsh
!include FileFunc.nsh

XPStyle on

Var Dialog
Var AppURL
Var URLText
Var URLState
Var URL

Page custom nsDialogsPage nsDialogsPageLeave

Function nsDialogsPage

    nsDialogs::Create 1018
    Pop $Dialog

    ${If} $Dialog == error
        Abort
    ${EndIf}

    ${NSD_CreateLabel} 0 0 100% 12u "App URL:"
    Pop $AppURL

    ${NSD_CreateText} 0 13u 100% 12u $URLState
    Pop $URLText

    nsDialogs::Show

FunctionEnd

Function nsDialogsPageLeave

    ${NSD_GetText} $URLText $URLState

    ${If} $URLState == ""
        MessageBox MB_OK "App URL is missing."
        Abort
    ${EndIf}

    StrCpy $1 $URLState

    ${GetParameters} $0
    ${GetOptions} "$0" "/URL=" $URL   ////HERE I'm trying to get argument

    ${If} $URL != ""
        StrCpy $1 "$URL"             /// HERE trying to copy $URL value
    ${EndIf}

    FileOpen $9 $INSTDIR\credentials.txt w
    FileWrite $9 "$1"
    FileClose $9
    SetFileAttributes $INSTDIR\credentials.txt HIDDEN|READONLY

FunctionEnd


Solution

  • Only .onInit and Sections are executed in silent installers, not the pages.

    Var URL
    
    !include LogicLib.nsh
    !include nsDialogs.nsh
    !include FileFunc.nsh
    
    Page Custom MyPageCreate MyPageLeave
    Page InstFiles
    
    Function .onInit
    ${GetParameters} $0
    ${GetOptions} "$0" "/URL=" $URL
    ; If the URL is required, check IfSilent and abort if empty here
    FunctionEnd
    
    Function MyPageCreate
    nsDialogs::Create 1018
    Pop $0
    ${NSD_CreateText} 0 13u 100% 12u "$URL"
    Pop $1
    nsDialogs::Show
    FunctionEnd
    
    Function MyPageLeave
    ${NSD_GetText} $1 $URL
    ${If} $URL == ""
        MessageBox MB_OK "App URL is missing."
        Abort
    ${EndIf}
    FunctionEnd
    
    Section
    DetailPrint "TODO: Write $URL to a file here..."
    SectionEnd