2
Mapaler
Since you have already got
$filename_postfix_length variable defined in/by your
PowerShell script--you could let that same script do all the other required things.
Your
PowerShell script (which I named
iconSwitch.ps1 within my test) with a little bit more commands:
Code: Select all
<#
$EnvironmentVariableName = "filename_postfix_length"
$EnvironmentVariableScope = "User"
function InputLength {
param(
[Parameter(Mandatory,
ValueFromPipeline)]
[ValidateRange(1,9)]
[byte]$length
)
PROCESS {
Write-Output $length
}
}
Write-Host "Current filename postfix length is $([Environment]::GetEnvironmentVariable($EnvironmentVariableName, $EnvironmentVariableScope))。"
$length = Read-Host -Prompt "Input new postfix length, only input 1-9" | InputLength
[Environment]::SetEnvironmentVariable($EnvironmentVariableName, $length, $EnvironmentVariableScope)
Write-Host "The new postfix length is $([Environment]::GetEnvironmentVariable($EnvironmentVariableName, $EnvironmentVariableScope))。"
#>
# in your case $filename_postfix_length variable is defined by your code above.
# here in my example, for the sake of convenience, I would not use your code,
# and define it (the variable) instead as a random number within 0..241 (nothing special, just reflecting how many icons I used in the test):
$filename_postfix_length = (Get-Random -min 0 -max 241)
# then I would use it to define an $icon suitable for the Total Commander toolbar button
$icon = '%OneDrive%\Icons\'+$filename_postfix_length.toString()+'.ico'
# define the button bar within the Total Commander path to deal with
$buttonBar = [IO.Path]::combine($env:commander_path,'Custom\myButton.bar') # in your case it could be ...combine($env:commander_path,'VERTICAL.BAR')
# define icon source path within OneDrive
$iconLibrary = [IO.Path]::combine($env:OneDrive,'Icons')
# some encoding examples (highly recommend to keep your .bar files as UTF-16LE with BOM)
$ansi = [Text.Encoding]::getEncoding("windows-1251")
$utf8 = [Text.Encoding]::getEncoding("utf-8")
$unicode = [Text.Encoding]::getEncoding("Unicode")
# read contents of the $buttonBar as the $text array
$text = [IO.File]::ReadAllText($buttonBar)
# define a pattern (to understand we are dealing with the right button)
$pattern = 'button33=' # in your case it could be 'button1='
# analyze the $buttonBar contents (defined as the $text array) line by line
foreach ($line in $text) {
if ($line -match $pattern) {
# in $line that matches $pattern change icon path to $icon and that becomes an $update
$update = $line -replace "button33=(.*)","button33=$icon" # there are too many literals here in this line: I was way lazy to automate it properly, sorry
# then, in its turn, in the $text we have we redefine that $line as the $update
$text = $text.replace($line,$update)}}
# write the updated $text back to $buttonBar
[IO.File]::WriteAllText($buttonBar,$text,$unicode)
<# this command in the script has been commented after adding the cm_wait command into the TC commands chain
# wait a bit just in case (to make sure the updated $buttonBar is properly saved)
sleep -m 1000 # 1000 milliseconds :: you may change this to whatever suits your environment
#>
#pause
User-command
em_iconSwitch (in usercmd.ini)
Code: Select all
[em_iconSwitch]
cmd=powershell -c "%commander_path%\Plugins\PowerShell\iconSwitch.ps1"
User-button with chained
em_iconSwitch and
cm_ReloadBarIcons commands (also the
cm_wait command has been added into the sequence)
Code: Select all
TOTALCMD#BAR#DATA
em_iconSwitch,cm_wait 1000,cm_ReloadBarIcons
WCMICON2.DLL
em_iconSwitch,cm_wait 1000,cm_ReloadBarIcons chain
-1
Here's how it works (video illustration):
Image:
https://i.imgur.com/btqdkq4.mp4 (before adding the cm_wait command)
Image:
https://i.imgur.com/XNxGs0a.mp4 (same as above with the cm_wait command added into the TC command sequence)
Note: The rightmost button on the toolbar is where we expect icon switching to occur (it's button 33 on my toolbar), and the button next to it (the second to the right) is the command button that runs the
em_iconSwitch,
cm_wait,
cm_ReloadBarIcons command sequence.
Note 2: A friendly piece of advice, if such an option is under your control, switch from Windows PowerShell to cross-platform PowerShell:
https://learn.microsoft.com/en-us/powershell/scripting/whats-new/migrating-from-windows-powershell-51-to-powershell-7
https://learn.microsoft.com/en-us/powershell/scripting/whats-new/differences-from-windows-powershell
https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows
https://github.com/PowerShell/PowerShell/releases
This is a user-friendly upgrade:
You still keep the outdated Windows PowerShell (moreover, I won't recommend deleting it, sometimes it is useful for testing, etc.),
while having the modern alternative--the cross-platform one, that is in active development.
On a user level (after the upgrade):
- whenever you type "powershell" (including TotalCommander user commands) you are using Windows PowerShell,
- whenever you type "pwsh" you are using cross-platform PowerShell, and that's it.
Note 3. By the way, it's been a pretty interesting challenge to complete. Thanks.