Wernfried Domscheit
Wernfried Domscheit

Reputation: 59436

Prompt user in Inno Setup for file to be used in Shortcut

In Inno Setup I try to create this shortcut:

"C:\Program Files (x86)\MapInfo\Professional\MapInfow.exe" "{app}\DPImap.MBX"

It works fine with static text, however location of MapInfow.exe can vary so I like to ask the user for it.

This is what I did so far, however the shortcut is not created as intended

; Default value for silent installion
#define MapInfoDefault AddBackslash(GetEnv("ProgramFiles(x86)")) + "MapInfo\Professional\MapInfow.exe"

[Tasks]
Name: desktopicon; Description: {cm:CreateDesktopIcon}; GroupDescription: {cm:AdditionalIcons}; Flags: unchecked

[Icons]
Name: {group}\DPImap; Filename: {code:MapInfoExecutable} {app}\DPImap.mbx
Name: {userdesktop}\DPImap; Filename: {code:MapInfoExecutable} {app}\DPImap.mbx; Tasks: desktopicon
[Code]    

function MapInfoExecutable(Param: String): String;
var
    FileName: string;
begin
    FileName := '';
    if GetOpenFileName('Locate your MapInfo Application', FileName, ExpandConstant('{pf32}'), 'Executable (*.exe)|*.exe', 'exe') then
        Result := FileName
    else
        { Return default #MapInfoDefault if user does not provide any file }
        Result := ExpandConstant('{#MapInfoDefault}');
end;

How can I provide proper user dialog?

Upvotes: 2

Views: 868

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202118

It should be:

[Icons]
Name: {group}\DPImap; Filename: "{code:MapInfoExecutable}"; \
    Parameters: """{app}\DPImap.mbx"""

You should also cache the selected file name, otherwise you get the prompt at least twice, and probably even more times.

var
  FileName: string;

function MapInfoExecutable(Param: String): String;
begin
  if FileName = '' then
  begin
    if not GetOpenFileName(
        'Locate your MapInfo Application', FileName, ExpandConstant('{pf32}'),
        'Executable (*.exe)|*.exe', 'exe') then
    begin
      { Return default #MapInfoDefault if user does not provide any file }
      FileName := '{#MapInfoDefault}';
    end;
  end;
  Result := FileName;
end;

Or actually even better, use a custom page, rather than a dialog, which pops up at uncontrollable moment.

See Inno Setup Prompt for external file location.

And even if you like the dialog, pop it on specific page/moment of your choice, cache the selected file name to a global variable and use the variable in the MapInfoExecutable.


Note that I've removed ExpandConstant from '{#MapInfoDefault}' - It's nonsense. See Evaluate preprocessor macro on run time in Inno Setup Pascal Script.

Upvotes: 2

Related Questions