NSIS exec JScript with parameters - nsis

I'm trying to run a JScript that receives a parameter (like a cmd argument).
Consider the following JScript:
myScript.js:
var args = WScript.Arguments; //get cmd args
WScript.Echo(args[0]); //output the first argument gotten from NSIS
I'm trying to do so with the following NSIS script:
installer.nsi:
Var /GLOBAL argumentToSendForJScript
StrCpy argumentToSendForJScript "helloFromNSIS"
File 'runme.js'
Exec "wscript.exe C:\myScript.js $argumentToSendForJScript"
Problem is, the argument isn't arriving the JScript. I suspect that it is due to the fact the argument is arriving to wscript.exe instead of myScript.js, because it works if I execute the script directly from a windows command prompt instead of NSIS, like so:
myScript.js helloFromCMD
Any ideas how this could be achieved?

WScript.Arguments is not a normal javascript array, you must access the items with (123), not [123].
Section
; Create a dummy script:
InitPluginsDir
FileOpen $0 "$PluginsDir\test.js" w
FileWrite $0 "var args = WScript.Arguments;$\n"
FileWrite $0 "WScript.Echo('length='+args.length, args(0));$\n"
FileClose $0
Var /GLOBAL argumentToSendForJScript
StrCpy $argumentToSendForJScript "helloFromNSIS"
ExecWait '"WScript.exe" "$PluginsDir\test.js" $argumentToSendForJScript'
SectionEnd

Related

Passing more than one parameter to another nsi

I have an installer I inherited, and I need to pass 2 parameters to another *.nsi on install. Currently it works fine with one param, which is just a string:
ExecShell "" '"$TEMP\Setup.exe"' "Param1"
This gets read as so, from the other side:
${GetParameters} $commandLineParam
The second param is a variable ($version) that needs to be sent over:
StrCpy $version "1.1.1.0"
Just adding an additional "Param2" doesn't build
Error 13 error MSB3721: The command ""C:\Program Files (x86)\NSIS\Unicode\makensis" ... exited with code 1.
I'm sure I'm missing something syntax-wise.
MakeNSIS removes the outer-most set of quotes when it parses the .NSI file.
StrCpy $0 "blah"
StrCpy $1 '"baz"'
ExecShell "" "c:\foo\bar.exe" 'p1 "Hello World" p3 $0 $1'
will run c:\foo\bar.exe with p1 "Hello World" p3 blah "baz" as the parameters.

nsis read directory from txt files?

I am trying read the directory from text file but it did't working!
the test.txt is a two lines file ,a line per directory.
anyone can help me?
; example1.nsi
;
; This script is perhaps one of the simplest NSIs you can make. All of the
; optional settings are left to their default settings. The installer simply
; prompts the user asking them where to install, and drops a copy of example1.nsi
; there.
;--------------------------------
; The name of the installer
Name "Example1"
; The file to write
OutFile "example1.exe"
; The default installation directory
InstallDir $DESKTOP
; Request application privileges for Windows Vista
RequestExecutionLevel admin
!include "LogicLib.nsh"
!include FileFunc.nsh
ShowInstDetails show
;--------------------------------
; Pages
;--------------------------------
Function .onInit
functionend
; The stuff to install
Section "" ;No components page, name is not important
FileOpen $0 "test.txt" r
;FileSeek $4 1000 ; we want to start reading at the 1000th byte
FileRead $0 $1 ; we read until the end of line (including carriage return and new line) and save it to $1
FileRead $0 $2
;FileRead $4 $2 10 ; read 10 characters from the next line
FileClose $0 ; and close the file
DetailPrint $1
DetailPrint $2
CopyFiles $1 "D:\Desktop\taobao"
; Set output path to the installation directory.
SetOutPath $INSTDIR
SectionEnd ; end the section
FileOpen $0 "test.txt" r is problematic for two reasons.
You are not using a full path.
I'm assuming this file does not already exist on the end-users machine. You probably need to extract it from the installer before you can read it.
Another problem is that FileRead includes the newline characters in the returned string and you must remove them if you don't need them. Newline characters are not valid in paths on Windows.
; Create simple text file for this example:
!delfile /nonfatal "myexample.txt"
!appendfile "myexample.txt" "Hello$\r$\n"
!appendfile "myexample.txt" "W o r l d$\r$\n"
!include "StrFunc.nsh"
${StrTrimNewLines} ; Tell StrFunc.nsh to define this function for us
Section
SetOutPath $InstDir
InitPluginsDir ; Make sure $PluginsDir exists (it is automatically deleted by the installer when it quits)
File "/oname=$PluginsDir\data.txt" "myexample.txt" ; Extract our myexample.txt file to $PluginsDir\data.txt
FileOpen $0 "$PluginsDir\data.txt" r
FileRead $0 $1
${StrTrimNewLines} $1 $1
FileRead $0 $2
${StrTrimNewLines} $2 $2
FileClose $0
DetailPrint "Line 1=$1"
DetailPrint "Line 2=$2"
SectionEnd

