Select to view content in your preferred language

Getting the current map with a script tool

762
4
03-15-2011 08:24 AM
RichardButgereit
Regular Contributor
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?
Tags (2)
0 Kudos
4 Replies
JasonScheirer
Esri Alum
import os
self.params[0].value = os.path.splitext(mxd.filePath)[0] + ".jpg" 


The map document object is not a string.
0 Kudos
RichardButgereit
Regular Contributor
That did it. Thanks.
0 Kudos
RichardButgereit
Regular Contributor
Since you solved my last one so well, how about this -- I also want to add a check box to add a time and date stamp to the filename, or to choose not to.

So this is working for the most part --

  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."""
    import os, datetime
    if self.params[2].value = True:
      now = datetime.datetime.now()
      time_date_stamp = now.strftime("%Y%m%d_%H%M_")
      mxd = arcpy.mapping.MapDocument("CURRENT")
      basedir = os.path.dirname(mxd.filePath)
      basename = os.path.basename(mxd.filePath)
      self.params[0].value = basedir + "\\" + time_date_stamp + basename[:-4] + ".jpg"
      self.params[1].value = basedir + "\\" +  time_date_stamp + basename[:-4] + ".pdf"
    else:
      mxd = arcpy.mapping.MapDocument("CURRENT")
      basedir = os.path.dirname(mxd.filePath)
      basename = os.path.basename(mxd.filePath)
      self.params[0].value = basedir + "\\" + basename[:-4] + ".jpg"
      self.params[1].value = basedir + "\\" + basename[:-4] + ".pdf"     
    return

Except for the line --

    if self.params[2].value = True:

How do I query the value of the checkbox?
0 Kudos
JasonScheirer
Esri Alum
Python uses == for equality, not =.
0 Kudos