Sign up ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

The engineers I work with have requested an autosave feature for ArcGIS, so I thought I'd make them a python tool that routinely saves. The script works fine when run in the foreground, but fails when "Always run in foreground" in not checked.

enter image description here

The returned messages:

enter image description here

Is there a workaround for this issue?

The code:

"""Autosave"""

import arcpy
import time
import os

arcpy.env.overwriteOutput = True

autosaveFolder = arcpy.GetParameterAsText (0)
autosaveFile = arcpy.GetParameterAsText (1)
waitTime = arcpy.GetParameter (2)

if not autosaveFile.lower().endswith (".mxd"):
    autosaveFile = autosaveFile + ".mxd"

waitSeconds = int (waitTime * 60)

autosaveMxd = os.path.join (autosaveFolder, autosaveFile)

while True:
    time.sleep (waitSeconds)
    mxd = arcpy.mapping.MapDocument("CURRENT")
    mxd.saveACopy (autosaveMxd)
    del mxd
share|improve this question
    
Arc is still not thread safe. I have a timer save in C# but I cheated a bit.. the timer runs in background and pops up a box that prompts to save. You don't want to save blindly as the current sketch is discarded on save and the undo buffer is flushed... it really should be up to the user if it's a good time to save or not. Would you like to see the C# code? – Michael Miles-Stimson Aug 27 at 23:15
    
I'd be a bit surprised if the engineers knew how to undo :) "the current sketch is discarded on save" I'm not sure what this means. Can you explain? I also wouldn't know how to implement the C# code, nor is it quite what the engineers are after, so it probably wouldn't do me much good. Thanks though. – Emil Brundage Aug 27 at 23:39
    
If you are currently editing or adding a feature it's not there until you press F2 to finish the sketch... should you hit Save or Save Edits while there is a sketch active the sketch is discarded (which is kind of disheartening if you've spent the last half hour plotting a lake boundary and it vanishes). Try launching a message box with the text "Time to save" - as a reminder; this might be the best you'll get with python. – Michael Miles-Stimson Aug 27 at 23:43

3 Answers 3

up vote 3 down vote accepted

When you are running a script in the background, a python process is spawned that is separate from ArcMap. So essentially, arcpy.mapping.MapDocument("CURRENT") is attempting to open a map document that it cannot see. This makes sense as imagine the issues if you had multiple .mxd open: Python would not know which one you mean by "CURRENT".

You can see this yourself by firing up python (not from ArcMap) and executing the above.

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    arcpy.mapping.MapDocument("CURRENT")
  File "C:\Program Files\ArcGIS\Server\arcpy\arcpy\arcobjects\mixins.py", line 610, in __init__
    super(MapDocumentMethods, self).__init__(mxd)
  File "C:\Program Files\ArcGIS\Server\arcpy\arcpy\arcobjects\_base.py", line 47, in __init__
    for arg in args))
RuntimeError: Object: CreateObject cannot open map document
share|improve this answer

My understanding is that Background Geoprocessing runs as a separate process and, unlike Foreground Geoprocessing, is unaware of your current ArcMap environment.

Consequently, I don't think you'll have success with your current approach.

AutoSaving maps is not the same as AutoSaving edits but you could look at Incremental "auto save" in ArcMap edit session and add your voice to the ArcGIS Idea(s) for this functionality.

Rather than a Python Toolbox the place that I would look to try and implement this would be as a Python AddIn Extension, but I have not tried using an extension to do this so an event to trigger your code may or may not be available.

You may also want to look at ArcGIS Pro because that has Autosave on edits, but I am not sure about AutoSaving projects/maps/etc.

share|improve this answer
    
I use a delegate to pick up the event on the main thread, I'm not sure how you can do that in python - a little too advanced for me. ArcGis Pro sounds like it might be worth looking at; does it use the same licensing as ArcGis desktop? – Michael Miles-Stimson Aug 27 at 23:40
    
@MichaelMiles-Stimson It uses Named Users but but my understanding is that basic principle is every ArcGIS for Desktop user on maintenance has entitlement to use ArcGIS Pro and/or ArcMap. – PolyGeo Aug 28 at 0:11
    
Technically speaking, it's a separate process, not a separate thread. – KHibma Aug 28 at 0:23
    
I'll trust your terminology rather than mine @KHibma so I've edited my question. – PolyGeo Aug 28 at 0:26

You could try using XTools Pro, which has an autosave MXD function built in

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.