NSIS push, pop and URLEncode

I'm trying to encode with URLEncode a strings in NSIS. I'm using a NSIS plugin called URLEncode. The problem is that I try encode 2 vars, but the first encoded var lost the value after I encoded the second var.
Push "${AFF_NAME}"
Call URLEncode
Pop $3
;at this point the messagebox show the result good.
MessageBox MB_OK $3
ReadRegStr $0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" "ProductName"
Push $0
Call URLEncode
Pop $0
;now after second var encoded the result of first var $3 lost the value
MessageBox MB_OK $3
Please Help I'm very new using NSIS and I dont know how do it good. I searching info but without success.
Very thanks !
I'm not aware of a URLEncode plug-in but there is a helper function on the NSIS wiki called URLEncode and it fails to preserve the registers.
You can fix it by modifying the start and end of the function so it looks like this:
; Start
Function URLEncode
System::Store S ; Save registers
Pop $0 ;param
...
Done:
Push $9
System::Store L ; Restore registers
FunctionEnd
; End

NSIS write to a file on APPDATA inside a subdir

I'm trying to append 1 line of text into a file on the $APPDATA folder which is inside of a folder that's generated randomly, so I don't know it's full path like:
C:\Users\MyUser\AppData\Roaming\MyApp\RANDOM_CRAP\config.json
While RANDOM_CRAP looks like some random string for a folder, like G4F6Hh3L.
What are my options here? Do I need to use either Search For a File or Search for a File or Directory (Alternative) ? It's a given that the only subfolder of MyApp folder is the RANDOM_CRAP folder, that contains the file I want to edit.
If there's no other way to access this file without searching for it, I've tried doing so but couldn't get this to work. (I'm very new to NSIS)
This is what I've tried (With the alternative approach):
Push "config.json"
Push "$APPDATA"
Push $0
GetFunctionAddress $0 "myCallback"
Exch $0
Push "1" ; include subfolders because my desired file is in the random folder
Push "0" ; no need the . option
Call SearchFile
Than I've copied the SearchFile code from this post and put a callback:
Function myCallback
Exch 3
Pop $R4
MessageBox MB_OK "Callback executing!"
MessageBox MB_OK "File is at : $R4"
FunctionEnd
I know that SearchFile is running (I've put a MessageBox inside) but myCallback isn't seemed to be called.
Many thanks.
If you are looking for a known file and only one directory in the path is unknown then you can probably just do a basic FindFirst search:
Section
; Create "random" folders:
CreateDirectory "$temp\MyApp\foo"
System::Call kernel32::GetTickCount()i.r1 ; random enough
CreateDirectory "$temp\MyApp\bar$1"
FileOpen $0 "$temp\MyApp\bar$1\config.json" a
FileWrite $0 '{bogus:"data"}$\n'
FileClose $0
CreateDirectory "$temp\MyApp\baz"
!include LogicLib.nsh
; Do the actual search:
StrCpy $9 "$temp\MyApp" ; The folder we are going to search in
FindFirst $0 $1 "$temp\MyApp\*"
loop:
StrCmp $1 "" done
${If} ${FileExists} "$9\$1\config.json"
DetailPrint "Found: $9\$1\config.json"
${EndIf}
FindNext $0 $1
Goto loop
done:
FindClose $0
SectionEnd

NSIS Function with more than 1 parameters

Can a NSIS Function have more than one parameter?
Why wont this code compile? If I cant have more than 1 param for a function what are my other options(disregarding using a macro)?
Compile error:
Function expects 1 parameters, got 4. Usage: Function function_name
Outfile "test.exe"
Caption ""
Name ""
# Compile Error Here: "Function expects 1 parameters, got 4. Usage: Function function_name"
Function MyFunction p1 p2 p3
DetailPrint "$p1, $p2, $p3"
FunctionEnd
Section
DetailPrint "Hello World"
SectionEnd
You have to pass parameters in registers and/or on the stack:
Function onstack
pop $0
detailprint $0
FunctionEnd
Function reg0
detailprint $0
FunctionEnd
Section
push "Hello"
call onstack
strcpy $0 "World"
call reg0
SectionEnd

Resources