<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Want to run my Python script as a script tool in a custom toolbox in Geoprocessing Questions</title>
    <link>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685621#M22740</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: pierrek&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you Wayne for being so patient and helpful.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I ran your script and get the following error message. Please find attached a screen shot.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I noticed that you select the first layer in my TOC. I am working on a multiple scale map and my *.mxd document has layers with the same naming multiple times (up to 15). So there would be no need to select the most top layer as you listed in code line 47. ("lyr = arcpy.mapping.ListLayers(mxd, layer)[0] # single-layer datasource replacement, 1st one listed from TOC.") I would need all layers with the same naming.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Anyway you also mentioned another ArcMap version. I am running the latest 10.2.1. I am not sure whether that is of any help.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As I mentioned earlier I am not a coder and the script becomes now even heavier and I kind get even more lost.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Could you please have another look for me at the script you have provided?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks alot.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Pierre&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 01 May 2014 22:25:34 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2014-05-01T22:25:34Z</dc:date>
    <item>
      <title>Want to run my Python script as a script tool in a custom toolbox</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685608#M22727</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi there,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I wrote a Python script which allows me to select a layer out of my TOC and change the source to a new data source.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The script I wrote works fine and does the job.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now I'd like to create a script tool with a user interface working out of a custom tool box.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I cannot get my head around with rewriting the script with variables and use it in conjunction with parameters as a script tool.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When I run my script tool it does not spill out any errors nor the script runs as I want it to. (It saves a new *.mxd but without any changes to the file).:confused:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;There must be something wrong the way I set up the variables in my Python script and/or the way I setup the parameter in the script tool properties.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;- Please find attached my original Python script which runs perfectly fine.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;- Also a revised script for the script tool running within a custom tool box.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;- ... and a screenshot of the parameter setup. (I did not make any changes to the parameter properties)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any help would be terrific.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Pierre&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Apr 2014 01:30:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685608#M22727</guid>
      <dc:creator>PierreKurth</dc:creator>
      <dc:date>2014-04-30T01:30:05Z</dc:date>
    </item>
    <item>
      <title>Re: Want to run my Python script as a script tool in a custom toolbox</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685609#M22728</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: Wayne_Whitley&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I just took a brief look... it appears you're setting the 1st param to the workspace but you have to feed in the full pathname to your map doc.&amp;nbsp; Maybe that's why it doesn't save any changes.&amp;nbsp; That's where I'd start.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, if you want to set the workspace separately, you can do something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.env.workspace = r'C:\thisIsMyDesktop\andThisIsWhereMyMxdLives'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;mxd = arcpy.mapping.MapDocument("myMapDoc.mxd")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...then change the workspace just before saving to the directory you you want to save to (if different than the original workspace).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Apr 2014 01:38:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685609#M22728</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2014-04-30T01:38:17Z</dc:date>
    </item>
    <item>
      <title>Re: Want to run my Python script as a script tool in a custom toolbox</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685610#M22729</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you Wayne for coming back to me so quickly.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am even more confused now.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As I mentioned the script runs without any errors and it also saves a new *.mxd no problem but it just does not want to change any data sources for the selected layers in my TOC.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I must have done something wrong the way I setup variables in my revised Pyhton script or the way I setup the parameters for my script tool.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Pierre&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Apr 2014 01:52:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685610#M22729</guid>
      <dc:creator>PierreKurth</dc:creator>
      <dc:date>2014-04-30T01:52:02Z</dc:date>
    </item>
    <item>
      <title>Re: Want to run my Python script as a script tool in a custom toolbox</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685611#M22730</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: Wayne_Whitley&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I did see another likely error - the 2nd parameter for the layer name should not be a layer parameter but a string value.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;In other words, when you list layers, that command in your script lists by a name (which is a string).&amp;nbsp; Try fixing that.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;All I was saying earlier was that your 1st param should be a map doc pathname,...looks like you may have taken care of that, but I'd also change that to string too although it may run as ArcMap Document parameter.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;EDIT-&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;So, since you got to the end of the script with a new mxd written, ignore for now what I said about the workspace - your main concern at the moment is to get your tool parameter types defined.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Apr 2014 01:56:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685611#M22730</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2014-04-30T01:56:42Z</dc:date>
    </item>
    <item>
      <title>Re: Want to run my Python script as a script tool in a custom toolbox</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685612#M22731</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you for the tip.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I changed my second and fourth parameter to a String.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The script is now working.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;To make it more elegant I wanted to have those drop down menus where the user only have to select&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;the layer(2nd parameter) and the new dataset(4th parameter) rather than typing them in.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;That is why I choose layer (for second parameter) and dataset for my 4th parameter.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any thoughts how I can make that happen?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks heaps already.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Pierre&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Apr 2014 04:14:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685612#M22731</guid>
      <dc:creator>PierreKurth</dc:creator>
      <dc:date>2014-04-30T04:14:38Z</dc:date>
    </item>
    <item>
      <title>Re: Want to run my Python script as a script tool in a custom toolbox</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685613#M22732</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: Wayne_Whitley&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Okay, so you'd do that with the ToolValidator class...you'd access that on the script tool properties window from the Validation tab - there is Python code there that executes prior to actually running the script file itself.&amp;nbsp; This could be made much more elegant as you say, but basically all I did below was generate the lists for the 2nd and 4th parameters from the 1st and 2nd parameters (respectively).&amp;nbsp; I included a zip of the tool box containing this script tool code on that tab (click 'Edit...' to open it in a familiar editor in order to further modify it).&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
