New scripts system in 3.77
Goals:
- To provide additional flexibility - use it with python, nushell, or other shells and command line executors, even ffmpeg or anything that can run from command line
- Enable long running operations (current system was never designed for loch copy and other heavy operations)
- Runs in a separate process in a new command line window (current system was not designed for input, for verbose logging or detailed error messages)
- Uses env variables instead of search&replace
- Breaks existing scripts so don't change unless you want to test the new system and provide feedback on how to improve it
- Enable in Settings>Advanced>Experimental>Experimental Scripts system
Details:
The first time you open scripts menu a new ScriptExecutors.json will be created in settings folder
You can use it to customize the header tags (like #PS) or extensions to use for different scripts. These can be comma-separated.
The way this works in the background is that program first checks the first line of a script file that is being executed if there is a tag like #PS, if so, it will use the first ScriptExecutor from that file, basically using the "executable" there, pass it "Arguments" listed there, and then add the script to it.
Example:
Powershell.exe -NoExit -NoProfile "D:\OC\Resources\Scripts\MyScript.ps1"
So it opens the actual powershell window
If there is no # tag, it will look if any executor in the list has that Extension associated with it (it doesn’t look through registry). This way you should be able to use any existing script file, but if you want to have data from OC (like list of selected files) you have to use env variables in your script. With env variables, any program that OC starts can just read those env variable and do what it needs with them.
Environment Variables
SELECTED_FILES
CURRENT_DIR
SELECTED_FILES_INACTIVE
CURRENT_DIR_INACTIVE
Let me know if see any conflicts - maybe we need some prefix like OC_ so it is OC_SELECTED_FILES
See example V2-Test-Example.ps1 from ScriptsInstall folder
I am not powershell or scripts expert, but it looks like this is easy enough:
$files = $env:SELECTED_FILES -split '\r\n'
//because files are separated by Windows NewLine character
You can figure out how to use env variables from pyhon, nushell or other scripts
ScriptExecutors.json is loaded only on program start, so if you manually modify it, restart program
Let me know if you think something else should be default that would benefit most users.
Do not convert your existing scripts just yet as a lot may change until this distils to a good process
I am not sure what is the future of the old scripts system as the Search&Replace was a bad idea and running in OC process was never meant for long operations but for millisecond operations like renaming a file, creating a folder or similar.
Old system now has #PS7 tag that will use pwsh.exe instead of powershell.exe (if installed)
But now for #PS7 the variables are with "OC" prefix - (many people already use $CurrentDir and similar in their scripts, so to avoid mistakes). $OCSelectedFiles, $OCCurrentDir, $OCSelectedFilesInactiveBrowser, $OCCurrentDirInactiveBrowser
Old system: added #RUNEXTERNAL (in 2nd line - combining tags in one line not supported) to open a new window instead of running in OC process. That might need to be default and tag might be changed to #RUNINOC to do the opposite. (Not much tested as I spent most time past weekend working on the new system)
It is a bit mess at this time (hence experimental) but I hope that with your feedback we can find a good way to move new scripting system forward