Jacob Darka
Posts: 23
Joined: Mon Jun 24, 2019 7:26 pm

Install Only if App.exe Exists in Selected Path

Mon Sep 20, 2021 7:15 pm

Hello,

I'm making an update for a different program using the surface theme, that program's directory and executable is similar to the following:
D:\RandomFolder1\Random Folder2\AppDir\App.exe < The path to the AppDir folder and App.exe could be in any directory

I'm aiming for one of the two following scenarios:

- Scenario 1
  • By default, the "Install" button is unavailable.
  • The user must select the executable "App.exe" in the "Select the install folder location".
  • For the Install button to be available, the path in the "Path Edit" box must contain "App.exe".
  • After the user selects the executable, the Install button should appear automatically.
  • The update should install in the same folder that contains the executable, per my first example it should install in "D:\RandomFolder1\Random Folder2\AppDir".
Summary: The install button will appear once the user selects the program's exe, and my update should install in the same directory of the exe.
The benefit behind this: This will make selecting the correct path far more easier for the less experienced users since now they will only have to select an exe to continue with the installation.

- Scenario 2
  • By default, the "Install" button is unavailable.
  • Once the user selects a path in the "Path Edit" box, Advanced Installer should check that selected path if it contains "App.exe" or not.
  • If the selected path does not contain the executable, a pop-up message should appear stating that the selected path is wrong, and the install button should remain unavailable until a correct path is given.
  • If the selected path contains the executable, the install button should appear and the user can continue on with the installation.
Summary: If the select install path by the user doesn't contain the "App.exe", show a warning, otherwise continue on with the installation.
The benefit behind this: This scenario isn't as straightforward as selecting an exe to continue, but showing a warning message upon choosing a wrong installation path is almost as helpful as scenario 1.

Whatever scenario is possible and easier to achieve I'm okay with.

It took me a bit to make sure that I explained what I'm aiming for correctly, so hopefully, I succeeded in that.

Thanks in advance,
Jacob

Liviu
Posts: 1026
Joined: Tue Jul 13, 2021 11:29 am
Contact:  Website

Re: Install Only if App.exe Exists in Selected Path

Tue Sep 21, 2021 12:15 pm

Hi Jacob,

You can achieve this by using our "Browse for file" custom action and a PowerShell script. Just follow these steps:

1. From the "Properties" page, create 2 properties like this:
  • Name: MY_FILEPATH
    Value: [|]

    (this means empty value which will be displayed on the EditBox control)
  • Name: StringSearch
    Value: notfound
properties.png
properties.png (7.52KiB)Viewed 12774 times

2. Go to the "Custom Actions" page and add these custom actions without sequence:
customActions.png
customActions.png (6.92KiB)Viewed 12774 times
  • BrowseForFile
  • MessageBox with the message: "File not found!".
  • PowerShellScriptInline with the following code:

    Code: Select all

    <#
    .NOTES
      "pwsh.exe" is run if required version is greater or equal to 6, otherwise
      "powershell.exe" is invoked by default
    #>
    
    #Requires -version 3
    Param()
    
    # your code goes here
    
    # we are setting the path of the selected file into prop variable to search the "app.exe" text in this string
    $prop = AI_GetMsiProperty MY_FILEPATH
    
    If ($prop  | %{$_ -match "app.exe"}) 
    {
    	# setting a property
    	AI_SetMsiProperty StringSearch "found"
    	
    	# here we get the app.exe folder directory since we want to set the install directory in this folder. 
    	$path = [System.IO.Path]::GetDirectoryName($prop)
    
    	AI_SetMsiProperty APPDIR $path
    
    }
    else
    {
    	# setting a property
    	AI_SetMsiProperty StringSearch "notfound"
    }
    
    In this code we search in the path of the selected file for the "app.exe" text and then we set the APPDIR to the directory of the selected file.
3. Now from the "Dialogs" page add a new dialog after the "WelcomeDlg". Add the following controls:
  • "Static text" to display some information.
  • "PushButton" and set the Display Text to "[ButtonText_Browse]".
  • "EditBox" and set the Property name to "MY_FILEPATH":
    EditBox.png
    EditBox.png (42.9KiB)Viewed 12774 times
