I am trying to find a way to execute a few lines of (preferably python-)code whenever I save a specific mxd (my template.mxd)

I'm working with arcgis desktop 10.3.1 (advanced, all extentions)

What I am trying to achieve is the following: I wrote a python script tool which enables the user to automatically export a lot of maps in pdf format. In order to do that, the user can choose certain basemaps from a value list. This list is generated in the validations of the script tool when the tool starts:

def updateParameters(self): 
  mxd = arcpy.mapping.MapDocument(mxd_path) 
  df = arcpy.mapping.ListDataFrames(mxd, "Detailframe",)[0]
  basemapList = arcpy.mapping.ListLayers(mxd,"basemap*",df)

So now the user can choose each basemap which is integrated in the template mxd. But this slows up the tool startup (~20-30 seconds). Which bothers me.

Info: In the validations (def updateParameters) I made sure, that this list-generation runs only once instead of "whenever a parameter has been changed".

my idea is something like this:

The user alters the template-mxd by i.e. adding some new basemap layers and saves the mxd. At this point a second tool is triggered and lists all the layers with the suffix "basemap" and finally writes them to a simple textfile. This text-file should be much quicker to access in the python script tool. So if the user opens the python script tool it reads out the previously generated textfile and gets all the latest layersnames from there. (the layernames is all I need...so a string or a list is fine)

I managed to do this in the label expressions in python code. But then the code of the second script runs every time when the user pans the map which massively slows up the template-mxd.

def FindLabel ( [text] ):

  mxd = arcpy.mapping.MapDocument(mxd_path) 
  df = arcpy.mapping.ListDataFrames(mxd, "Detailframe",)[0]
  basemapList = arcpy.mapping.ListLayers(mxd,"basemap*",df)

  log = "c:/log/basemaptextfile.txt"
  txt = open(log, "w")
  for x in basemapList:
    txt.write('%s \n' % (x))


I read about arcgis addins which have similair functions which are called i.e. when a mxd is opened --> def openDocument(self): How could I achieve that some code runs when the mxd is saved?

Of course this would ONCE slow down the template mxd...at the time, when the saving is done. But this would be much better than having to wait 30 seconds each time my tool starts.

Any ideas how to approach? I am not familiar with arcobjects, but I would give it a try if it isn't too complicated.

UPDATE: the final construct shall have 4 components:

1) a template mxd which containes basemaps with the suffix "basemap". The user shall be able to alter this document by adding ne basemap layers or delete existing

2) python script tool which functions as a gui and reads out all the layers which have the suffix "basemap" and puts them into a value list, so the user can select which basemaps shall be exported to pdf. At this time this is achieved by listing the layers in the template mxd, but this takes too long. In the future this should be done by reading the layers from a txt.

3) a second code (maybe arcgis addin) which lists all the layers and writes them as a list or string into a simple txt

4) the python standalone .py which takes all these basemaps and creates pdf's from it(this works pretty well so far)

so what needs to be done is to find a solution for detecting if the user adds, removes or renames basemap layers in the template mxd and writing them to a txt. I thought this could be done by detecting if the template mxd was saved, because the user is supposed to save the template mxd before he runs the python script tool. But now I'm thinking this can maybe done with some functions from the arcgis addins. please feel free to suggest different approaches...

  • 2
    AFAIK this is not possible without ArcObjects and even then it's rather hairy, because there is no event that fires when the map document is saved: help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/… -- I will say your solution sounds quite convoluted. Perhaps if you you further explained what your users actually need someone can offer a better idea. – blah238 Feb 27 '16 at 0:41
  • 3
    Also here are the members of a Python add-in Extension class, perhaps one of these event handlers might be suitable for you: desktop.arcgis.com/en/arcmap/10.3/guide-books/python-addins/… – blah238 Feb 27 '16 at 0:47
  • oh wow, some of the members sound very promising for what I'm trying to achieve...especially closeDocument, itemAdded and itemDeleted. but you are right, I will update my description to better explain what the tool is supposed to do – fry82 Feb 27 '16 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.