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
  • 1
    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 'True'/'False' rather than 'true'/'false'. – PolyGeo Jul 9 '15 at 22:11
  • @PolyGeo - Yes, thank you for correcting me on that. It is indeed True. Will give your suggestion a try, thanks. – JumpInTheFire Jul 9 '15 at 22:17

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).

  • 1
    Also note the type changes from string on command line to unicode from toolbox. See my answer that explores PolyGeo's answer in some more detail. Bottom line: always test strings and pretend you don't know about booleans. – matt wilkie Jul 14 '15 at 17:14

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.

  • +1 Thanks for looking at this in more detail. It's a place in ArcGIS Geoprocessing that seems to me to be crying out for more consistency. – PolyGeo Jul 15 '15 at 23:54

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


This is an old question, but just to chime in - Arc 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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