class ToolValidator(object):
&amp;nbsp; """Class for validating a tool's parameter values and controlling
&amp;nbsp; the behavior of the tool's dialog."""

&amp;nbsp; def __init__(self):
&amp;nbsp;&amp;nbsp;&amp;nbsp; """Setup arcpy and the list of tool parameters."""
&amp;nbsp;&amp;nbsp;&amp;nbsp; self.params = arcpy.GetParameterInfo()

&amp;nbsp; def initializeParameters(self):
&amp;nbsp;&amp;nbsp;&amp;nbsp; """Refine the properties of a tool's parameters.&amp;nbsp; This method is
&amp;nbsp;&amp;nbsp;&amp;nbsp; called when the tool is opened."""
&amp;nbsp;&amp;nbsp;&amp;nbsp; return

&amp;nbsp; def updateParameters(self):
&amp;nbsp;&amp;nbsp;&amp;nbsp; """Modify the values and properties of parameters before internal
&amp;nbsp;&amp;nbsp;&amp;nbsp; validation is performed.&amp;nbsp; This method is called whenever a parameter
&amp;nbsp;&amp;nbsp;&amp;nbsp; has been changed."""
&amp;nbsp;&amp;nbsp;&amp;nbsp; stringFilterTargetLyrs = self.params[1].filter
&amp;nbsp;&amp;nbsp;&amp;nbsp; stringFilterSourceLyrs = self.params[3].filter
&amp;nbsp;&amp;nbsp;&amp;nbsp; theMap = self.params[0].value
&amp;nbsp;&amp;nbsp;&amp;nbsp; theGDB = self.params[2].value
&amp;nbsp;&amp;nbsp;&amp;nbsp; if theMap:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument(str(theMap))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyrList = [str(lyr) for lyr in arcpy.mapping.ListLayers(mxd)]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; stringFilterTargetLyrs.list = lyrList
&amp;nbsp;&amp;nbsp;&amp;nbsp; if theGDB:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.workspace = str(theGDB)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fcList = arcpy.ListFeatureClasses()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; stringFilterSourceLyrs.list = fcList
&amp;nbsp;&amp;nbsp;&amp;nbsp; return

