0

I have an ArcToolbox tool I created based on a script I wrote that needs an input parameter that equals the folder location of the ArcToolbox .tbx file from which the tool runs. I am distributing the toolbox, so users can run it from a variety of locations. How do I create a folder parameter that autofills with a default of the current .tbx location?

I have tried writing a bit of python under the Validation tab of the Tool properties:

def updateParameters(self):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parameter
    has been changed."""

    stringFilter = self.params[1].filter

    list = []

    pathname = os.path.realpath(".")

    list.append(pathname)

    stringFilter.list = list
    if not self.params[1].altered:
        self.params[1].value = stringFilter.list[0]


return

But when I run the tool, the only text that autofills into the parameter reads, "RemoteDatabase"

  • 2
    I cant think of a way to find where the TBX itself is when simply opening the script tool. (The Py file doesnt need to live in the same dir as the TBX). I'd question why you need to point to that particular directory. Why not something like arcpy.env.scratchFolder. That always exists and can be used. Otherwise, see this answer for some known locations available via scripting: gis.stackexchange.com/questions/72348/… – KHibma Aug 9 '18 at 16:15
5

What you're looking for is the __file__ variable, which in this case will contain a path such as:

C:\Users\users\Documents\ArcGIS\Toolbox.tbx#Script.InitializeParameters.py

So just do a pathname = os.path.dirname(__file__) to get the folder in which the .tbx resides.

  • Ahhh you're right. Different behavior from where you call it. Inside validation it points to the TBX. During tool execute it points to the script.py – KHibma Aug 9 '18 at 16:33
  • This works if the parameter type is string. Do you know if there is a way to use it with a workspace type parameter? This is because, although I want the parameter to default to the .tbx folder, the user should also be able to navigate to a workspace of their choosing. Thanks! – spaine Aug 9 '18 at 18:14
  • I identified one problem was that the workspace and folder parameter types cannot default to a list (as was happening in my script above). However, defaulting to a single string does work if the parameter type is folder. It also works for workspace parameter types, even though the tool shows a red X and an error warning that "The workspace is not the correct workspace type" if the workspace type is filtered to specific workspace types. – spaine Aug 9 '18 at 18:38
  • Directly setting a workspace parameter's value to the full path of a folder works totally fine for me, even when filtering to disallow remote databases. No need to edit the filter in the code, just set it in the parameter edit dialog. I suspect either you have your filters wrong or there's something else wrong with the code, in which case we'd need to see its current state – mikewatt Aug 9 '18 at 22:07
  • Also, you may have identified this, but you shouldn't use list as a variable name, since it's a reserved word in Python. – mikewatt Aug 9 '18 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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