Build a Script Tool#

After developing the script, we can now build it into a script tool.

1. Script tool in custom toolbox#

Like a ModelBuilder model, a script tool also exists in a toolbox.

2. Create a new script tool#

create_script

3. Configure the “General” tab#

  • Name: for operation (alphanumeric only and no spaces)

  • Label: for display (all characters allowed)

  • Link the script (.py file) to the tool

wizard_general

4. Configure the “Parameter” tab#

  • Name and label for each parameter

  • Data Type: Feature Class, Field, String, Number (short, long, double), Boolean, and other

  • Type: Required or Optional

  • Direction: Input (in most situation) or Output

wizard_parameter

Checkout the Official Documententation.

5. Access parameters values#

One advantage of using a script tool is that it can work with user-defined inputs. Here’s how it works.

  • arcpy.GetParameterAsText()

  • access by position of the parameter, e.g., arcpy.GetParameterAsText(0)

import arcpy

# Define hard-coded parameter
I75_fc = "I75"
blkgrp_fc = "blockgroups"
cntbnd_fc = "county_boundary"
lawenforce_fc = "law_enforcement"
# Access soft-coded parameter
blkgrp = arcpy.GetParameterAsText(0)
I75 = arcpy.GetParameterAsText(1)
cntbnd = arcpy.GetParameterAsText(2)
lawenforce = arcpy.GetParameterAsText(3)
output_gdb = arcpy.GetParameterAsText(4)
blkgrp_select = arcpy.GetParameterAsText(5)
# intermediate output
I75_buff = output_gdb + "\\" + "I75_Buff"
blkgrp_law = output_gdb + "\\" + "blkgrp_law"