&amp;nbsp; def updateMessages(self):
&amp;nbsp;&amp;nbsp;&amp;nbsp; """Modify the messages created by internal validation for each tool
&amp;nbsp;&amp;nbsp;&amp;nbsp; parameter.&amp;nbsp; This method is called after internal validation."""
&amp;nbsp;&amp;nbsp;&amp;nbsp; return
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Your script file (included) is basically untouched so you should be able to unzip and copy the entire toolbox to run the tool from your directory (it is relative-pathed).&amp;nbsp; It should give you the general idea, even limited to only generating the 2 lists, although further refined code would additionally filter for geom data types, check schema, and include feature datasets, etc.&amp;nbsp; Also, you may want to add a param for the path to the saved mxd copy.&amp;nbsp; You can readily see that TargetLyrs refers to a listing of target layers to change the datasource for in the target mxd and SourceLyrs refers to a listing of the available SDE feature classes from the source gdb.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Further info here at the webhelp:&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.2/index.html#/Customizing_script_tool_behavior/00150000000t000000/" rel="nofollow noopener noreferrer" target="_blank"&gt;http://resources.arcgis.com/en/help/main/10.2/index.html#/Customizing_script_tool_behavior/00150000000t000000/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Anyway, the above code is a quick intro to at least the capability you requested, adding value lists based on your other params.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Enjoy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:50:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685613#M22732</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-12T04:50:22Z</dc:date>
    </item>
    <item>
      <title>Re: Want to run my Python script as a script tool in a custom toolbox</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685614#M22733</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you. Now it gets serious. As you probably noticed I am not a coder and I must admit this is my first time I have touched any Python scripting.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Anyway I really like to way the user can now select certain parameters via a dropdown list.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I did exactly what you did and made changes to the ToolValidator class.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The script runs fine and saves a new file but unfortunately it does not change any source. &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Also is does not even come back with an error message.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Maybe it does not like it when I have inputs as string parameter because I guess the parameter we set with the ToolValidator are now objects? I do not really know this is just me guessing.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Could you please have a look at it again for me?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I attached my current tool and the script this time with relative paths. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I also added a new field which allows the user to define the name of the output file.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Pierre&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 30 Apr 2014 23:42:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685614#M22733</guid>
      <dc:creator>PierreKurth</dc:creator>
      <dc:date>2014-04-30T23:42:14Z</dc:date>
    </item>
    <item>
      <title>Re: Want to run my Python script as a script tool in a custom toolbox</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685615#M22734</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: Wayne_Whitley&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The attached zip at the last post was already geared to go, if you will try that 1st - in other words, it has the ToolValidator code already embedded and is also sourced to your script (and the script tool param datatypes are also set.&amp;nbsp; Download, unzip to a directory you have a Catalog connection to (or make a new connection), open the script tool and try it out.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Tell me if the layer you target successfully changes datasource.&amp;nbsp; It could be the target layer you're trying to modify the datasource for is not compatible with the chosen source layer - that was what I was saying about further refining the tool to assure filtering for viable candidate source substitutions.&amp;nbsp; For simplicity (and convenience for me) I didn't bother with the further coding and in doing so, left it up to your judgement to make good choice --- unfortunately, we oftentimes have data layers we aren't altogether familiar with....so if you intend to use this type of tool often you'd likely want to beef up both the initial list filtering and the main script's error trapping.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Let me know as soon as that as-is copy runs, and what results you get, and then we'll discuss troubleshooting options further; it's a simple script and the point of failure is probably these lines below - in fact, you could probably insert a simple msg afterwards as shown to make sure the 'if' statement evaluated true and executed your 'replaceDataSource' line:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
if lyr.supports("DATASOURCE"):
&amp;nbsp; lyr.replaceDataSource(serverConnection, "SDE_WORKSPACE", newSource)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('replace executed...')
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...if you're curious about the param datatypes, check the properties of the attached...I think they're Map Document, String, Workspace, and String for the 4 original parameters (in that order).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;PS- This is obvious I guess, but make sure you're checking the datasource in the mxd copy you saved to, not the original you started with.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:50:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685615#M22734</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-12T04:50:26Z</dc:date>
    </item>
    <item>
      <title>Re: Want to run my Python script as a script tool in a custom toolbox</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685616#M22735</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I gave it another go and ran your original Toolbox and script. I ended up with the same results like before&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The script runs and it saves a new *.mxd but no changes were made to the target.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I also added the msg string as you suggested and it did not come up with a message. I guess that means the script struggles at this point?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;What else could I try?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;There must be something not right the way I select the layer and/or the new targeted via the drop down.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am stuck and do not know where else to look at.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Pierre&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 01 May 2014 01:41:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685616#M22735</guid>
      <dc:creator>PierreKurth</dc:creator>
      <dc:date>2014-05-01T01:41:04Z</dc:date>
    </item>
    <item>
      <title>Re: Want to run my Python script as a script tool in a custom toolbox</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685617#M22736</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: Wayne_Whitley&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Try this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Do you know how to enter commands in the Python window within ArcMap?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1- Open the mxd you are targeting (of make a copy for experimentation and open that).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2- Run the following lines below one by one in the Python window of the open mxd - I'm not testing these lines so let me know if I make a mistake and an error is returned in the window.&amp;nbsp; Where I have "enter this" in quotes, enter what I ask you.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import arcpy
