From 66f399dbed3d0929648e4ee273f588d343d4f3a6 Mon Sep 17 00:00:00 2001 From: ali Date: Tue, 17 Nov 2026 12:26:48 +0800 Subject: [PATCH] =?UTF-8?q?feat(scripts):=20=E8=B5=84=E6=BA=90=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=99=A8=E5=8F=B3=E9=94=AE=E8=8F=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - install-explorer-menu.ps1:注册 HKCU 右键项(文件夹 / 文件夹空白处 / mkv,mp4,avi 文件) - uninstall-explorer-menu.ps1:一键移除 - explorer-media-rename.ps1:菜单入口,文件取其所在目录,结束后可选暂停窗口 - media-rename.ps1:支持 MEDIA_RENAME_DRYRUN=1 环境变量,便于验证 --- scripts/explorer-media-rename.ps1 | 48 +++++++++++++++++++++++ scripts/install-explorer-menu.ps1 | 60 +++++++++++++++++++++++++++++ scripts/media-rename.ps1 | 2 + scripts/uninstall-explorer-menu.ps1 | 31 +++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 scripts/explorer-media-rename.ps1 create mode 100644 scripts/install-explorer-menu.ps1 create mode 100644 scripts/uninstall-explorer-menu.ps1 diff --git a/scripts/explorer-media-rename.ps1 b/scripts/explorer-media-rename.ps1 new file mode 100644 index 0000000..b207f5b --- /dev/null +++ b/scripts/explorer-media-rename.ps1 @@ -0,0 +1,48 @@ +#Requires -Version 7.0 +<# +.SYNOPSIS + 资源管理器右键菜单的入口:把选中的文件夹(或视频文件所在目录)交给 media-rename。 + +.DESCRIPTION + 由 install-explorer-menu.ps1 注册到: + - 文件夹右键 + - 文件夹空白处右键(当前目录) + - .mkv / .mp4 / .avi 文件右键(取其所在目录) + +.PARAMETER Pause + 执行结束后停住窗口,便于查看输出(由右键菜单传入)。 + +.PARAMETER Paths + 资源管理器传入的选中项(通常一个)。 +#> +[CmdletBinding()] +param( + [switch]$Pause, + [Parameter(ValueFromRemainingArguments = $true)][string[]]$Paths = @() +) + +$ErrorActionPreference = 'Stop' +try { [Console]::OutputEncoding = [Text.Encoding]::UTF8 } catch { } + +$target = $Paths | Where-Object { $_ } | Select-Object -First 1 + +if (-not $target) { + Write-Host '❌ 没有收到要处理的路径' -ForegroundColor Red + if ($Pause) { Write-Host ''; Read-Host '按 Enter 关闭' | Out-Null } + exit 1 +} + +if (Test-Path -LiteralPath $target -PathType Leaf) { + $target = Split-Path -Parent $target +} + +Write-Host "📁 目标: $target" -ForegroundColor DarkGray +& (Join-Path $PSScriptRoot 'media-rename.ps1') -Tokens @($target) +$code = $LASTEXITCODE +if ($null -eq $code) { $code = 0 } + +if ($Pause) { + Write-Host '' + Read-Host '按 Enter 关闭窗口' | Out-Null +} +exit $code diff --git a/scripts/install-explorer-menu.ps1 b/scripts/install-explorer-menu.ps1 new file mode 100644 index 0000000..721d6fe --- /dev/null +++ b/scripts/install-explorer-menu.ps1 @@ -0,0 +1,60 @@ +#Requires -Version 7.0 +<# +.SYNOPSIS + 在 Windows 资源管理器右键菜单里注册「SceneRename 重命名」。 + +.DESCRIPTION + 只写 HKCU(当前用户),不需要管理员权限,可随时用 uninstall-explorer-menu.ps1 移除。 + 注册位置: + HKCU\Software\Classes\Directory\shell\SceneRename (文件夹右键) + HKCU\Software\Classes\Directory\Background\shell\SceneRename (文件夹空白处右键) + HKCU\Software\Classes\SystemFileAssociations\.mkv\shell\SceneRename + ... 同上 .mp4 / .avi(文件右键,取其所在目录) + +.EXAMPLE + .\install-explorer-menu.ps1 +#> +[CmdletBinding()] +param() + +$ErrorActionPreference = 'Stop' + +$runner = Join-Path $PSScriptRoot 'explorer-media-rename.ps1' +if (-not (Test-Path -LiteralPath $runner)) { throw "找不到 $runner" } + +$alias = Join-Path $env:LOCALAPPDATA 'Microsoft\WindowsApps\pwsh.exe' +if (Test-Path -LiteralPath $alias) { + # 应用执行别名,跨 PowerShell 版本升级保持稳定 + $pwsh = $alias +} +else { + $pwsh = (Get-Command pwsh -ErrorAction Stop).Source +} + +function New-MenuCommand([string]$Token) { + # Token: %1 = 选中的文件夹/文件;%V = 当前目录 + return '"' + $pwsh + '" -NoLogo -NoProfile -File "' + $runner + '" -Pause "' + $Token + '"' +} + +$menuText = 'SceneRename 重命名' + +$targets = @( + @{ Key = 'HKCU:\Software\Classes\Directory\shell\SceneRename'; Token = '%1' }, + @{ Key = 'HKCU:\Software\Classes\Directory\Background\shell\SceneRename'; Token = '%V' }, + @{ Key = 'HKCU:\Software\Classes\SystemFileAssociations\.mkv\shell\SceneRename'; Token = '%1' }, + @{ Key = 'HKCU:\Software\Classes\SystemFileAssociations\.mp4\shell\SceneRename'; Token = '%1' }, + @{ Key = 'HKCU:\Software\Classes\SystemFileAssociations\.avi\shell\SceneRename'; Token = '%1' } +) + +foreach ($item in $targets) { + $commandKey = Join-Path $item.Key 'command' + New-Item -Path $item.Key -Force | Out-Null + New-Item -Path $commandKey -Force | Out-Null + New-ItemProperty -Path $item.Key -Name '(default)' -Value $menuText -PropertyType String -Force | Out-Null + New-ItemProperty -Path $item.Key -Name 'Icon' -Value $pwsh -PropertyType String -Force | Out-Null + New-ItemProperty -Path $commandKey -Name '(default)' -Value (New-MenuCommand $item.Token) -PropertyType String -Force | Out-Null +} + +Write-Host '✔ 已注册右键菜单(当前用户)' -ForegroundColor Green +$targets | ForEach-Object { Write-Host (' - ' + $_.Key) -ForegroundColor DarkGray } +Write-Host '提示:若菜单未立即出现,重启一次 explorer.exe 或重新登录。' -ForegroundColor Yellow diff --git a/scripts/media-rename.ps1 b/scripts/media-rename.ps1 index c0851c5..4f4ab20 100644 --- a/scripts/media-rename.ps1 +++ b/scripts/media-rename.ps1 @@ -34,6 +34,8 @@ $ErrorActionPreference = 'Stop' try { [Console]::OutputEncoding = [Text.Encoding]::UTF8 } catch { } $OutputEncoding = [Text.Encoding]::UTF8 +if ($env:MEDIA_RENAME_DRYRUN -eq '1') { $DryRun = $true } + $Remote = 'ali@10.10.1.202' $QuarkDir = '/hdd/sda1/Quark-Down' $Modes = @('manual', 'auto', 'clean-ads', 'extract-subs', 'watch') diff --git a/scripts/uninstall-explorer-menu.ps1 b/scripts/uninstall-explorer-menu.ps1 new file mode 100644 index 0000000..1c2adf7 --- /dev/null +++ b/scripts/uninstall-explorer-menu.ps1 @@ -0,0 +1,31 @@ +#Requires -Version 7.0 +<# +.SYNOPSIS + 移除 install-explorer-menu.ps1 注册的右键菜单。 + +.EXAMPLE + .\uninstall-explorer-menu.ps1 +#> +[CmdletBinding()] +param() + +$ErrorActionPreference = 'Stop' + +$keys = @( + 'HKCU:\Software\Classes\Directory\shell\SceneRename', + 'HKCU:\Software\Classes\Directory\Background\shell\SceneRename', + 'HKCU:\Software\Classes\SystemFileAssociations\.mkv\shell\SceneRename', + 'HKCU:\Software\Classes\SystemFileAssociations\.mp4\shell\SceneRename', + 'HKCU:\Software\Classes\SystemFileAssociations\.avi\shell\SceneRename' +) + +$removed = 0 +foreach ($key in $keys) { + if (Test-Path -LiteralPath $key) { + Remove-Item -LiteralPath $key -Recurse -Force + $removed++ + Write-Host " - 已移除 $key" -ForegroundColor DarkGray + } +} + +Write-Host "✔ 右键菜单已清理($removed 项)" -ForegroundColor Green