I've created a versatile add-in script that automates executing different arbitrary commands for different corresponding specified folder paths, including with wildcards and several other features.
Features:
Commands
Set any number of path-command pairs with one script
Run a command upon entering a specific folder path
Specify an optional leave command in addition to entry command
Supports exact paths, wildcards, aliases, and windows environment variables
Smart Execution
Works whether navigating into a matching folder, or switching to a matching tab
Leave command will only trigger when entering a non-matching folder, or switching to a non-matching tab
For example, if entering sub-folders of a wildcarded path, it won't re-execute every time (unless optional switch is enabled)
Optional switches to always run "entry" and/or "leave" command when entering a matched path, even if already coming from a matched path.
Switches individually enabled/disabled for each path
Example Use Case:
Automatically opening a toolbar when entering a specific folder or path, and closing it when leaving the folder or switching to a different tab
Automatically enabling Flat View within a certain folder
Anything else that can't already be accomplished via a custom folder format
Redirecting from library "lib://" paths to their true drive path (See Example)
Usage:
Open the FolderCommandPairs config setting and enter any number of path-command groups with the following format:
X is a number (1, 2, 3, etc.) to group related commands
Available switches: AlwaysRunEntry, AlwaysRunLeave, DontResolvePath
-- AlwaysRunEntry, AlwaysRunLeave: Lets you run the entry/leave commands even when the next folder also matches the rule.
-- DontResolvePath: The given path will not be resolved before being checked against the lister path. May be necessary for paths like lib:// which would be re-written as C:\Users... and therefore might not match when expected. (Detailed Explanation here)
Other Config Notes:
The folder path can include wildcards (*), folder aliases, and Windows environment variables
Commands can even include built-in Opus function arguments like {sourcepath}
Lines starting with // are treated as comments. Empty lines are also ignored.
LeaveCommand and Switches are optional and need not be included.
Open the Script Management window (Main Toolbar > Settings > Scripts)
Drag the downloaded AutoFolderCommand.vbs.txt to the list.
In the Script Management window, click the Gear to open the config settings
Note: It's probably necessary to temporarily enable the DisableCache option after changing the FolderCommandPairs config for changes to take effect. Restarting Opus would work too.
Examples / Screenshots
Example Config (The script does have an example config set by default)
I've just updated the script to version 1.1.0 with a new optional switch called DontResolvePath which lets you control whether the path you enter for a particular path/command group will be automatically resolved or not before checking for a match.
For example, say you have this command pair, to automatically redirect a library to its absolute path:
Path1 = lib://Documents
EntryCommand1 = Go C:\Users\YourName\Documents
Normally, the script would use the FSUtil.Resolve method on the path which would result in the path being set as C:\Users\YourName\Documents, then stores that to check against the path shown in the lister. This allows you to use aliases in the script's config, etc.
But in this case, we don't actually want the resolved version, because the path that shows in the lister is literally lib://Documents, so we want to keep that as the path to check against. Therefore before, it wouldn't detect a match even though the path used in the config is what shows in the lister.
So now you can include the DontResolvePath switch for that particular group and it should work.
For example this is what I have in my config to redirect any library to its true path:
Path3 = lib://*
EntryCommand3 = Go {sourcepath}
Switches3 = DontResolvePath
Also note that you can include the usual function arguments like {sourcepath} just like you would use in a User Command. And also notice how wildcards still work because that is checked separately.
Hi, I'm trying to open a certain folder always with admin activated, then un-activate admin when I get out of the folder.
Path1 = C:\!Scripts
EntryCommand1 = Set ADMIN=Toggle,0
This is what I wrote in config. I also tried with Set ADMIN=On or Off. But neither works. I know that "Set ADMIN=Toggle,0" works well as a button to toggle admin on and off without the UAC prompt. That is what I wanted to achieve, automatically, when entering the folder, but nothing I tried worked.
P.S. Are there other ways to achieve this without using this script? I mean to activate admin when navigating to a folder and disable after exiting the folder.
Hm well right now the script might not work well for this use case because it runs the 'exit' command also when the specified path isn't the active tab in the lister any more (if you switch tabs). You'd just have to probably try not to switch tabs in that lister or else you'd be repeatedly re-prompted for UAC escalation.
When I can get around to it I might be able to add a new switch for "only on close" to only run the exit command if the path is actually closed and not if the lister is just switched to another tab.
That being said I think it should theoretically work still right now, but it seems I could only get the command to run without a timeout. So like Set ADMIN=On or Set ADMIN=Toggle, which shows the prompt. I wonder if it's a security feature to prevent scripts from secretly escalating themselves.
I think no matter what you'd still need the UAC prompt by the way, I think you mean the Opus prompt for how long to timeout the admin mode.
Hello and Thank You for responding. Big fan of your work on Youtube. I have a folder with Autohotkey scripts, which I regularly update. But every time I edit them I forget to elevate to admin and when I'm done editing I have to restart notepad++ as admin, copy paste the changes and it is frustrating as it keeps happening again and again. Was just looking for a solution and found your script. Sadly, it does not work for me. Neither the admin=toggle version, nor simple admin=on. It does not prompt the UAC. I guess I'll have to build my muscle memory and activate admin manually when I get into the scripts folder. I even mapped the simplest shortcut Shift+A to it, but still forget each time.