<?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: How to select layer from table of contents programmatically? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-select-layer-from-table-of-contents/m-p/299826#M23201</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I would also prefer not to hard code the layer path, since I have a shapefile joined with a table in the table of contents and that is the shapefile I want to export, not the original shapefile without join. In ArcObject, we could use pMap.Layer(i) where i (0,1,2,3 etc.) refers to the layers' order in the table of contents. Here's the concept of the code, although I couldn't get it to run. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
arcpy.env.overwriteOutput = True

mxd = arcpy.mapping.MapDocument(r"C:\temp\test.mxd")

# export the second layer in the table of contents

for lyr(1) in arcpy.mapping.ListLayers(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management(lyr(1), r"R:\Corp Data Mgt\SGurung\temp\output")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 14:23:16 GMT</pubDate>
    <dc:creator>sarbajitgurung</dc:creator>
    <dc:date>2021-12-11T14:23:16Z</dc:date>
    <item>
      <title>How to select layer from table of contents programmatically?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-select-layer-from-table-of-contents/m-p/299817#M23192</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I need to select second layer from table of contents of an active data frame and then export that layer to a new feature class. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
import arcpy

# Point to current map document
mxd = arcpy.mapping.MapDocument("CURRENT")

# Now how do I select the second layer from table of contents of current map document?

# Once I figure that out I can then export the layer by using "arcpy.CopyFeatures_management()"
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Please help.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 20 Aug 2012 21:09:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-select-layer-from-table-of-contents/m-p/299817#M23192</guid>
      <dc:creator>sarbajitgurung</dc:creator>
      <dc:date>2012-08-20T21:09:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to select layer from table of contents programmatically?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-select-layer-from-table-of-contents/m-p/299818#M23193</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I don't believe the Layer object supports indexing but here is another method&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1. Find out the what the data source is for the layer&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")
for lyr in arcpy.mapping.ListLayers(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print lyr.dataSource
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;2. Next I would set a conditional statement&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")
for lyr in arcpy.mapping.ListLayers(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.dataSource == #place the results from the previous script here
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Do your export here.
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This method in my opinion allows for more flexibility because even if the name of the layer changes in the TOC, the datasource is less likely to change especially if the source is within a gdb.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 14:23:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-select-layer-from-table-of-contents/m-p/299818#M23193</guid>
      <dc:creator>KenCarrier</dc:creator>
      <dc:date>2021-12-11T14:23:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to select layer from table of contents programmatically?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-select-layer-from-table-of-contents/m-p/299819#M23194</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Ken, I've followed your code but I've been unable to run the code. Here's the code: &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(r"C:\temp\test.mxd")

for lyr in arcpy.mapping.ListLayers(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.dataSource == r"C:\temp\test.shp"
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management(lyr, r"C:\temp\output\testout.shp")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any suggestions?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 14:23:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-select-layer-from-table-of-contents/m-p/299819#M23194</guid>
      <dc:creator>sarbajitgurung</dc:creator>
      <dc:date>2021-12-11T14:23:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to select layer from table of contents programmatically?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-select-layer-from-table-of-contents/m-p/299820#M23195</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I got this to work on my data with a local mxd.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
mxd = arcpy.mapping.MapDocument("CURRENT") 
lyrs = arcpy.mapping.ListLayers(mxd)

for lyr in lyrs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.dataSource == r"C:\Temp\TestTopology.gdb\TaxParcels\parcels":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "True"
&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; print "False"
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope this helps, it returns True for me. Are you running this in ArcMap or as a standalone script? I ran mine from within the mxd. It will just need some tweaking if you are running as a standalone.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 14:23:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-select-layer-from-table-of-contents/m-p/299820#M23195</guid>
      <dc:creator>KenCarrier</dc:creator>
      <dc:date>2021-12-11T14:23:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to select layer from table of contents programmatically?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-select-layer-from-table-of-contents/m-p/299821#M23196</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Syntax was incorrect... try this...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
arcpy.env.overwriteOutput = True

# mxd = arcpy.mapping.MapDocument(r"C:\temp\test.mxd")
mxd = arcpy.mapping.MapDocument("CURRENT")

for lyr in arcpy.mapping.ListLayers(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.dataSource == r"C:\temp\test.shp":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management(lyr, r"C:\temp\output\testout.shp")
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print lyr.dataSource + " not exported..."&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;NOTE:&amp;nbsp; You need to make sure that the "output" directory is already created... it will not automatically create it...&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also note that the "else:" statement is not really needed, but it might help you troubleshoot the path name...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jason&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 14:23:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-select-layer-from-table-of-contents/m-p/299821#M23196</guid>
      <dc:creator>JasonMiller</dc:creator>
      <dc:date>2021-12-11T14:23:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to select layer from table of contents programmatically?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-select-layer-from-table-of-contents/m-p/299822#M23197</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Jason,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;good catch&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I did not see he forgot the : after the if statement.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 21 Aug 2012 18:42:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-select-layer-from-table-of-contents/m-p/299822#M23197</guid>
      <dc:creator>KenCarrier</dc:creator>
      <dc:date>2012-08-21T18:42:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to select layer from table of contents programmatically?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-select-layer-from-table-of-contents/m-p/299823#M23198</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Jason and Ken, that was a good catch indeed as my tiring eyes missed that colon. It runs well when I run it from Python window in ArcGIS but it crashes when I run it in PythonWin. The only thing I've changed in PythonWin is: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
#mxd = arcpy.mapping.MapDocument("CURRENT")
mxd = arcpy.mapping.MapDocument(r"C:\temp\test.mxd")
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;i.e., instead of referring to CURRENT map document, I've used the path as I need to automate this task. Any suggestions?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 14:23:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-select-layer-from-table-of-contents/m-p/299823#M23198</guid>
      <dc:creator>sarbajitgurung</dc:creator>
      <dc:date>2021-12-11T14:23:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to select layer from table of contents programmatically?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-select-layer-from-table-of-contents/m-p/299824#M23199</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;First, make sure that your C:\temp\output\&amp;nbsp; directory already exists, because it will not be created automatically...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Second, I edited my previous post to include the line&amp;nbsp;&amp;nbsp; arcpy.env.overwriteOutput = True&amp;nbsp;&amp;nbsp; which can go just after your import statement...&amp;nbsp; It will make so that no error is thrown when you try to overwrite an existing file... What probably happened is that it ran fine the first time (in ArcGIS) because your output folder was empty, but when you ran it again (in PythonWin) the shapefile already existed..&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If neither of those is of any help, then please post your error code...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jason&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Aug 2012 00:26:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-select-layer-from-table-of-contents/m-p/299824#M23199</guid>
      <dc:creator>JasonMiller</dc:creator>
      <dc:date>2012-08-22T00:26:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to select layer from table of contents programmatically?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-select-layer-from-table-of-contents/m-p/299825#M23200</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Jason. I know the output directory won't be created automatically, so I created it before running the script. I'm running the following script on PythonWin. It doesn't give me any error, it just crashes when I run this script. Is this a common problem with PythonWin? It works when I run it from IDLE in ArcGIS. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
arcpy.env.overwriteOutput = True

mxd = arcpy.mapping.MapDocument(r"C:\temp\test.mxd")

for lyr in arcpy.mapping.ListLayers(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if lyr.dataSource == r"C:\temp\test.shp":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management(lyr, r"C:\temp\output")
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print lyr.dataSource + "Not exported!"&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 14:23:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-select-layer-from-table-of-contents/m-p/299825#M23200</guid>
      <dc:creator>sarbajitgurung</dc:creator>
      <dc:date>2021-12-11T14:23:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to select layer from table of contents programmatically?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-select-layer-from-table-of-contents/m-p/299826#M23201</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I would also prefer not to hard code the layer path, since I have a shapefile joined with a table in the table of contents and that is the shapefile I want to export, not the original shapefile without join. In ArcObject, we could use pMap.Layer(i) where i (0,1,2,3 etc.) refers to the layers' order in the table of contents. Here's the concept of the code, although I couldn't get it to run. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
arcpy.env.overwriteOutput = True

mxd = arcpy.mapping.MapDocument(r"C:\temp\test.mxd")

# export the second layer in the table of contents

for lyr(1) in arcpy.mapping.ListLayers(mxd):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management(lyr(1), r"R:\Corp Data Mgt\SGurung\temp\output")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 14:23:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-select-layer-from-table-of-contents/m-p/299826#M23201</guid>
      <dc:creator>sarbajitgurung</dc:creator>
      <dc:date>2021-12-11T14:23:16Z</dc:date>
    </item>
  </channel>
</rss>

