Toolbar button script executes across multiple lister windows

I have a custom button on my toolbar with the following script assigned:

Go "%USERPROFILE%\Documents\"
CreateFolder NAME "{date|yyyy-MM-dd}" READAUTO

Its purpose is to create a new folder based on today's date in my documents folder. If I have only a single lister open, then it always works correctly. If I have more than one open, then it sometimes "splits" the two commands, the first changing the active folder in the lister in which I clicked, the latter then creating the folder in whatever location the other lister has open.

I haven't been able to figure out why this is happening or how to stop it. Can somebody help? Thanks in advance.

You can do it all in one line:

CreateFolder NAME "%USERPROFILE%\Documents\{date|yyyy-MM-dd}" READAUTO

Or, slightly shorter:

CreateFolder NAME "/MyDocuments\{date|yyyy-MM-dd}" READAUTO

First, thanks! I'll try that and see if it fixes my issues.

Second, I'd like to add one minor point and one question, both intended for the edification of others. The minor point is that Leo's use of the "/MyDocuments" shortening for "%USERPROFILE%\Documents" has a subtle potential difference: the former will map to OneDrive if you have it handling your my-documents folder, whereas the latter will always map to your physical system drive. In other words, I can't use the shortening in my case because I do not want the folder in OneDrive; I want it only on my local drive given the content I put into it.

The question is this: I certainly get how changing the two lines to one makes the operation more "atomic", but it doesn't make me any more sanguine about executing script that cannot be collapsed like that within the context of a single lister. Is there a way to make sure all my script for a given button executes within the context of the lister in which I pressed the button? In short, as much as I'm happy with a fix, I'd like to know how to avoid the problem in the future as well.

Thanks in advance!

Generally, don't make changes to the UI if they are just to change the context that later commands will work on. It works sometimes, but at other times will have unexpected results (due to timing, or other windows causing the command to use a context that wasn't expected, or the mode the UI is in not allowing the change or doing something unusual with it if the mode is unusual; or due to you clicking on things while a command is still running and changing the context underneath the command).

In general, only change folders, or select files, and so on, if that's the end result you want the UI to be left in after the command finishes. The Go command is the thing to use if you want to navigate the UI to a folder. But it's not the thing to use if you want to tell the CreateFolder command which folder you want it to create things in.

If you want a command to work on specific files or a specific folder path, tell the command the path you want it to work on directly. That's what the one-line version does.