Script tools are geoprocessing tools that execute a script or executable file. Script tools allow you to run these scripts from within ArcGIS, using a geoprocessing tool as the interface to the functionality in your script. When you create a script tool, you specify parameters that your script needs in order to execute. The most common parameters are input and output datasets, field names, and strings chosen from a choice list. When the script tool is run, the parameter values are passed to your script and used by your script during execution.
Almost all script tools execute a Python script (.py) that uses ArcPy, a Python site package installed with ArcGIS. ArcPy provides access to geoprocessing tools as well as additional functions, classes, and modules that allow you to create simple or complex workflows quickly and easily. Through ArcPy, your script can access the geoprocessing environment of ArcGIS, providing benefits like on-the-fly projection, selection set support, validation of inputs, error messaging, and recording of history.
While the vast majority of script tools are written in Python, you can create a script tool for anything that accepts arguments. For example, you can create a script tool for a .r, .bat, or .exe file that accepts arguments.
Script tools are created in a toolbox. To create a script tool, right-click a toolbox in the Project pane and select New > Script Tool.
When you create a new script tool, that new script tool's property page opens. In the script tool property page you can set a number of properties such as:
- General settings such as the tool name, password protection, and the location of the source script file.
- The tool Parameters that determine what input datasets, additional parameters, and output datasets the script tool will display.
- Validation code which allows you to customize how tool parameters respond and interact to values and other parameters on the script tool (for example, change the value of one parameter based on what option is selected for a different parameter). Validation code is written in Python.
Defining parameter properties is a key step in creating a script tool. On the Parameters page, each row represents one parameter. For each parameter, you define a number of properties such as the label that appears on the tool, what type of data the parameter accepts, and if the parameter is optional or required. You must at a minimum enter the parameter Label, Name, Data Type, Type, and Direction.
The name of the parameter displayed on the tool.
The name of the parameter when the script tool is called as a function from a different Python script.
The type of data the parameter accepts. This determines the control used on the tool. For example, if you select Feature Layer, you can pick a layer from an active map or browse to a feature class.
Parameters can be required or optional. If the parameter is required, it must have a value or the tool cannot run. If the parameter is optional, the tool can run without specifying a value.
Parameters can be input or output. Input parameters represent existing data to be processed or values to use in processing. Output parameters represent new data the tool creates or values that are calculated.
Entering the name of a category puts the parameter in a drop-down group on the tool's dialog box.
Filters allow you to determine if only specific types of a dataset, file, or field, specific values, or values in a range are acceptable input to a tool. For example, you can filter a feature class parameter so only point feature classes are acceptable; or, you can filter a number parameter so only values between 1-10 are acceptable.
You can make one parameter dependent on another parameter. For example, a field parameter is dependent on a dataset parameter; setting this dependency makes it so you can select a field from the dependent dataset.
A value automatically set for the parameter when the tool's dialog box opens.
A parameter can derive its default value from a geoprocessing environment. So if the specified geoprocessing environment is set, the value will be used as the default for the given parameter when the tool's dialog box opens.
The path to a layer file that defines the symbology of the parameter. For example, you can make an output dataset parameter have the same symbology of the layer file when the output is created and added to the map.
The ToolValidator Python code block gives you even more control of the appearance, behaviors, and messages of your script tool. You can edit the Validation code directly from the properties window, or click the Open in Script Editor button to open the validation code in your preferred script editor that is set in Geoprocessing options.