4. Click on the "Browse.." button you added in the previous step and from the "Published Events" add the following events like in the screenshot:
BrowseEvents.png
BrowseEvents.png (43KiB)Viewed 12774 times
5. Now we need to condition the "Next" button to be enable only when the user selects the specified file. To do that, click on the "Next" button and in the "Control Conditions" tab add the following conditions:
  • Condition: StringSearch = "notfound"
    Action: Disable
  • Condition: StringSearch = "found"
    Action: Enable
    NextConditions.png
    NextConditions.png (12.1KiB)Viewed 12774 times
    Also, from the "Published Events" change the condition to "StringSearch = "found":
    NextButton.png
    NextButton.png (11.68KiB)Viewed 12774 times
6. You can delete the "FolderDlg" dialog since we already set the APPDIR to the "app.exe" file directory. This way the user can not change the install location after we check that the file is selected.

After that, when the user is installing the package on this dialog will be prompted to select the file you specified, in this case "app.exe", the Next button is disabled and will be enabled only when the "app.exe" file is selected. Also the APPDIR is set to the file directory since you want to install your update there.

Attached is the sample project I've made, have a look on it and let me know if this helped you.

Best regards,
Liviu
Attachments
Demo Path.aip
(20.38KiB)Downloaded 375 times
________________________________________
Liviu Sandu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Jacob Darka
Posts: 23
Joined: Mon Jun 24, 2019 7:26 pm

Re: Install Only if App.exe Exists in Selected Path

Wed Sep 22, 2021 12:36 am

Hello Liviu,

You actually combined the two scenarios, amazing! and it works flawlessly, thank you.

I had to make some changes to the properties to apply that to my surface-themed project, but thanks to your detailed guide and demo, I was able to do that with no problem.

I have two other small questions that I don't know if I should make another topic for them or not, but if that's required I'll remove them from here and do so.

Question 1:
The default width and height of the layout of the surface theme is 345x400 "Specified in installer unites":
Image
But when viewing the installer in real-time, the dimensions are very different, here is an example where the red rectangle is actually 345x400 pixels:
Image

I don't think that this is a bug and indeed is the intended behavior, but is there any way to change that? to make the value of the width and height in Advanced Installer match the width and height in pixels?

This will save me a lot of time re-editing and constantly changes the dimensions of the background and added images.

Question 2:
Is there any way to remove this header so that the upper part of the installer would be seamless as the other sides of it?
Image

Liviu
Posts: 1026
Joined: Tue Jul 13, 2021 11:29 am
Contact:  Website

Re: Install Only if App.exe Exists in Selected Path

Wed Sep 22, 2021 2:52 pm

You actually combined the two scenarios, amazing! and it works flawlessly, thank you.

I had to make some changes to the properties to apply that to my surface-themed project, but thanks to your detailed guide and demo, I was able to do that with no problem.
You are welcome, Jacob!

I'm glad this works as expected.
Question 1
...
I don't think that this is a bug and indeed is the intended behavior, but is there any way to change that? to make the value of the width and height in Advanced Installer match the width and height in pixels?
Please note that sizes of the controls inside the Installer are specified in installer units, not pixels.

You can transform Installer Units into pixels using this formula:

Code: Select all

Image pixel = (MSI Unit * 4) / 3
For example, the result in pixels for 345x400 installer units:
Width = 460 pixels
Height = 533 pixels

Note that the title bar is not included in the formula height. In the BlackSurface theme the title bar has 38 pixels height. So if you want to include the title bar in your pixels you have to add +38 height and the final result is 571 pixels height.
pixels.png
pixels.png (25.55KiB)Viewed 12733 times
titleBar.png
titleBar.png (25.13KiB)Viewed 12733 times
Question 2:
Is there any way to remove this header so that the upper part of the installer would be seamless as the other sides of it?
As for that question, I'm afraid you cannot achieve this.

Hope this helps!

Best regards,
Liviu
________________________________________
Liviu Sandu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Jacob Darka
Posts: 23
Joined: Mon Jun 24, 2019 7:26 pm

Re: Install Only if App.exe Exists in Selected Path

Wed Sep 22, 2021 3:52 pm

I got it, thank you very much Liviu.

Cheers,
Jacob

Liviu
Posts: 1026
Joined: Tue Jul 13, 2021 11:29 am
Contact:  Website

Re: Install Only if App.exe Exists in Selected Path

Wed Sep 22, 2021 5:02 pm

You are always welcome, Jacob!

Best regards,
Liviu
________________________________________
Liviu Sandu - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Building Installers”