I have an ArcGIS toolbox script that applies a user-defined definition query to multiple layers within the current MXD.

The only user variable currently required is the definition query itself, which I have listed as an "SQL expression". The script works fine, but it would be nice if the SQL validation box on the toolbox GUI was populated with the details of the first layer in the MXD, allowing the user to construct and validate a definition query against the first layer.

How can I achieve this using a hard-coded variable pointing to the first layer in the TOC?

It's fairly straightforward in a one-to-one scenario, by asking the user to input the feature layer as a parameter and then making the SQL expression parameter "Obtained from", but I can't figure out how to do it by defining it myself.

closed as off-topic by PolyGeo Apr 9 at 10:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.


I think this is what you're looking for:

# stores current map doc in a variable
mxd = arcpy.mapping.MapDocument("CURRENT")

# accesses the first layer in mxd
lyr = arcpy.mapping.ListLayers(mxd)[0]
  • Thanks. I already had the code to access the first layer. It was making another parameter dependent upon it that I was struggling with. I have a solution though which I will post below. – Rich Mar 7 at 9:58

I eventually figured this out with some help from an ESRI trainer.

The key was to feed the hardcoded parameter in using tool validation at the initialisation stage and list it as a parameter of type "derrived" (which hid it on the GUI) and then make the user input parameter "obtained from" it.

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