mxd = arcpy.mapping.MapDocument('CURRENT')
targetLyrName = "enter name of layer in TOC"
targetLyr = arcpy.mapping.ListLayers(mxd, targetLyrName)[0]
if targetLyr is not None:&amp;nbsp; print "your selected layer is {0}.format(targetLyr.longName)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;3- Did a layer name for the selected layer print?&amp;nbsp; If so, continue on:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
serverConnection = r"enter an SDE connection file pathname"
newSource = "enter the full name of the feature class to use for replacement"
arcpy.env.workspace = serverConnection
if arcpy.Exists(newSource):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'good to go'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if targetLyr.supports("DATASOURCE"):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "DATASOURCE property is supported for this layer."
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; targetLyr.replaceDataSource(serverConnection, "SDE_WORKSPACE", newSource)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "DATASOURCE property is not supported for this layer."
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Now tell me what messages you get reported back to you and tell me if the datasource has changed in the current mxd.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:50:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685617#M22736</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-12T04:50:28Z</dc:date>
    </item>
    <item>
      <title>Re: Want to run my Python script as a script tool in a custom toolbox</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685618#M22737</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I tried the code you sent.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The first bit turned a "y". See screenshot attached. I had to change the line from (there was a " missing and the "0" was not in the right bracket style?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;if targetLyr is not None:&amp;nbsp; print "your selected layer is {0}.format(targetLyr.longName)&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;to&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;print "your selected layer is" [0].format(targetLyr.longName)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I still kept going the results you can also find in the attached screenshot.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope that helps.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 01 May 2014 03:26:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685618#M22737</guid>
      <dc:creator>PierreKurth</dc:creator>
      <dc:date>2014-05-01T03:26:10Z</dc:date>
    </item>
    <item>
      <title>Re: Want to run my Python script as a script tool in a custom toolbox</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685619#M22738</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: Wayne_Whitley&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...no, that's not right, the replaceDataSource line should be a single line.&amp;nbsp; You should have also corrected the indention - if/elif/else statements part of the same logical block should be identically indented.&amp;nbsp; And the [0] is wrong.&amp;nbsp; Try this again, and the only reason I'm directing you to do this is to get more informative messages back - I suspect something's happening like you aren't making a valid SDE connection or an improper datasource is being applied.&amp;nbsp; This time, I'm attaching a file containing the same lines for you to load into the ArcMap Python window, hopefully to avoid the indention/parsing error.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1- Right-click in the Python window of ArcMap, select 'load'...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2- Browse to the location of the saved py file (file attached) and select it...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;3- Fill in the 3 parameters (1st 3 lines), similar to what you did before...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;4- Make sure the indention appears correct, etc...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;5- Press 'Enter' to execute...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;6- Report the returned messages...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This may help isolate the source of error, depending on what msgs you get back.&amp;nbsp; If the datasource change works this way, then you'll need to work on how the tool params are passed to the script to get that working as well.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Good luck!&amp;nbsp; If I have time, I'll attach later a 'fully-functioning' zip including a map doc showing the change of a file gdb datasource.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I tested this on both file and SDE data sources, and by the way this version is 10.2.x.&amp;nbsp; ...a few simple modifications make it compatible for 10.0, in case that's necessary.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;PS- If for some reason you're still getting errors with indention and so forth, you could optionally make a temporary script tool and run it within the current map doc.&amp;nbsp; Make it with no parameters (fill in those params as directed in the script file itself and save the file).&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 01 May 2014 11:40:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685619#M22738</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2014-05-01T11:40:54Z</dc:date>
    </item>
    <item>
      <title>Re: Want to run my Python script as a script tool in a custom toolbox</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685620#M22739</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;oops, I see I failed to attach the script file I meant to at the last post. Regardless, I think this script was due for a slight re-write, so I did the below revised script file (and it is reflected in the attached zip which I will remember to attach, to include the same script tool interface w/ validation -- with the new map doc param added).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It occurred to me that maybe you're not providing the pathname to the new map document, so I made the script form the path to write the new file at the same root location as the orig mxd. Otherwise, with the behavior of my 10.2.1, it attempts to write to the current workspace (which may not be a regular 'file' directory). After I inadvertently wrote a map doc copy inside the Default.gdb, yes I said 'inside' my default workspace, I decided to override that behavior. There was no error, and ArcMap didn't seem to mind referencing a layer in the gdb from an mxd within the same gdb. Maybe that is by design, but I decided to 'fix' that with the following, and it also prints the file path you should check the mxd for your datasource update. If for some reason you don't provide a unique name, the save is simply not executed, and you get a warning message. This is made to handle only file or SDE gdb sources, just to keep this bare-bones simple, and there's no handling for feature datasets...so beware of that.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
