Modify the value of a CheckBox control based on whether a value is found in a JSON file or not - Advanced Installer Community
Catalin
Posts: 7847
Joined: Wed Jun 13, 2018 7:49 am

Modify the value of a CheckBox control based on whether a value is found in a JSON file or not

Hello everyone,

I have the following scenario from one of our clients:
I have a checkbox that I would like to set based on a value from a JSON file on disk that is not installed by my package.
and I thought it might be useful to share a solution to this with you all, considering this can be a more common scenario than I initially thought.

There are many ways in which we can achieve such feat, but the one we'll explore today will be a PowerShell script.

Of course, for this, it would be ideal for the location of the JSON file to be known beforehand so that wen can go straight to that without having to do a search for the file.

Assume we have the following JSON file:

Code: Select all

$env:ProgramData\MyCompany\MyApp\config.json
with the following content:

Code: Select all

{
  "server": {
    "url": "https://example.com/api",
	"name": "testName",
	"version": "1.0.1"
  }
}
Let's say we want to enable/disable a checkbox based on whether the value server.url is in the config file.

Here is a little PowerShell script that does just that:

Code: Select all

$json = Get-Content "$env:ProgramData\MyCompany\MyApp\config.json" -Raw -ErrorAction SilentlyContinue | ConvertFrom-Json

$value = $json.server.url   # <-- the value you want

if ($value) { AI_SetMsiProperty SERVER_URL $value } # <-- if the value exists, set a property to that value
Let's consider that we have the following dialogs structure:
Screenshot_194.png
Screenshot_194.png (6.55 KiB) Viewed 229 times

And that our dialog containing the CheckBox control is "NewDialog_1".

Here's how the Checkbox is configured:
Screenshot_195.png
Screenshot_195.png (32.84 KiB) Viewed 229 times

In order to influence the state of this checkbox based on our property, SERVER_URL, we need to execute the custom action prior to the dialog containing the checkbox is spawned.

That would mean we have to:

0. go to "Custom Actions" page and add a PowerShell inline script, "Unsequenced"

1. in the "Dialogs" page, click on "WelcomeDlg"

2. click on the "Next" button

3. below, under "Published Events" and click "New..."

4. add the "Execute custom action" event and as argument, select the PowerShell script

5. now select the dialog containing the CheckBox

6. under "Init Events", add a new event, like so:
Screenshot_196.png
Screenshot_196.png (10.34 KiB) Viewed 229 times

Now, if the value exists in the JSON, the property will be set and as a consequence, the CheckBox will be enabled.

Hope this helps!

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Sample Projects”