3

I've written a simple python script that allows me to quickly create a copy of a template Mxd, pointing to different shapefiles. The user specifies the new location, a name for the new mxd, then the folder that the shapefiles are stored in and the name of a point, polyline, and polygon shapefile. This script works fine.

newFilePath = r"X:\folder\outputfolder"
mxdName = "New_MXD"
shpFolder = r"X:\folderpath\templates"
shape_points = "input_P"
shape_polylines = "input_L"
shape_polygons = "input_G"




template_points = r"X:\folderpath\template_P.shp"
template_polylines = r"X:\folderpath\template_L.shp"
template_polygons = r"X:\folderpath\template_G.shp"

import arcpy 
MyMXD = r"X:\folder\template.mxd"
mxd = arcpy.mapping.MapDocument(MyMXD)  
outMxd = newFilePath +"\\"+ mxdName+".mxd"
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.supports("DATASOURCE"):
    if lyr.dataSource == template_points:
        print lyr.dataSource
        lyr.replaceDataSource(shpFolder, 'SHAPEFILE_WORKSPACE', shape_points)
        print "data replaced"
    if lyr.dataSource == template_polylines:
        lyr.replaceDataSource(shpFolder, 'SHAPEFILE_WORKSPACE', shape_polylines)                 
    if lyr.dataSource == template_polygons:
        print lyr.dataSource
        lyr.replaceDataSource(shpFolder, 'SHAPEFILE_WORKSPACE', shape_polygons)

mxd.saveACopy(outMxd)

I would like to turn this script into a tool that my colleagues can run without having to open the script. I've seen Esri guides for adding scripts to a toolbox, and for creating python toolboxes, but its not clear which I need to do. Any suggestings would be great.

I've tried the first option, and replaces the first block with:

NewFilePath = arcpy.GetParameterAsText(0)
mxdName = arcpy.GetParameterAsText(1)
shpFolder = arcpy.GetParameterAsText(2)
shape_points = arcpy.GetParameterAsText(3)
shape_polylines = arcpy.GetParameterAsText(4)
shape_polygons = arcpy.GetParameterAsText(5)

When I tick "Run Python script in process" the script runs but nothing happens. When I untick this and tick "show command window when excecuting script" the script fails and I get ERROR 000714: Error in script ViewSismageExport.

Is setting up parameters more complicated than this? I first tried setting the folders to be folders and the shapefiles to be shapefiles, but I've not got everything as string, with it still not working.

Does anyone know what I'm doing wrong?

  • 1
    Script tools and Python toolboxes serve essentially the same purpose, so unless one type is specified for you, the choice is yours on which one you want to use. However, Python toolboxes are only supported in ArcGIS 10.1 and up. Script tools have been around since well before that. – blah238 Mar 28 '14 at 21:46
5

Setting the parameters as you have done is the correct thing to do. The order of parameters in your code (0 to 5) must be the same as the order of the parameters when you are wiring in your script as a script tool.

I note that your newFilePath now starts with a capital N, python is case sensitive.

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.