mxdLocation = arcpy.GetParameterAsText(0)
layer = arcpy.GetParameterAsText(1)
serverConnection = arcpy.GetParameterAsText(2)
newSource = arcpy.GetParameterAsText(3)
newMXDname = arcpy.GetParameterAsText(4)

def gdbType(connPath):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; connType = os.path.splitext(connPath)[1]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if connType == '.sde':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('\nsde source.')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; wsType = 'SDE_WORKSPACE'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif connType == '.gdb':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('\nfile gdb source:')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; wsType = 'FILEGDB_WORKSPACE'
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('\nunhandled source type')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; wsType = ''
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return wsType

arcpy.env.workspace = serverConnection
if arcpy.Exists(newSource):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; wsType = gdbType(serverConnection)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('the workspace for the new source is {0}\n'.format(serverConnection))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
MXDdir = os.path.dirname(mxdLocation)
newMXDname = os.path.join(MXDdir, newMXDname + '.mxd')
mxd = arcpy.mapping.MapDocument(mxdLocation)

lyr = arcpy.mapping.ListLayers(mxd, layer)[0] # single-layer datasource replacement, 1st one listed from TOC.

if lyr:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.supports("DATASOURCE") and wsType != '':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('DATASOURCE property is supported for this layer.\n')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.replaceDataSource(serverConnection, wsType, newSource)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if not arcpy.Exists(newMXDname):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('Please check the {0} layer properties for the source change in your new map doc:'.format(lyr.name))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('{0}.\n'.format(newMXDname))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.saveACopy(newMXDname)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddWarning('mxd name is not unique and will not be saved...\n')
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('DATASOURCE property is either not supported or handling by this script is not provided.\n')


