I'm using ArcGIS for Desktop

I have a script tool which requires a parameter (folder path to a gdB, via GetParameterAsText) so that a later process can be saved there. The thing I want to do is make an Optional Boolean parameter, that when checked, automatically fills in the folder path gdB parameter. The Boolean text asks the user, "Use default workspace?", to which if they check the box (boolean == True), then the folder path parameter will be automatically defaulted (saving the user a small amount of time). I have already created the Boolean parameter in the script tool, I am however unaware of how to "fill in" the required parameter based on the user's Boolean.

Code Snippet:

# Ask user to select the Geodatabase workspace to use for data output
userWorkspace = arcpy.GetParameterAsText(0)

# Ask user if they want to use default workspace (boolean)
useDefault = arcpy.GetParameterAsText(1)

# If user wants default workspace, then give it to the required parameter (0)
if useDefault == True:
 arcpy.env.workspace = path
 # Set workspace environment based upon user's choice
 arcpy.env.workspace = userWorkspace

5 Answers 5


Try adding a print useDefault immediately after you set it to see what is returned.

I have a suspicion (without testing) that it may be returning 'True'(or 'False') rather than 'true' (or 'false').

Also, arcpy.GetParameterAsText() always returns a string so you need to test for the String of 'True'(or 'False').

If you use arcpy.GetParameter() then you will be able to test for the Booleans True (or False).


Extending @PolyGeo's answer, running this snippet from the command line and from a toolbox with a single parameter set as "boolean, optional" will give slightly different returns.

import arcpy
useDefault = arcpy.GetParameterAsText(0)

# report to Arcgis message queue

# report to shell console
print useDefault
print type(useDefault)

When run from a toolbox the message is:

<type 'unicode'>

and from a command line, python test.py True:

<type 'str'>

When we change to using arcpy.GetParameter(0) the message queue result is:

<type 'bool'>

but the command line report remains unchanged:

<type 'str'>

So the useDefault parameter is not a native python boolean. You (sadly) can't use standard python idioms like if useDefault: ..., but must test against strings:

usedDefault = GetParameterAsText(0)

if not useDefault.lower() == 'true':
    #... do "option box not checked" stuff

if useDefault.lower() == 'true':
    #... do "option box IS checked" stuff

Strictly speaking the .lower() suffix is not needed if the script will (a) always be run from a toolbox, and (b) be defined as a boolean parameter (checkbox). It's added here for clarity and flexibility.


This can be accomplished via the use of ArcGIS's Script Tool Validation: http://resources.arcgis.com/en/help/main/10.1/index.html#//00150000002m000000

def updateParameters(self):  
    """Modify the values and properties of parameters before internal 
    validation is performed.  This method is called whenever a parameter 
    has been changed."""  
    if self.params[1].value == True:  
        self.params[0].value = arcpy.env.workspace  

GUI Pic GUI Pic #2


ArcPy is a bit weird with the GetParameterAsText() for the script options. For whatever reason I couldn't test mine as a straight Boolean True/False, as you noted in your question. You can, however, try:

if str(useDefault) == 'true':
    arcpy.env.workspace = path

That should work just as you intended.


Try omitting the AsText part. Use:

useDefault = arcpy.GetParameter(1)

It won't change the boolean to a text string. It will get the actual Boolean parameter.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.