5

Esri's documentation on Python toolboxes states, 'When the script is run as a script tool, arcpy is fully aware of the application (such as ArcMap) it was called from.'

How can I check which application launched the Python toolbox tool?

I have a parameter allowing the user to use the display extent as the area of interest, but it only works in ArcMap. I would like to disable the Boolean parameter if launched from ArcCatalog.

1
  • 1
    sys.executable, returns the full path to ArcCatalog.exe or ArcMap.exe or something else if it's run outside of these platforms.. better yet os.path.basename(sys.executable) which returns ArcMap.exe, ArcCatalog.exe or something else if run by double click. See stackoverflow.com/questions/404744/… Oct 16, 2017 at 21:31

1 Answer 1

5

Based on https://stackoverflow.com/questions/404744/determining-application-path-in-a-python-exe-generated-by-pyinstaller I wrote a little script, added it to a toolbox and executed in ArcMap and ArcCatalog:

import os, sys, arcpy

arcpy.AddMessage(os.path.basename(sys.executable))

This returns ArcCatalog.exe if run in catalog, ArcMap.exe if run in arcmap and python.exe if run in a CMD window. Even though the case seems fairly constant I would do a string comparison in either upper or lower case like this:

if os.path.splitext(os.path.basename(sys.executable))[0].lower() == 'arcmap':

Because the case of ArcMap is not guaranteed to be constant between versions.

5
  • Better yet, I'd check != 'python' -- that way it will also work in other apps, such as Pro.
    – Paul
    Oct 16, 2017 at 23:12
  • 1
    I'm not sure what would be returned for ArcGIS Pro @Paul , perhaps PolyGeo can answer that as I'm fairly sure he has access to that platform, I however do not. The question was how to tell if the script is being accessed from Map or Catalog so, although it's a good idea to prepare for every reasonable possibility, the check != 'python' is only the first step in deciding if you need to do some extra display steps. Oct 16, 2017 at 23:35
  • 2
    ArcGISPro.exe is what's returned.
    – Paul
    Oct 17, 2017 at 4:47
  • 1
    if os.path.splitext(os.path.basename(sys.executable))[0].lower() in ['arcmap', 'arcgispro']:?
    – Ian Yau
    Oct 17, 2017 at 19:57
  • 1
    That should be sufficient to ascertain if the script is being run by ArcMap or ArcGIS Pro. Oct 17, 2017 at 21:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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