I want to install files in different folders, depending on whether the user has selected to install for all users or just the current user.
I have added used CreateInputOptionPage() to create an option page with two radio buttons.
However, my installer is now littered with lots of duplicate lines, like these two:
Source: {#ProjectRootFolder}\License.txt; DestDir: {userdocs}\{#MyAppName}; Check: NOT IsAllUsers
Source: {#ProjectRootFolder}\License.txt; DestDir: {commondocs}\{#MyAppName}; Check:IsAllUsers
Is there a more elegant way to do the above? Can Pascal code, for example, create a variable like #define does so I can use it in place of {userdocs} and {commondocs} above?
Further details:
The IsAllUsers() function above calls this code:
function IsAllUsers: Boolean;
begin
#ifdef UPDATE
Result := AllUsersInRegistryIsTRUE;
#else
Result := AllUsersOrCurrentUserPage.Values[1]; // wizard page second radio button
#endif
end;
and:
function AllUsersInRegistryIsTRUE: Boolean; // True if preceding install was to all users' documents
var
AllUsersRegValue: AnsiString;
begin
if RegQueryStringValue(HKEY_LOCAL_MACHINE, 'Software\MyApp', 'AllUsers', AllUsersRegValue) then
Result := (UpperCase(AllUsersRegValue) = 'YES')
else
Result := FALSE;
end;
Will something like this suit?
[Files]
Source: {#ProjectRootFolder}\License.txt; DestDir: {code:GetDir}\{#MyAppName};
...
[Code]
var
OptionsPage: TInputOptionWizardPage;
procedure InitializeWizard;
begin
OptionsPage := CreateInputOptionPage(wpUserInfo,
'please select', 'the kind of installation', 'and continue..',
True, False);
OptionsPage.Add('All users');
OptionsPage.Values[0] := True;
OptionsPage.Add('This user');
end;
function GetDir(Dummy: string): string;
begin
if OptionsPage.Values[0] then
Result := ExpandConstant('{commondocs}')
else
Result := ExpandConstant('{userdocs}');
end;
Related
I would like to create a custom Inno Setup page that will be displayed at the very beginning of installation, with 3 radio buttons to select from. That selection should set the value in #define BinVer. Currently I'm forced to make 3 separate installers, but I prefer to have just one with such feature. I'm setting it up like that for each exe:
#define BinVer "111111", in another compilation I change it to #define BinVer "222222", and finaly I have #define BinVer "333333" for the third compilation. I'm using this define in such situations li ke that:
procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
case CurUninstallStep of
usPostUninstall:
begin
CreateDir(ExpandConstant('{app}\bin\{#BinVer}\files'));
end;
end;
end;
or
Source: "{#CompPath}\Folder\sed.exe"; DestDir: "{app}\bin\{#BinVer}\files\gui\unbound\"; Flags: deleteafterinstall
So far I have tried something like this, but it's just very beginning, not sure what to do next, I'm not that good with programming it:
[Code]
procedure InitializeWizard();
var
Page: TInputOptionWizardPage;
begin
Page := CreateInputOptionPage(wpWelcome, '', '', '', False, False);
Page.AddEx('Radio button 1', 0, True);
Page.AddEx('Radio button 2', 0, True);
Page.AddEx('Radio button 3', 0, True);
end;
Also tried something like that (but I don't know if this is correct way to achieve this, it's not even compiling without errors):
[Code]
var
BinVer: string;
function InitializeSetup(): Boolean;
var
Page: TInputOptionWizardPage;
begin
Page := CreateInputOptionPage(wpWelcome, 'Select BinVer', 'Select BinVer', '', False, False);
Page.Add('BinVer 1', '111111');
Page.Add('BinVer 2', '222222');
Page.Add('BinVer 3', '333333');
Page.DefaultValueIndex := 0; // initially select the first radio button
Result := True;
end;
function NextButtonClick(CurPageID: Integer): Boolean;
var
Page: TInputOptionWizardPage;
begin
Result := True;
if CurPageID = wpWelcome then
begin
Page := GetInputOptionPage(wpWelcome);
case Page.SelectedValueIndex of
0: BinVer := '111111';
1: BinVer := '222222';
2: BinVer := '333333';
end;
end;
end;
You cannot change a value of a preprocessor variable (used on compile-time) on runtime.
But you do not have to. You want to use the value on runtime from Pascal Code. And you already have the value stored to runtime Pascal variable (BinVer: string). So use that:
CreateDir(ExpandConstant('{app}\bin\' + BinVer + '\files'));
Though you do not really need to store the value to the global variable either. You can query the "InputOptionPage" when needed.
Example of full code, linking the selection to the [Files] section entry destination:
[Files]
Source: "{#CompPath}\Folder\sed.exe"; \
DestDir: "{app}\bin\{code:GetBinVer}\files\gui\unbound\"; \
Flags: deleteafterinstall
[Code]
var
InputOptionPage: TInputOptionWizardPage;
procedure InitializeWizard();
begin
InputOptionPage := CreateInputOptionPage(wpWelcome, '', '', '', False, False);
InputOptionPage.AddEx('Radio button 1', 0, True);
InputOptionPage.AddEx('Radio button 2', 0, True);
InputOptionPage.AddEx('Radio button 3', 0, True);
// initially select the first radio button
InputOptionPage.SelectedValueIndex := 0;
end;
function GetBinVer(Param: string): string;
begin
case InputOptionPage.SelectedValueIndex of
0: BinVer := '111111';
1: BinVer := '222222';
2: BinVer := '333333';
else RaiseException('Unexpected selection');
end;
end;
(not tested)
Though linking it to a code in CurUninstallStepChanged is more complicated, that happens in another (uninstaller) process. So you will have to store the selection/BinVer somewhere (e.g. Windows Registry), where you will load it from in the uninstaller. You might by able to use SetPreviousData/GetPreviousData.
I am preparing an installer with Inno Setup. But I'd like to add an additional custom (none of the available parameters) command line parameters and would like to get the value of the parameter, like:
setup.exe /do something
Check if /do is given, then get the value of something. Is it possible? How can I do this?
With InnoSetup 5.5.5 (and perhaps other versions), just pass whatever you want as a parameter, prefixed by a /
c:\> myAppInstaller.exe /foo=wiggle
and in your myApp.iss:
[Setup]
AppName = {param:foo|waggle}
The |waggle provides a default value if no parameter matches. Inno setup is not case sensitive. This is a particularly nice way to handle command line options: They just spring into existence. I wish there was as slick a way to let users know what command line parameters the installer cares about.
BTW, this makes both #knguyen's and #steve-dunn's answers somewhat redundant. The utility functions do exactly what the built-in {param: } syntax does.
Further to #DanLocks' answer, the {param:*ParamName|DefaultValue*} constant is documented near the bottom of the Constants page:
http://www.jrsoftware.org/ishelp/index.php?topic=consts
I found it quite handy for optionally suppressing the license page. Here is all I needed to add (using Inno Setup 5.5.6(a)):
[code]
{ If there is a command-line parameter "skiplicense=true", don't display license page }
function ShouldSkipPage(PageID: Integer): Boolean;
begin
Result := False
if PageId = wpLicense then
if ExpandConstant('{param:skiplicense|false}') = 'true' then
Result := True;
end;
Inno Setup directly supports switches with syntax /Name=Value using {param} constant.
You can use the constant directly in sections, though this use is quite limited.
An example:
[Registry]
Root: HKCU; Subkey: "Software\My Company\My Program\Settings"; ValueType: string; \
ValueName: "Mode"; ValueData: "{param:Mode|DefaultMode}"
You will more likely want to use switches in Pascal Script.
If your switch has the syntax /Name=Value, the easiest way to read its value is using ExpandConstant function.
For example:
if ExpandConstant('{param:Mode|DefaultMode}') = 'DefaultMode' then
begin
Log('Installing for default mode');
end
else
begin
Log('Installing for different mode');
end;
If you want to use a switch value to toggle entries in sections, you can use Check parameter and a auxiliary function, like:
[Files]
Source: "Client.exe"; DestDir: "{app}"; Check: SwitchHasValue('Mode', 'Client')
Source: "Server.exe"; DestDir: "{app}"; Check: SwitchHasValue('Mode', 'Server')
[Code]
function SwitchHasValue(Name: string; Value: string): Boolean;
begin
Result := CompareText(ExpandConstant('{param:' + Name + '}'), Value) = 0;
end;
Ironically it is more difficult to check for a mere presence of switch (without a value).
Use can use a function CmdLineParamExists from #TLama's answer to Passing conditional parameter in Inno Setup.
function CmdLineParamExists(const Value: string): Boolean;
var
I: Integer;
begin
Result := False;
for I := 1 to ParamCount do
if CompareText(ParamStr(I), Value) = 0 then
begin
Result := True;
Exit;
end;
end;
You can obviously use the function in Pascal Script:
if CmdLineParamExists('/DefaultMode') then
begin
Log('Installing for default mode');
end
else
begin
Log('Installing for different mode');
end;
But you can even use it in sections, most typically using Check parameter:
[Files]
Source: "MyProg.hlp"; DestDir: "{app}"; Check: CmdLineParamExists('/InstallHelp')
A related problem:
Add user defined command line parameters to /? window
If you want to parse command line arguments from code in inno, then use a method similar to this. Just call the inno script from the command line as follows:
c:\MyInstallDirectory>MyInnoSetup.exe -myParam parameterValue
Then you can call the GetCommandLineParam like this wherever you need it:
myVariable := GetCommandLineParam('-myParam');
{ ================================================================== }
{ Allows for standard command line parsing assuming a key/value organization }
function GetCommandlineParam (inParam: String):String;
var
LoopVar : Integer;
BreakLoop : Boolean;
begin
{ Init the variable to known values }
LoopVar :=0;
Result := '';
BreakLoop := False;
{ Loop through the passed in arry to find the parameter }
while ( (LoopVar < ParamCount) and
(not BreakLoop) ) do
begin
{ Determine if the looked for parameter is the next value }
if ( (ParamStr(LoopVar) = inParam) and
( (LoopVar+1) <= ParamCount )) then
begin
{ Set the return result equal to the next command line parameter }
Result := ParamStr(LoopVar+1);
{ Break the loop }
BreakLoop := True;
end;
{ Increment the loop variable }
LoopVar := LoopVar + 1;
end;
end;
This is the function I wrote, which is an improvement of Steven Dunn's answer. You can use it as:
c:\MyInstallDirectory>MyInnoSetup.exe /myParam="parameterValue"
myVariable := GetCommandLineParam('/myParam');
{ util method, equivalent to C# string.StartsWith }
function StartsWith(SubStr, S: String): Boolean;
begin
Result:= Pos(SubStr, S) = 1;
end;
{ util method, equivalent to C# string.Replace }
function StringReplace(S, oldSubString, newSubString: String): String;
var
stringCopy: String;
begin
stringCopy := S; { Prevent modification to the original string }
StringChange(stringCopy, oldSubString, newSubString);
Result := stringCopy;
end;
{ ================================================================== }
function GetCommandlineParam(inParamName: String): String;
var
paramNameAndValue: String;
i: Integer;
begin
Result := '';
for i := 0 to ParamCount do
begin
paramNameAndValue := ParamStr(i);
if (StartsWith(inParamName, paramNameAndValue)) then
begin
Result := StringReplace(paramNameAndValue, inParamName + '=', '');
break;
end;
end;
end;
Yes it is possible, you can use the ParamStr function in PascalScript to access all the commandline parameters. The ParamCount function will give you the number of commandline parameters.
Another possibility is to use GetCmdTail
In response to:
"With InnoSetup 5.5.5 (and perhaps other versions), just pass whatever you want as a parameter, prefixed by a /"
"#NickG, yes, every constant you can expand by the ExpandConstant function"
This is not the case. Trying to use a command line parameter in ExpandConstant in InnoSetup 5.5.6 results in a runtime error.
PS: I would have added a comment directly but apparently I dont have enough "reputation"
I've modified a little bit knguyen's answer. Now it's case insensitive (you can write en console /myParam or /MYPARAM) and it can accept default value. Also I fixed the case when you receive larger parameter then expected (for ex: /myParamOther="parameterValue" in place of /myParam="parameterValue". Now myParamOther doesn't match).
function GetCommandlineParam(inParamName: String; defaultParam: String): String;
var
paramNameAndValue: String;
i: Integer;
begin
Result := defaultParam;
for i := 0 to ParamCount do
begin
paramNameAndValue := ParamStr(i);
if (Pos(Lowercase(inParamName)+'=', AnsiLowercase(paramNameAndValue)) = 1) then
begin
Result := Copy(paramNameAndValue, Length(inParamName)+2, Length(paramNameAndValue)-Length(inParamName));
break;
end;
end;
end;
I found the answer: GetCmdTail.
You can pass parameters to your installer scripts. Install the Inno Setup Preprocessor and read the documentation on passing custom command-line parameters.
How Should i modify my code so i can let user detemine the path of second directory (path of java in script)
I used this Command befor i try in mode:
mysetup.exe /DIR="C:\test"
installation path
second path
And how to let user to choose one of these Component1 or component2 or both.
choose component to be installed
#define AppName "My App"
[Setup]
AppName={#AppName}
AppVersion=1
DefaultDirName={code:getInstallDir}\{#AppName}
;DefaultDirName={pf}\My App
DisableDirPage=yes
[Files]
[Code]
#include 'System.iss'
var
Page: TInputDirWizardPage;
UsagePage: TInputOptionWizardPage;
function InputDirPageNextButtonClick(Sender: TWizardPage): Boolean;
begin
{ Use the first path as the "destination path" }
WizardForm.DirEdit.Text := Page.Values[0];
Result := True;
end;
procedure InitializeWizard;
begin
Page := CreateInputDirPage(wpWelcome,
'Destination', '',
'Where should App be installed?',
False, 'New Folder');
Page.Add('App path');
Page.Values[0] := WizardForm.DirEdit.Text;
UsagePage := CreateInputOptionPage(wpWelcome,
'Installation', 'choose component',
'please choose one!:',
False, False);
UsagePage.Add('Component1');
UsagePage.Add('Component2');
Page.OnNextButtonClick := #InputDirPageNextButtonClick;
Page := CreateInputDirPage(wpSelectDir,
'Java path', '',
'please specify Java Folder:', False, '');
Page.Add('Java');
Page.OnNextButtonClick := #InputDirPageNextButtonClick;
end;
If I understand your question: You are looking for a way to use a custom command-line parameter to populate the directory value on a custom directory page. Here's one way:
[Code]
var
CustomPage: TInputDirWizardPage;
CustomPath: string;
function InitializeSetup(): Boolean;
begin
result := true;
CustomPath := ExpandConstant('{param:CustomPath}');
end;
procedure InitializeWizard();
begin
CustomPage := CreateInputDirPage(wpSelectDir,
'Select Custom Path',
'What custom path do you want?',
'Select a directory.',
false,
'');
CustomPage.Add('Custom path:');
CustomPage.Values[0] := CustomPath;
end;
With this in your [Code] section, running the setup program with a /CustomPath="directory name" parameter will set the value on the form to the parameter from the command line.
How can I pass a variable from [Code] section to parameters in [Run] section in Inno Setup?
Basically, I want to do the following.
Get and save user input to a variable in a procedure InitializeWizard.
Pass the user input to an executable in [Run] section
Here is my code.
[Run]
Filename: "someProgram.exe"; Parameters: ??userInput??
[Code]
procedure InitializeWizard;
var
ConfigPage: TInputQueryWizardPage;
UserInput: String;
begin
{ Create the page }
ConfigPage :=
CreateInputQueryPage(
wpWelcome, 'User input', 'User input',
'Please specify the following information, then click Next.');
{ Add items (False means it's not a password edit) }
ConfigPage.Add('Input here:', False);
{ Set initial values (optional) }
ConfigPage.Values[0] := ExpandConstant('hello');
{ Read values into variables }
UserInput := ConfigPage.Values[0];
end;
Thanks.
You're looking for the scripted constant. See the following example:
[Run]
Filename: "SomeProgram.exe"; Parameters: {code:GetParams}
[Code]
var
ConfigPage: TInputQueryWizardPage;
function GetParams(Value: string): string;
begin
Result := ConfigPage.Values[0];
end;
procedure InitializeWizard;
begin
{ Create the page }
ConfigPage :=
CreateInputQueryPage(
wpWelcome, 'User input', 'User input',
'Please specify the following information, then click Next.');
{ Add items (False means it's not a password edit) }
ConfigPage.Add('Input here:', False);
{ Set initial values (optional) }
ConfigPage.Values[0] := ExpandConstant('hello');
end;
I am new to Inno Setup and I have already read the documentation. Now I know that Inno Setup can accept different/custom parameter and could be processed via Pascal script. But the problem is, I don't know how to write in Pascal.
I am hoping I could get help about the coding.
I'd like to pass /NOSTART parameter to my setup file which while tell the setup to disable(uncheck) the check mark on "Launch " and if /NOSTART is not provided, it it will enable(check) the check mark "Launch "
or if possible, that Launch page is not required and do everything via code.
Since you can't imperatively modify flags for section entries and directly accessing the RunList would be quite a dirty workaround, I'm using for this two postinstall entries, while one has no unchecked flag specified and the second one has. So, the first entry represents the checked launch check box and the second one unchecked launch check box. Which one is used is controlled by the Check parameter function, where is checked if a command line tail contains /NOSTART parameter.
Also, I've used a little more straightforward function for determining if a certain parameter is contained in the command line tail. It uses the CompareText function to compare text in a case insensitive way. You can replace it with CompareStr function, if you want to compare the parameter text in a case sensitive way. Here is the script:
[Setup]
AppName=My Program
AppVersion=1.5
DefaultDirName={pf}\My Program
OutputDir=userdocs:Inno Setup Examples Output
[Run]
Filename: "calc.exe"; Description: "Launch calculator"; \
Flags: postinstall nowait skipifsilent; Check: LaunchChecked
Filename: "calc.exe"; Description: "Launch calculator"; \
Flags: postinstall nowait skipifsilent unchecked; Check: not LaunchChecked
[Code]
function CmdLineParamExists(const Value: string): Boolean;
var
I: Integer;
begin
Result := False;
for I := 1 to ParamCount do
if CompareText(ParamStr(I), Value) = 0 then
begin
Result := True;
Exit;
end;
end;
function LaunchChecked: Boolean;
begin
Result := not CmdLineParamExists('/NOSTART');
end;
and so a little research read and read .. i got my answer.
here's my code (except the "GetCommandLineParam")
[Code]
{
var
StartNow: Boolean;
}
function GetCommandLineParam(inParam: String): String;
var
LoopVar : Integer;
BreakLoop : Boolean;
begin
{ Init the variable to known values }
LoopVar :=0;
Result := '';
BreakLoop := False;
{ Loop through the passed in arry to find the parameter }
while ( (LoopVar < ParamCount) and
(not BreakLoop) ) do
begin
{ Determine if the looked for parameter is the next value }
if ( (ParamStr(LoopVar) = inParam) and
( (LoopVar+1) <= ParamCount )) then
begin
{ Set the return result equal to the next command line parameter }
Result := ParamStr(LoopVar+1);
{ Break the loop }
BreakLoop := True;
end;
{ Increment the loop variable }
LoopVar := LoopVar + 1;
end;
end;
{
function InitializeSetup(): Boolean;
var
NOSTART_Value : String;
begin
NOSTART_Value := GetCommandLineParam('/NOSTART');
if(NOSTART_Value = 'false') then
begin
StartNow := True
end
else
begin
StartNow := False
end;
Result := True;
end;
}
procedure CurStepChanged(CurStep: TSetupStep);
var
Filename: String;
ResultCode: Integer;
NOSTART_Value : String;
begin
if CurStep = ssDone then
begin
NOSTART_Value := GetCommandLineParam('/NOSTART');
if(NOSTART_Value = 'false') then
begin
Filename := ExpandConstant('{app}\{#MyAppExeName}');
Exec(Filename, '', '', SW_SHOW, ewNoWait, Resultcode);
end
end;
end;
a code update. Thanks to #TLama
function CmdLineParamExists(const Value: string): Boolean;
var
I: Integer;
begin
Result := False;
for I := 1 to ParamCount do
if CompareText(ParamStr(I), Value) = 0 then
begin
Result := True;
Break;
end;
end;
procedure CurStepChanged(CurStep: TSetupStep);
var
Filename: String;
ResultCode: Integer;
NOSTART_Value : String;
RunApp : Boolean;
begin
if CurStep = ssDone then
begin
RunApp := CmdLineParamExists('/START');
if(RunApp = True) then
begin
Filename := ExpandConstant('{app}\{#MyAppExeName}');
Exec(Filename, '', '', SW_SHOW, ewNoWait, Resultcode);
end
// NOSTART_Value := GetCommandLineParam('/START');
// if(NOSTART_Value = 'true') then
// begin
// Filename := ExpandConstant('{app}\{#MyAppExeName}');
// Exec(Filename, '', '', SW_SHOW, ewNoWait, Resultcode);
//end
end;
end;
How about the following, easy to read
; Script generated by the Inno Script Studio Wizard.
; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!
#define MyAppName "Install Specialty Programs"
#define MyAppVersion "1.0"
#define MyAppPublisher ""
[Setup]
; NOTE: The value of AppId uniquely identifies this application.
; Do not use the same AppId value in installers for other applications.
; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
AppId={{5}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
;AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
DefaultDirName={pf}\{#MyAppName}
DisableDirPage=yes
DefaultGroupName={#MyAppName}
DisableProgramGroupPage=yes
OutputDir=P:\_Development\INNO Setup Files\Specialty File Install
OutputBaseFilename=Specialty File Install
Compression=lzma
SolidCompression=yes
[Languages]
Name: "english"; MessagesFile: "compiler:Default.isl"
[Files]
Source: "P:\_Development\INNO Setup Files\Specialty File Install\Files\0.0 - Steps.docx"; DestDir: "c:\support\Specialty Files"; Tasks: V00Step
[Tasks]
Name: "Office2013"; Description: "Running Office 2013"; Flags: checkablealone unchecked
Name: "Office2016"; Description: "Running Office 2016"; Flags: checkablealone unchecked
Name: "V00Step"; Description: "Steps To Follow (Read Me)"; Flags: exclusive
[Run]
Filename: "C:\Program Files (x86)\Microsoft Office\Office15\WINWORD.EXE"; Parameters: """c:\support\Specialty Files\0.0 - Steps.docx"""; Description: "Run if Office 2013 is installed"; Tasks: V00Step AND Office2013
Filename: "C:\Program Files (x86)\Microsoft Office\Office16\WINWORD.EXE"; Parameters: """c:\support\Specialty Files\0.0 - Steps.docx"""; Description: "Run if Office 2016 is installed"; Tasks: V00Step AND Office2016