A script tool is a geoprocessing tool that executes a script or executable file. Script tools run these scripts using a geoprocessing tool as the interface for your script. When you create a script tool, you specify parameters that your script needs to execute. The most common parameters are input and output datasets, field names, and strings chosen from a choice list. When the script tool runs, the parameter values are passed to your script and used by your script during execution.
Many 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. Through ArcPy, your script can access the geoprocessing environment of ArcGIS, providing benefits such as on-the-fly projection, selection set support, validation of inputs, error messaging, and recording of history.
Script tools are created in a toolbox. To create a script tool, right-click a toolbox in the Catalog pane and select New > Script.
Properties
When you create a script tool, that new script tool's properties dialog box opens with several pages, each with related properties, including the following:
- General
- Parameters
- Execution
- Validation
General
The General page includes properties such as the tool name, label, description, and if the path of the execution file of the script tool should be stored as a path relative to the toolbox file.
Parameters
The Parameters page presents a table of parameter properties, where each row is one parameter. The table will be empty by default. For each parameter, you define a number of properties, such as the label that appears for that parameter in the UI, what type of data the parameter accepts, and whether the parameter is optional or required. You must, at a minimum, set the parameter Label, Name, Data Type, Type, and Direction properties.
Property | Description |
---|---|
Label | The name of the parameter displayed on the tool. |
Name | The name of the parameter when the script tool is called as a function from a different Python script. |
Data Type | 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. |
Type | 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. |
Direction | Parameters can be input or output. Input parameters represent existing data to be processed or values to use in processing. Output parameters represent data the tool creates or values that are calculated. |
Category | Entering the name of a category puts the parameter in a drop-down group on the tool's dialog box. |
Filter | Filters allow you to limit the specific types of a dataset, file, or field; specific values; or values in a range that 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 and 10 are acceptable. |
Dependency | 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. |
Default | A value automatically set for the parameter when the tool's dialog box appears. |
Environment | 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 appears. |
Symbology | 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. |
Execution
The Execution page includes properties related to the execution code of the script tool, or the path to a Python or R script file. The page also includes commands to embed and password-protect the script tool's execution code, or export the execution code to a new file.
You can choose to embed the execution code within the toolbox, or link to an external script file. Regardless of the execution code location, the page includes a code editor that you can use to modify the execution code, or click the Open in Script Editor button to open the execution code in a script editor. You can specify your preferred script editor in Geoprocessing options.
Validation
The Validation page displays the validation code of the script tool. Validation allows you to customize how tool parameters respond and interact to values and other parameters on the script tool. Validation code is written in Python.
The ToolValidator Python code block gives you control of the appearance, behaviors, and messages of your script tool. You can edit the Validation code directly from a code editor, or click the Open in Script Editor button to open the validation code in your preferred script editor that is set in Geoprocessing options.