Select to view content in your preferred language

Set default values for parameters in toolbox for each user?

671
2
Jump to solution
12-01-2023 02:27 PM
AlfredBaldenweck
MVP Regular Contributor

Hi all,

We have a custom toolbar that references a toolbox on a network drive somewhere. I was wondering if it's possible at all for users to set defaults for the tool individually? That is, each user's preferred values for the parameters would be saved for them.

For example, one tool zooms to PLSS Township sections. The first parameter is the user's state (California, Oregon, etc), and the second is the meridian. It's pretty annoying to always have to pick your state every time, but the tool is available for every user in our agency across the country.

I don't think this is possible, but if someone could surprise me, that'd be awesome.

1 Solution

Accepted Solutions
DavidSolari
Frequent Contributor

You can do this by:

  • Looking for a text file with config options in a known location and populating those values in the tool's validation section if they exist, and 
  • Writing the user's chosen values to said config file if no default value exists.

For the location of the file, the best practice for Windows is to use a folder with your team's name in the %APPDATA% folder, which is set per user. You can get the config file like so:

import os
from os import path
folder = path.expandvars("%APPDATA%\\AlfredSoft")
if not path.exists(folder):
    os.mkdir(folder)
config_path = path.join(folder, "tool_config.json")  # Or whatever format works for you
if path.exists(config_path):
    with open(config_path) as config:
        pass  # Work with the file here!

 As for creating or editing the config file, you should do that in the tool itself or in the validator's updateParameters method, depending on how you want to lay your code out. If you get reading working then writing shouldn't be much more work.

View solution in original post

2 Replies
DavidSolari
Frequent Contributor

You can do this by:

  • Looking for a text file with config options in a known location and populating those values in the tool's validation section if they exist, and 
  • Writing the user's chosen values to said config file if no default value exists.

For the location of the file, the best practice for Windows is to use a folder with your team's name in the %APPDATA% folder, which is set per user. You can get the config file like so:

import os
from os import path
folder = path.expandvars("%APPDATA%\\AlfredSoft")
if not path.exists(folder):
    os.mkdir(folder)
config_path = path.join(folder, "tool_config.json")  # Or whatever format works for you
if path.exists(config_path):
    with open(config_path) as config:
        pass  # Work with the file here!

 As for creating or editing the config file, you should do that in the tool itself or in the validator's updateParameters method, depending on how you want to lay your code out. If you get reading working then writing shouldn't be much more work.

AlfredBaldenweck
MVP Regular Contributor

Worked like a charm.

 

'''getParameterInfo()'''
        folder = os.path.expandvars("%APPDATA%\\Esri\ArcGISPro")
        config_path = os.path.join(folder, "zoomToTRS_Config.txt")
        # Check config file for values, use as default. 
        if os.path.exists(config_path):
            with open(config_path, "r") as file:
                    line = file.readline().split(",")
                    param0.value = line[0]
                    param1.value = line[1]
'''Execute'''

        #Save default State and Meridian Value.
        folder = os.path.expandvars("%APPDATA%\\Esri\ArcGISPro")
        config_path = os.path.join(folder, "zoomToTRS_Config.txt")
        # Write/Overwrite if the toggle is turned on.
        if parameters[6].value == True:
            with open(config_path, "w") as file:
                file.write(f"{parameters[0].valueAsText},{parameters[1].valueAsText}")

 

0 Kudos