rdbutger

Getting the current map with a script tool

Discussion created by rdbutger on Mar 15, 2011
Latest reply on Mar 15, 2011 by JScheirer-esristaff
With arcpy, this is the common method to get the current map --

mxd = arcpy.mapping.MapDocument("CURRENT")

With script tool validation, we are led to believe that we can run Python. So -- with a simple tool, I want to get the current map, and populate output paths to export that map to a JPG. So within the ToolValidator, I try this --

class ToolValidator:
  """Class for validating a tool's parameter values and controlling
  the behavior of the tool's dialog."""

  def __init__(self):
    """Setup arcpy and the list of tool parameters."""
    import arcpy
    self.params = arcpy.GetParameterInfo()

  def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
    called when the tool is opened."""
    mxd = arcpy.mapping.MapDocument("CURRENT")
    self.params[0].value = mxd[:-4] + ".jpg" 
    return

  def updateParameters(self):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parmater
    has been changed."""
    return

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    return

However, that returns the error --

initializeParameters Execution Error: Runtime error <type 'exceptions. TypeError'>: 'MapDocument' object is unsubscriptable

Sure, I can easily rewrite the script to allow the user to select the map to export, hard code the output, and/or just allow the user to set the output for the current map -- but what I really want is for the tool to open up with a suggested name for the export, but also allow the user to send it to another file if they desire.

ESRI Tech Support tells me this can't be done. Would anyone have any other ideas on how to get the current map within the ToolValidator?

Outcomes