Script For Running Various Commands When Entering Specific Paths

Auto Commands For Specific Folders

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:

PathX = <folder_path>
EntryCommandX = <command_to_run_on_entry>
LeaveCommandX = <command_to_run_on_leave>
SwitchesX = <comma_separated_switches>

Where:

  • 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.

Installation

  • Download AutoFolderCommand.vbs.txt (20.4 KB)
  • 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)

Config Window


Changelog:

  • 1.0.0 → 1.1.0:
    • Added "DontResolvePath" switch
    • Added note that standard function command arguments can be included in commands
10 Likes

Nice work!

I added a link from Trigger Flat View (or any other command) in configured folders to here, since your script is like a much more powerful version of that old one.

2 Likes

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.

2 Likes