You are here:
Geoprocessing
>
Automating your work with scripts
>
Creating script tools
Note:
This topic was updated for 9.3.1.
Script tool parameters are created and modified
- When adding a script tool to a toolbox or
- Right-click the script tool, click Properties, then click the Parameters tab.
To access script tool properties, right-click the tool, click Properties, then click the Parameters tab.
To add a new parameter, click the first empty cell in the Display Name column and enter the name of the parameter. This is the name that will be displayed on the tool dialog and can contain spaces. For command line, the parameter name will be the display name with spaces replaced by underscores (_).
After entering the display name of the parameter, choose a
data type for the parameter by clicking in the Data Type cell, as shown below.
Each parameter has additional properties you can set, as shown below.
|
Type
|
Can be Required, Optional, or Derived. Derived means that the user does not enter a value for the parameter. Derived types are always output parameters. |
|
Direction
|
Can be Input or Output. If the parameter Type is Derived, direction is always equal to Output. |
|
Multivalue
|
Multivalue is Yes if you want a list of values, No if you want a single value. |
|
Default or Schema
|
The default value for the parameter. When the parameter data type is either feature set or record set, Default is replaced with Schema. |
|
Environment
|
If the default value for the parameter is to come from an environment setting, this property contains the name of the environment setting. |
|
Filter
|
If you want only certain datasets or values to be entered for a parameter, you can specify a filter. There are six types of filters, and the type of filter you can choose depends on the data type of the parameter. |
|
Obtained from
|
This property applies to derived output parameters and input parameter data types. For derived output parameters, Obtained from can be set to the parameter containing the definition of the output. For input parameters, Obtained from is set to the parameter containing the information needed for input. |
|
Symbology
|
This property applies only to output parameters. The value is the location of a layer file (.lyr) that contains the symbology for displaying the output. |
Type
There are three choices for Type:
- A Required parameter requires an input value from the user. The script will not be executed until the user supplies a value.
- An Optional parameter does not require a value from the user.
- A Derived parameter is only for output parameters (see Direction, below). A derived output parameter does not show on the tool dialog.
The remainder of this section discusses the use of Derived output parameters.
There are four reasons for a derived output parameter, as follows:
- The tool modifies an input such as Add Field. Add Field adds a field to the input table—it doesn't create a new output table.
- The tool creates output using information in other parameters, such as Create Feature Class. With the Create Feature Class, you specify the workspace and the name of the new feature class, and the feature class is created for you.
- The tool outputs a scalar value as opposed to a dataset. Get Count, for example, outputs a long (the number of records). Whenever a tool outputs a scalar value, the output is Derived.
- The underlying script will create data. For example, you may have a script that updates an existing table in a known workspace. The user doesn't need to provide this table in the dialog or command line.
All tools should have outputs
All script tools should have output parameters so that they can be used in ModelBuilder. The fundamental idea of ModelBuilder is to connect the output of tools to inputs of other tools, and if your script tool doesn't have an output parameter, it isn't very useful in ModelBuilder.
Derived output that modifies an input parameter
The illustration below shows a hypothetical script tool, Update Field Values, used in ModelBuilder. (For purposes of discussion, Update Field Values is used by an organization to examine the contents of a set of known text fields and correct spelling and capitalization errors.) Update Field Values doesn't produce a new feature class, but it does update field values on the input feature class.
The correct parameter definition of Update Field Values is shown in the illustration below, where Update Field Values has an output feature class parameter with Type set to Derived. Since the output is the same as the input for this tool, Obtained from is set to the input parameter. (Obtained from uses the name of the parameter, which is the display name with spaces replaced by underscores.)
Derived output that does not modify an input parameter
The illustration below shows a different tool where the output is derived but it is not obtained from any input parameter (Obtained from is left blank). In this scenario, the hypothetical Post Data to Repository tool copies the input feature class to a known workspace (the repository), then adds and populates a date/time field.
Setting the output value
In the illustration above, note that the Output features variable is empty (white instead of green). This is because the name and location of the output features is not known until the tool is executed. In this case, your script must specify the output value by using the geoprocessing object method SetParameterAsText(). The SetParameterAsText() method will set the value of an output parameter using either an object, such as a value table, or a text string.
Learn more about the CopyParameter method
Learn more about the GetParameterAsText method
Learn more about the SetParameterAsText method
It is possible to provide a value for the output
before the tool is executed by providing tool validation code.
Learn more about tool validation
Here is example code that uses SetParameterAsText, based on the work performed by the Post Data to Repository script, described above.
# Post data to Repository - copies the input features to the
# current repository, adding and populating a date
# field
#
# Import system modules and create the geoprocessing object
#
import sys, string, os, arcgisscripting
gp = arcgisscripting.create(9.3)
# Get the value of the input parameter
#
inFC = gp.GetParameterAsText(0)
# Create the pathname to the output feature class.
# 1) get the name of the feature class
# 2) remove any file extension, such as ".shp"
# (we're copying to a geodatabase that doesn't
# allow file extensions, like .shp)
#
fcName = os.path.basename(inFC)
fcName = os.path.splitext(fcName)[0]
repository = r"E:\repository\featuredata.gdb"
outFC = os.path.join(repository, fcName)
# Copy the input to the output, add the date field
#
gp.CopyFeatures_management(inFC, outFC)
gp.AddField_management(outFC, "PostDate", "DATE")
# Create a locale-specific string containing the current date
# and time, then calculate it into the PostDate field, adding
# the required quotes around the postTime string.
#
import time
postTime = time.strftime("%x %X")
gp.AddMessage(postTime)
qPostTime = "\"" + postTime + "\""
gp.CalculateField_management(outFC, "PostDate", qPostTime)
# Set output parameter
#
gp.SetParameterAsText(1, outFC)
Output values instead of data
The examples above show outputting derived datasets. Some tools, however, output values instead of datasets, such as the
Get Count tool, which outputs a Long data type containing the number of rows in a table. Outputting values instead of datasets is not uncommon. You may have scripts of your own that perform analysis on several related datasets and output nothing more than a couple of numbers, or a pass/fail Boolean value.
Output parameters containing value data types (such as Long or Boolean) are always Derived rather than Required.
For a good example of outputting values, follow the links below. They lead to a topic that discusses outputting two Boolean values in a script tool for use in ModelBuilder.
View example of outputting two Boolean values
View the code that outputs two Boolean values
Direction
This property defines whether the parameter is an input to the tool or an output of the tool.
If the parameter type is Derived, then the parameter direction will be automatically set to output.
Multivalue
If you want a parameter to be able to handle a list of values rather than just one value, set the MultiValue property to Yes.
In tool dialogs, there are two different user interface controls that are used for mulitvalues, as illustrated below.
- A list of checkboxes is used for fields and for strings, longs, and doubles if they contain a ValueList filter.
- All other data types display the multivalue parameter control.
Both types of multivalue controls are illustrated below.
Mulitvalues are passed to your script as a semicolon-delimited string. Drawing from the above illustration, if the user selected all road types, the parameter value would be Interstates;Primary roads;Secondary roads. To break apart a delimited string, use the Python split() method, as shown in the code example below.
roadTypes = gp.GetParameterAsText(0)
roadTypeList = roadTypes.split(";")
# Process each road type
#
for rType in roadTypeList:
# rType contains an individual road type string (ex: "Interstates")
#
gp.AddMessage("Processing: " + rType)
Default
The default value will be the contents of the parameter when the script's tool dialog box is opened. It is also the value that will be used if a # is entered for the parameter in the command line. If you don't specify a value for the Default property, the parameter value will be blank when the script's dialog box is opened. If you specify a value for this property, the Environment property will become disabled. To enable the Environment property, remove the Default property.
Schema
When the input parameter data type is a Feature Set or Record Set, you can specify the location of a schema that defines the fields and geometry type of the features to be entered. A schema is either a feature class, table, or layer file (.lyr).
About Feature and Record Sets
The Feature and Record Set data types allow interactive input of data. A Feature Set allows the user of your script to interactively create features in ArcMap by clicking on the map. The Record Set allows your user to interactively create rows in a simple table grid.
For more information on Feature and Record Sets, follow the links below.
Environment
You can set the default value for a parameter to the value of an environment setting by right-clicking the cell next to Environment and choosing the name of the environment setting. Once you choose an environment setting, the Default property will be ignored. To use the Default property instead of the Environment property, clear out the Environment property by selecting the blank entry in the drop-down list.
Filter
If you want only certain dataset types or values to be entered for a parameter, you can specify a filter. Click the cell next to Filter and choose the appropriate filter from the drop-down list. A dialog box opens and you specify the values for the filter. There are six types of filters, and the type of filter you can choose depends on the data type of the parameter.
| Value List |
A list of string or numeric values. Used with String, Long, Double, and Boolean parameter data types. |
| Range |
A minimum and maximum value. Used with Long and Double data types. |
| Feature Class |
A list of allowable feature class types: Point, Multipoint, Polyline, Polygon, MultiPatch, Sphere, Annotation, Dimension. More than one value can be supplied to the filter. |
| File |
A list of file suffixes. Example: "txt; e00; ditamap". |
| Field |
A list of allowable field types: Short, Long, Single, Double, Text, Date, OID, Geometry, Blob, Raster, GUID, GlobalID, XML. More than one value can be supplied to the filter. |
| Workspace |
A list of allowable workspace types: File System, Local Database, or Remote Database. More than one value can be supplied. |
Usually, there is only one filter type you can choose. Only Long and Doubles have two choices, Value List and Range.
You can also set filters programmatically by providing validation logic.
Learn more about validation logic
Value List
The Value List filter is very useful for providing a set of keywords. Many tools have a predefined set of keywords, such as the field type parameter found in
Add Field, or the JoinAttributes parameter of many of the tools in the
Overlay toolset.
A Value List filter can be used for Long and Double data types. For these types, you enter the allowable numeric values.
If you want the user to be able to choose more than one of the values, set the
Multivalue property to Yes.
A Value List can be used for Boolean data types. For Boolean data types, the Value List contains two values, the true value and false value. The true value is always the first value in the list. These values are used in command line for specifying the value. See, for example,
Add Field and the {NULLABLE | NON_NULLABLE} keywords used for the IsNullable property.
Range
A Long or Double parameter can have a Range filter. Range filters have two values, the minimum and maximum. The first value in the list is the minimum. The range is inclusive, meaning the minimum and maximum are valid choices.
Feature Class
For this filter, choose one or more filter values. Input feature classes will be checked against the filter values. So, for example, if you select only Points as the filter value, the user can only enter point feature classes as the parameter value.
File
The file filter contains a list of file suffixes that a file may have, such as txt (simple text file) and csv (comma separated value). You can supply any text for a suffix—it doesn't have to a suffix that ArcGIS recognizes. The suffix can be of any length, and does not include the dot.
Field
The field filter defines the permissible
field types: Short, Long, Single, Double, Text, Date, OID, Geometry, Blob, Raster, GUID, GlobalID, XML. More than one value can be supplied to the filter.
Workspace
The workspace filter specifies the types of input workspaces that are permissible. There are three values:
- File System
A system folder, used to store shapefiles, ArcInfo coverages, INFO tables, and grids
- Local Database
A personal or file geodatabase
- Remote Database
An ArcSDE database connection
Obtained from
The Obtained from property has two purposes:
- For a derived output parameter, Obtained from is set to the input parameter that will be modified by the tool. For more information on derived data and Obtained from, see the discussion of the Type property, above.
- For input parameters, Obtained from contains the name of other parameters used by the data type. For example, for an input field data type, Obtained from is set to the name of the table parameter containing the fields, as illustrated below.
You can only set Obtained from for certain input parameters, as shown in the table below.
| Field or SQL Expression |
Table |
The table containing the fields |
| INFO Item or INFO Expression |
INFO Table |
The INFO table containing the items |
| Coverage Feature Class |
Coverage |
The coverage containing features |
| Area Units or Linear Units |
GeoDataset |
A geographic dataset used to determine the default units |
| Coordinate System |
Workspace |
A workspace used to determine the default coordinate system |
| Network Analyst Hierarchy Settings |
Network Dataset |
The network dataset containing hierarchy information |
| Geostatistical Value Table |
Geostatistical Layer |
The analysis layer containing tables |
Symbology
If the output of your tool is a feature set, raster, TIN, or Network Analyst Layer, you can specify the location of a layer file (.lyr) in the Symbology property. When your tool is run from ArcMap, ArcGlobe, or ArcScene, and Add results of geoprocessing operations to the display is enabled, the output is added to the display and drawn using the symbology defined in the Symbology layer file.
NOTE: The layer file is read each time the tool is run. If the layer file cannot be found (it was moved or deleted), default symbology will be used.
Follow the link below to learn more setting symbology. This link leads you to a ModelBuilder topic. Although the context of the topic is ModelBuilder, the same concepts apply to script parameter symbology.
Learn more about output symbology