arcpy.AddMessage('The End.')
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Let me know if you have questions. Test it out. If you need it to handle feature datasets, that handling can be fairly easily included, just I thought this approach was fair enough to show the interaction between the script tool interface (with modified Validator) and the main script itself.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Just hope it runs successfully for you this time! It's ready to go if you will download the attached, extract the folder (probably advisable to delete the prior similarly named download to avoid confusion), and run the tool as-is. Report back with any error messages you receive...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 16:47:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685620#M22739</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2021-12-12T16:47:41Z</dc:date>
    </item>
    <item>
      <title>Re: Want to run my Python script as a script tool in a custom toolbox</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685621#M22740</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: pierrek&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you Wayne for being so patient and helpful.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I ran your script and get the following error message. Please find attached a screen shot.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I noticed that you select the first layer in my TOC. I am working on a multiple scale map and my *.mxd document has layers with the same naming multiple times (up to 15). So there would be no need to select the most top layer as you listed in code line 47. ("lyr = arcpy.mapping.ListLayers(mxd, layer)[0] # single-layer datasource replacement, 1st one listed from TOC.") I would need all layers with the same naming.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Anyway you also mentioned another ArcMap version. I am running the latest 10.2.1. I am not sure whether that is of any help.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As I mentioned earlier I am not a coder and the script becomes now even heavier and I kind get even more lost.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Could you please have another look for me at the script you have provided?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks alot.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Pierre&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 01 May 2014 22:25:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685621#M22740</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2014-05-01T22:25:34Z</dc:date>
    </item>
    <item>
      <title>Re: Want to run my Python script as a script tool in a custom toolbox</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685622#M22741</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;That means ListLayers did not find the layer by name...I'm not in the office to troubleshoot, but it appears that layer is within a group layer?&amp;nbsp; So you may have to add additional code to also list those layers inside groups - to confirm, take the target layer outside the group and save the mxd, and rerun the script.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, the for loop can be added back...once this problem is handled.&amp;nbsp; If that's not the problem, then there may be something different about the way the layers listed by the Validator are being passed to the ListLayers function --- the reason why you need the ListLayers function is to get a handle on the layer object.&amp;nbsp; The list within the validator class is just a text list for display purposes....so from there, the proper text chosen from the tool interface has to be passed into the main script.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Question:&amp;nbsp; Is the layer name in the TOC exactly the same as the input appears in the script dialog?&amp;nbsp; ...looks like the text FME may be missing on the end?&amp;nbsp; Not sure if that means anything.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 01 May 2014 22:50:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685622#M22741</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2014-05-01T22:50:23Z</dc:date>
    </item>
    <item>
      <title>Re: Want to run my Python script as a script tool in a custom toolbox</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685623#M22742</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I duplicated your error - the cause is my ListLayers line in the ToolValidator code.&amp;nbsp; Long story short, when layers are embedded within group layers, the returned names take on a 'longName' string, so I substituted this original line that works for non-grouped layers:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;lyrList = [str(lyr) for lyr in arcpy.mapping.ListLayers(mxd)]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...with this line that works for layers, grouped or not:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;lyrList = sorted(set([os.path.basename(str(lyr)) for lyr in arcpy.mapping.ListLayers(mxd) if not lyr.isGroupLayer]))&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also the main script has been corrected to loop on all the layers by the 'short' name, in other words, the layer parameter (text list generated from the validator code) are unique values passed to the main scripts ListLayers function which in turn returns all layer objects (in a group layer or not) by that name to change the datasource.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's the modified main script (also in the newly attached toolbox folder [remember to delete any other toolbox versions to avoid confusion]):&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;# Script Name: Change_datasource # Description: This script allows you to select a specific layer #&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; in your table of content and change the data source. # Created By:&amp;nbsp; Pierre # Date:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 29/04/2014&amp;nbsp;&amp;nbsp; import arcpy import os&amp;nbsp; # Read the parameter values: #&amp;nbsp; 1: Input workspace #&amp;nbsp; 2: Input layer #&amp;nbsp; 3: Set new server connection #&amp;nbsp; 4: Set new source dataset #&amp;nbsp; mxdLocation = arcpy.GetParameterAsText(0) layer = arcpy.GetParameterAsText(1) serverConnection = arcpy.GetParameterAsText(2) newSource = arcpy.GetParameterAsText(3) newMXDname = arcpy.GetParameterAsText(4)&amp;nbsp; def gdbType(connPath): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; connType = os.path.splitext(connPath)[1] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if connType == '.sde': &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('\nsde source.') &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; wsType = 'SDE_WORKSPACE' &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; elif connType == '.gdb': &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('\nfile gdb source:') &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; wsType = 'FILEGDB_WORKSPACE' &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('\nunhandled source type') &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; wsType = '' &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return wsType&amp;nbsp; arcpy.env.workspace = serverConnection if arcpy.Exists(newSource): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; wsType = gdbType(serverConnection) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('the workspace for the new source is {0}\n'.format(serverConnection)) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MXDdir = os.path.dirname(mxdLocation) newMXDname = os.path.join(MXDdir, newMXDname + '.mxd') mxd = arcpy.mapping.MapDocument(mxdLocation)&amp;nbsp; # single-layer datasource replacement, 1st one listed from TOC. #lyr = arcpy.mapping.ListLayers(mxd, layer)[0]&amp;nbsp; # multiple-layer datasource replacement... for lyr in arcpy.mapping.ListLayers(mxd, layer): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.supports("DATASOURCE") and wsType != '': &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('DATASOURCE property is supported for {0}.\n'.format(lyr.longName)) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr.replaceDataSource(serverConnection, wsType, newSource) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('DATASOURCE property is either not supported or handling by this script is not provided.\n')&amp;nbsp; if not os.path.exists(newMXDname): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('Please check the layer properties for ALL layers named \'{0}\', checking the source change in your new map doc:'.format(layer)) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('{0}.\n'.format(newMXDname)) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.saveACopy(newMXDname) else: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddWarning('mxd name is not unique and will not be saved...\n')&amp;nbsp; arcpy.AddMessage('The End.')&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Enjoy - let me know if you have any further problems....it's very interesting working with the ToolValidator class and interacting/debugging with it.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;-Wayne&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;PS- Another thing I didn't mention that you may need depending on how you've named your layers in the TOC.&amp;nbsp; You have the option to use a wildcard for the layer name you're searching for....for example, not that the following is practical for you to do, but you could form this valid wildcard combination:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.mapping.ListLayers(mxd, 'BIOTA_*_ABORIGINAL_LAND*')&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 03 May 2014 02:44:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685623#M22742</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2014-05-03T02:44:30Z</dc:date>
    </item>
    <item>
      <title>Re: Want to run my Python script as a script tool in a custom toolbox</title>
      <link>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685624#M22743</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Original User: pierrek&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you. The script now works perfectly. I made minor adjustments so the script allows me to make changes to my currently open ArcMap document rather saving a copy.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks again. You have been of great help I really appreciate.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Pierre&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 05 May 2014 02:03:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/geoprocessing-questions/want-to-run-my-python-script-as-a-script-tool-in-a/m-p/685624#M22743</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2014-05-05T02:03:51Z</dc:date>
    </item>
  </channel>
</rss>

