How to pass mxd as argument

881
3
Jump to solution
12-22-2012 12:49 PM
FrankRoberts
Occasional Contributor III
I have a simple script that is doing some parsing of the mxd that I pass it.  The script works just fine if I hard code the name of the mxd into the script, however I would like to pass the full path of the mxd to the script when I run it. This seems to be an issue since the examples I have seen all have the ???r??? in them prior to the path and mxd name.  For example(this works by the way):

mxd = arcpy.mapping.MapDocument(r"C:\Data\GISData\PythonCode\test.mxd")



I???ve tried a number of things but haven???t figured out how to get around this simple problem.  For Example:

mxdIn = sys.argv[0] mxd = arcpy.mapping.MapDocument(mxdIn)

Also tried:


mxdIn = sys.argv[0] mxd = arcpy.mapping.MapDocument(r (mxdIn))



Thanks for your help in advance!
Tags (2)
1 Solution

Accepted Solutions
T__WayneWhitley
Frequent Contributor
Try mxd = arcpy.GetParameterAsText(1) passing in your mxd document name.

EDIT:  Actually, it is arcpy.GetParameterAsText(0) to fetch the 1st parameter set at your script tool's interface.  What threw me is that you were using sys.argv[0], which I think should be sys.argv[1].  It has been a while since I have fetched parameters the 'system argument' route, which is still valid, however, if not mistaken, sys.argv[0] fetches the script name.

So, this should work, doing it your way (provided of course that the argument is a valid mxd pathname):
mxdIn = sys.argv[1] mxd = arcpy.mapping.MapDocument(mxdIn)

View solution in original post

3 Replies
T__WayneWhitley
Frequent Contributor
Try mxd = arcpy.GetParameterAsText(1) passing in your mxd document name.

EDIT:  Actually, it is arcpy.GetParameterAsText(0) to fetch the 1st parameter set at your script tool's interface.  What threw me is that you were using sys.argv[0], which I think should be sys.argv[1].  It has been a while since I have fetched parameters the 'system argument' route, which is still valid, however, if not mistaken, sys.argv[0] fetches the script name.

So, this should work, doing it your way (provided of course that the argument is a valid mxd pathname):
mxdIn = sys.argv[1] mxd = arcpy.mapping.MapDocument(mxdIn)
T__WayneWhitley
Frequent Contributor
I tend to forget and have to check which param by index I am actually fetching and of course it depends on how many variables you've set at the tool interface and in what order.

EDIT:  If you need a reference to the mxd object, then you have to pass the string into this statement (as it appears you were trying to do):
mxd = arcpy.mapping.MapDocument(r'your mxd pathname string')

In this case the mxd variable is an object reference whereas the initial GetParameterAsText mentioned was assigned to mxd as a string pathname (not an object).  So this sequence of commands may be a little more straightforward:

# Get the string pathname (mxdStr), 1st param as a script tool
mxdStr = arcpy.GetParameterAsText(0)
# Pass the string to the MapDocument method to create the map doc object (mxdObj)
mxdObj = arcpy.mapping.MapDocument(mxdStr)
FrankRoberts
Occasional Contributor III
Thanks Wayne that was it!  I knew it had to be something simple:

So this was my solution:

mxdIn = arcpy.GetParameterAsText(0)
mxd = arcpy.mapping.MapDocument(mxdIn)


Have a great Christmas!