<?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 Interactively setting the current workspace using the ToolValidator class in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/interactively-setting-the-current-workspace-using/m-p/601966#M47049</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a tool which has the user select a workspace in the first parameter.&amp;nbsp; Once the user selects the appropriate workspace I would then like the tool's environmental settings to be changed so the current workspace be set to that select workspace.&amp;nbsp; What I am hoping to accomplish is if the user just inputs a name in a feature class output parameter, the new featureclass will be created in the previously selected workspace by default.&amp;nbsp; I have tried using the ToolValidator however it doesn't seem to change the tool's environment settings.&amp;nbsp; I have included code from my ToolValidator class showing two ways I have tried to accomplish this.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for any advice or suggestions, &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Jim Oakleaf&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;class ToolValidator:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; def __init__(self):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.params = arcpy.GetParameterInfo()&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; def initializeParameters(self):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; def updateParameters(self):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if self.params[0].altered:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; theW = self.params[0].value&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dsW=arcpy.Describe(theW)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.workspace=dsW.catalogPath&amp;nbsp; """Tried this method"""&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.scratchWorkspace=theW&amp;nbsp;&amp;nbsp; """Also tried this method"""&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; def updateMessages(self):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 15 Nov 2012 20:53:05 GMT</pubDate>
    <dc:creator>JimOakleaf</dc:creator>
    <dc:date>2012-11-15T20:53:05Z</dc:date>
    <item>
      <title>Interactively setting the current workspace using the ToolValidator class</title>
      <link>https://community.esri.com/t5/python-questions/interactively-setting-the-current-workspace-using/m-p/601966#M47049</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a tool which has the user select a workspace in the first parameter.&amp;nbsp; Once the user selects the appropriate workspace I would then like the tool's environmental settings to be changed so the current workspace be set to that select workspace.&amp;nbsp; What I am hoping to accomplish is if the user just inputs a name in a feature class output parameter, the new featureclass will be created in the previously selected workspace by default.&amp;nbsp; I have tried using the ToolValidator however it doesn't seem to change the tool's environment settings.&amp;nbsp; I have included code from my ToolValidator class showing two ways I have tried to accomplish this.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for any advice or suggestions, &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Jim Oakleaf&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;class ToolValidator:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; def __init__(self):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; import arcpy&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.params = arcpy.GetParameterInfo()&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; def initializeParameters(self):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; def updateParameters(self):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if self.params[0].altered:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; theW = self.params[0].value&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dsW=arcpy.Describe(theW)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.workspace=dsW.catalogPath&amp;nbsp; """Tried this method"""&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.env.scratchWorkspace=theW&amp;nbsp;&amp;nbsp; """Also tried this method"""&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; def updateMessages(self):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 15 Nov 2012 20:53:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/interactively-setting-the-current-workspace-using/m-p/601966#M47049</guid>
      <dc:creator>JimOakleaf</dc:creator>
      <dc:date>2012-11-15T20:53:05Z</dc:date>
    </item>
    <item>
      <title>Re: Interactively setting the current workspace using the ToolValidator class</title>
      <link>https://community.esri.com/t5/python-questions/interactively-setting-the-current-workspace-using/m-p/601967#M47050</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;SPAN&gt;I don't understand why you want to do this in ToolValidator? Why not in script itself?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Arek&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 16 Nov 2012 06:31:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/interactively-setting-the-current-workspace-using/m-p/601967#M47050</guid>
      <dc:creator>ArkadiuszMatoszka</dc:creator>
      <dc:date>2012-11-16T06:31:34Z</dc:date>
    </item>
    <item>
      <title>Re: Interactively setting the current workspace using the ToolValidator class</title>
      <link>https://community.esri.com/t5/python-questions/interactively-setting-the-current-workspace-using/m-p/601968#M47051</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Arek -&amp;nbsp; I am trying to make it easier when the user is inputting a parameter.&amp;nbsp; I would like the default location for other named datasets being created point to the same directory.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So for example the user has&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1) Project Workspace parameter&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2) Analysis Output location&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So the user sets their project workspace and then sets the name of the output location however since I am using a data type of Raster Dataset with an output direction, the parameter wants to use the current workspace set by the tool as the default location if a user just inputs a name.&amp;nbsp; Additionally the browser dialog which opens points to the last workspace a dataset was written to which can also be different.&amp;nbsp; I just want them both to point to the users selected workspace by default.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have attached an image which may make more sense.&amp;nbsp; So in the image, the user selected &lt;/SPAN&gt;&lt;STRONG&gt;test.gdb&lt;/STRONG&gt;&lt;SPAN&gt; as their project workspace and then just typed in New which defaulted to creating the dataset in the &lt;/SPAN&gt;&lt;STRONG&gt;Default.gdb&lt;/STRONG&gt;&lt;SPAN&gt; (current workspace of tool) and then to make things even more convoluted if the user clicks to open a dialog they are being directed to the &lt;/SPAN&gt;&lt;STRONG&gt;NewSetup.gdb&lt;/STRONG&gt;&lt;SPAN&gt; which was one used by them most recently.&amp;nbsp; If I click on the Environments button on the tool and then change the current workspace the default output location is the current workspace set.&amp;nbsp; It just seems like I should be able to do this through code once the user selects their workspace.&amp;nbsp;&amp;nbsp; I guess I could just set warnings if the location of the output is in a different workspace than what the user selects. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks, jim&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[ATTACH=CONFIG]19379[/ATTACH]&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 16 Nov 2012 15:24:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/interactively-setting-the-current-workspace-using/m-p/601968#M47051</guid>
      <dc:creator>JimOakleaf</dc:creator>
      <dc:date>2012-11-16T15:24:55Z</dc:date>
    </item>
    <item>
      <title>Re: Interactively setting the current workspace using the ToolValidator class</title>
      <link>https://community.esri.com/t5/python-questions/interactively-setting-the-current-workspace-using/m-p/601969#M47052</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt; So the user sets their project workspace and then sets the name of the output location however since I am using a data type of Raster Dataset with an output direction, the parameter wants to use the current workspace set by the tool as the default location if a user just inputs a name. (1)&amp;nbsp; Additionally the browser dialog which opens points to the last workspace a dataset was written to which can also be different. (2)&amp;nbsp; I just want them both to point to the users selected workspace by default.&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;(1) Unfortunately, I think the workspace setting is only local to the validate functions, you can't migrate it "up". So you need to do it this way:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# the first parameter [0] is the (input) workspace parameter
