Compress Project Script for Arc10

569
5
10-20-2010 03:00 AM
PaulWittle
New Contributor
Hello,

Can anybody correct the following Python script so that it will run? The basic idea is simple, it saves a copy, deletes the original, renames the copy to the original name and then opens the new (smaller) project file.

try:

    #Imports
    import arcpy, os

    #Definitions
    mxd = arcpy.mapping.MapDocument("CURRENT")
    #file_path = os.path.
    file_path = os.path.dirname(mxd)
    file_temp = os.path.basename(Input).rstrip(os.path.splitext(Input)[1])
    file_name = file_path + file_temp  ".mxd"
    file_name_temp = file_path + file_temp + "_Copy.mxd"

    #Save a copy
    mxd.saveACopy(file_name_temp)
    close(mxd)
    os.rename(file_name_temp, file_name)
    open(file_name)

#Output Errors Report
except Exception as e:
    print e.message
    # If using this code within a script tool, AddError can be used to return messages
    #   back to a script tool.  If not, AddError will have no effect.
    arcpy.AddError(e.message)

However, it currently doesn't work and python doesn't seem to allow step by step debugging.

Thanks
0 Kudos
5 Replies
JeffBarrette
Esri Regular Contributor
Hello Paul,

Have you tried testing your lines of code in the Python Window of ArcMap?  Here you would see immediate feedback with the code you are testing.  For example.

filePath = os.path.dirname(mxd) throws a "Map Document object is not subscriptable".

Perhaps an alternative would be to use filePath = os.path.dirname(mxd.filePath)

I'm also a little concerned with trying attempting to close the application that is currently open.  After doing a little pthon searching, it appears you would need to call other modules to actually close an application (e.g., the win32com module).  This would be much easier if you were not using "current".

os.startfile(mxd.path) is a way to open the resulting mxd.

I hope this helps a little,
Jeff
0 Kudos
PaulWittle
New Contributor
Thanks for that,

I've now got the whole script working except for the close file line... As you say, this is not an easy fix it seems. I've put my new script below, any suggestions?
--------------

#Imports
import arcpy, os, win32api

#Definitions
mxd = arcpy.mapping.MapDocument("CURRENT")
#file_path = os.path.
filePath = os.path.dirname(mxd.filePath) + "\\"
#getting the file name
fullfile = mxd.filepath
file_temp = os.path.basename(fullfile)
file_temp = file_temp.rstrip(os.path.splitext(file_temp)[1])

#Put together the full file references   
file_name = filePath + file_temp + ".mxd"
file_name_temp = filePath + file_temp + "_Copy.mxd"

#Save a copy
mxd.saveACopy(file_name_temp)
#Closing the file doesn't work!
#Need to close the file to rename the copy and then open it again
os.rename(file_name_temp, file_name)
os.startfile(file_name)

--------------------
Many thanks,
Paul
0 Kudos
AaronPaul
New Contributor II
All- I'd like to know what this script does or what its application is meant to do.

I need to decrease the size of my mxd's and am wondering if this script does that.

Does this script reduce file sizes of .mxd's??

Thank you,

Aaron
0 Kudos
PaulWittle
New Contributor
Yes, the purpose of the script was to reduce file sizes when you close a project but I have since discovered that the "ArcGIS Document Defragmenter" does the same job.

It would still be useful to know if you can call the "ArcGIS Document Defragmenter" from a script then I could set it to run when ArcMap closes.

Paul.
0 Kudos
AaronPaul
New Contributor II
I was unaware of the Document Defragger or the MXD Doctor. Thank you for providing this information. I have many MXD's that are dying for this solution. Good times
0 Kudos