# to make things easy for the user, set this to default to the current workspace
# in the parameter properties dialog (or in the code if pyt toolbox)
# the second [1] is your output dataset.
def updateParameters(self):
 if self.params[0].value and self.params[1].altered:
&amp;nbsp;&amp;nbsp;&amp;nbsp; outFC = str(self.params[1].value)
&amp;nbsp;&amp;nbsp;&amp;nbsp; import os
&amp;nbsp;&amp;nbsp;&amp;nbsp; if os.path.dirname(outFC) == ""
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.params[1].value = os.path.join(self.params[0].value,outFC)
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;(2) Bad news, the workspace that appears in the popup when you click the folder is not the current workspace, it's either the home folder, or the last folder opened with an open dialog. This is a Windows UI standard I think, although it is annoying!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 01:49:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/interactively-setting-the-current-workspace-using/m-p/601969#M47052</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-12T01:49:01Z</dc:date>
    </item>
    <item>
      <title>Re: Interactively setting the current workspace using the ToolValidator class</title>
      <link>https://community.esri.com/t5/python-questions/interactively-setting-the-current-workspace-using/m-p/601970#M47053</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hey Curtis - Thanks for your response!&amp;nbsp; Unfortunately before moving away from the input box ArcGIS is placing a workspace in front of the inputted name.&amp;nbsp; I am just going to go with a warning for users and move on.&amp;nbsp; Your code helped me figure out how to do this, so thanks!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if self.params[0].value and self.params[6].altered:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outFC = str(self.params[6].value)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.params[6].clearMessage&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; import os&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if os.path.dirname(outFC)!= str(self.params[0].value):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.params[6].setWarningMessage("Output location not within project folder!")&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 19 Nov 2012 20:38:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/interactively-setting-the-current-workspace-using/m-p/601970#M47053</guid>
      <dc:creator>JimOakleaf</dc:creator>
      <dc:date>2012-11-19T20:38:17Z</dc:date>
    </item>
    <item>
      <title>Re: Interactively setting the current workspace using the ToolValidator class</title>
      <link>https://community.esri.com/t5/python-questions/interactively-setting-the-current-workspace-using/m-p/601971#M47054</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Unfortunately before moving away from the input box ArcGIS is placing a workspace in front of the inputted name.&amp;nbsp; I am just going to go with a warning for users and move on.&amp;nbsp; Your code helped me figure out how to do this, so thanks!&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if self.params[0].value and self.params[6].altered:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outFC = str(self.params[6].value)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.params[6].clearMessage
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; import os
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if os.path.dirname(outFC)!= str(self.params[0].value):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.params[6].setWarningMessage("Output location not within project folder!")&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You're right, the internal validation for output filenames gets there before we can alter it (when the output parameter is initialized).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For best results, be sure and update parameter values in the &lt;/SPAN&gt;&lt;STRONG&gt;updateParameters&lt;/STRONG&gt;&lt;SPAN&gt; function -- and set validation messages (like setWarningMessage above) in the &lt;/SPAN&gt;&lt;STRONG&gt;updateMessages&lt;/STRONG&gt;&lt;SPAN&gt; function.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's the way I'd do it:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&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; if self.params[0].value and self.params[6].value:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; import os
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; inWS = os.path.dirname(str(self.params[0].value))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; outWS = os.path.dirname(str(self.params[6].value))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if inWS != outWS:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; self.params[6].setWarningMessage(
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "Output will be written to a different workspace than input!")
&amp;nbsp;&amp;nbsp;&amp;nbsp; return
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 01:49:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/interactively-setting-the-current-workspace-using/m-p/601971#M47054</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-12T01:49:03Z</dc:date>
    </item>
  </channel>
</rss>

