Files
SceneMarshal/scripts/install-explorer-menu.ps1
T

237 lines
11 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#Requires -Version 7.0
<#
.SYNOPSIS
在 Windows 资源管理器右键菜单注册 SceneMarshal 菜单(当前用户,无需管理员)。
.DESCRIPTION
默认注册成「SceneMarshal ▸」级联子菜单,子项:
1. 整理入库 → auto(预览确认 → 整理 → 单目录自动归档)
2. 归档入库 → archive(交互选分类)
3. 综艺整理 → variety(排集 → 特典放置)
4. 补外挂字幕 → extract-subs
5. 兼容性校验 → check(只读,出报告)
级联写法与 Windows 自带项(UpdateEncryptionSettingsWork)一致:
父键上写 ExtendedSubCommandsKey = 该键自身的 HKCR 相对路径,子项放在父键的 Shell\ 下。
注册位置(每处一个父菜单):
HKCU\Software\Classes\Directory\shell\SceneMarshal (文件夹右键)
HKCU\Software\Classes\Directory\Background\shell\SceneMarshal (文件夹空白处右键)
HKCU\Software\Classes\SystemFileAssociations\.mkv\shell\SceneMarshal
... 同上 .mp4 / .avi(文件右键,自动回溯到剧集根目录)
.PARAMETER Flat
兜底模式:不级联,直接注册 5 个平级菜单项(名称带「SceneMarshal ▸ 」前缀)。
.PARAMETER Verify
只读自检:校验 5 处父键、自引用、子项文案、图标、命令里的 wscript/vbs/pwsh 是否存在。
不写注册表;退出码 0 全绿,1 有问题。
.PARAMETER Repair
等价于重新安装(先清理再注册)。
.EXAMPLE
.\install-explorer-menu.ps1
.EXAMPLE
.\install-explorer-menu.ps1 -Flat
#>
[CmdletBinding()]
param(
[switch]$Flat,
[switch]$Verify,
[switch]$Repair
)
$ErrorActionPreference = 'Stop'
$runner = Join-Path $PSScriptRoot 'explorer-media-rename.ps1'
if (-not (Test-Path -LiteralPath $runner)) { throw "找不到 $runner" }
$enqueueVbs = Join-Path $PSScriptRoot 'scenemarshal-enqueue.vbs'
if (-not (Test-Path -LiteralPath $enqueueVbs)) { throw "找不到 $enqueueVbs" }
$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
}
$repoRoot = Split-Path -Parent $PSScriptRoot
$iconPath = Join-Path $repoRoot 'assets\scenemarshal.ico'
if (-not (Test-Path -LiteralPath $iconPath)) { $iconPath = $pwsh }
$menuName = 'SceneMarshal'
$iconsDir = Join-Path $repoRoot 'assets\menu'
function Get-ItemIcon([string]$name) {
$p = Join-Path $iconsDir $name
if (Test-Path -LiteralPath $p) { return $p }
return $iconPath
}
# 级联子项:ExtendedSubCommandsKey 模式下 Explorer 取 MUIVerb 作为显示名
# Key 决定菜单排序(字典序),改文案不影响顺序;插新项才需要重排 01…NN
$items = @(
@{ Key = '01-rename'; Text = '整理入库'; Action = 'rename'; Icon = (Get-ItemIcon 'rename.ico') },
@{ Key = '02-archive'; Text = '归档入库'; Action = 'archive'; Icon = (Get-ItemIcon 'archive.ico') },
@{ Key = '03-variety'; Text = '综艺整理'; Action = 'variety'; Icon = (Get-ItemIcon 'variety.ico') },
@{ Key = '04-extract-subs'; Text = '补外挂字幕'; Action = 'extract-subs'; Icon = (Get-ItemIcon 'subtitle.ico') },
@{ Key = '05-check'; Text = '兼容性校验'; Action = 'check'; Icon = (Get-ItemIcon 'check.ico') }
)
# Relative = ExtendedSubCommandsKey 的值(相对 HKEY_CLASSES_ROOT),同时用于定位注册表路径
$locations = @(
@{ Relative = 'Directory\shell\SceneMarshal'; Token = '%1' },
@{ Relative = 'Directory\Background\shell\SceneMarshal'; Token = '%V' },
@{ Relative = 'SystemFileAssociations\.mkv\shell\SceneMarshal'; Token = '%1' },
@{ Relative = 'SystemFileAssociations\.mp4\shell\SceneMarshal'; Token = '%1' },
@{ Relative = 'SystemFileAssociations\.avi\shell\SceneMarshal'; Token = '%1' }
)
$vbsPath = $enqueueVbs
$wscriptPath = Join-Path $env:SystemRoot 'System32\wscript.exe'
function New-MenuCommand([string]$Action, [string]$Token) {
# 经 wscript 调 VBS:无控制台窗口,只把任务写进队列,
# 由唯一的 worker 窗口按顺序处理(多选时不会弹多个终端)
'"' + $wscriptPath + '" //nologo "' + $vbsPath + '" ' + $Action + ' "' + $pwsh + '" "' + $Token + '"'
}
function Get-RegPath([string]$relative) {
return "HKCU:\Software\Classes\$relative"
}
# ---------------------------------------------------------------- 自检(只读)
if ($Verify) {
$problems = New-Object System.Collections.Generic.List[string]
if (-not (Test-Path -LiteralPath $wscriptPath)) { $problems.Add("wscript 不存在: $wscriptPath") }
if (-not (Test-Path -LiteralPath $vbsPath)) { $problems.Add("入队 VBS 不存在: $vbsPath") }
if (-not (Test-Path -LiteralPath $pwsh)) { $problems.Add("pwsh 不存在: $pwsh") }
foreach ($loc in $locations) {
$parent = Get-RegPath $loc.Relative
if (-not (Test-Path -LiteralPath $parent)) {
$problems.Add("缺少父键: HKCU\Software\Classes\$($loc.Relative)")
continue
}
$props = Get-ItemProperty -LiteralPath $parent
if ($props.MUIVerb -ne $menuName) {
$problems.Add("$($loc.Relative): MUIVerb 应为「$menuName」,实际「$($props.MUIVerb)」")
}
if ($props.ExtendedSubCommandsKey -ne $loc.Relative) {
$problems.Add("$($loc.Relative): ExtendedSubCommandsKey 不是自引用($($props.ExtendedSubCommandsKey))")
}
if ($props.Icon -and -not (Test-Path -LiteralPath $props.Icon)) {
$problems.Add("$($loc.Relative): 父菜单图标不存在 $($props.Icon)")
}
foreach ($item in $items) {
$child = Join-Path (Join-Path $parent 'Shell') $item.Key
if (-not (Test-Path -LiteralPath $child)) {
$problems.Add("$($loc.Relative): 缺少子项 $($item.Key)")
continue
}
$cp = Get-ItemProperty -LiteralPath $child
if ($cp.MUIVerb -ne $item.Text) {
$problems.Add("$($loc.Relative)\$($item.Key): 文案应为「$($item.Text)」,实际「$($cp.MUIVerb)」")
}
if ($cp.Icon -and -not (Test-Path -LiteralPath $cp.Icon)) {
$problems.Add("$($loc.Relative)\$($item.Key): 图标不存在 $($cp.Icon)")
}
$cmd = (Get-ItemProperty -LiteralPath (Join-Path $child 'command')).'(default)'
if (-not $cmd) {
$problems.Add("$($loc.Relative)\$($item.Key): command 为空")
continue
}
# 仓库移盘 / 换 pwsh 安装后,写死的绝对路径会失效
foreach ($needle in @($wscriptPath, $vbsPath, $pwsh)) {
if ($cmd -notlike "*$needle*") {
$problems.Add("$($loc.Relative)\$($item.Key): command 未包含 $needle")
}
}
if ($cmd -notmatch ('\s' + [regex]::Escape($item.Action) + '\s')) {
$problems.Add("$($loc.Relative)\$($item.Key): command 里的动作不是 $($item.Action)")
}
}
}
if ($problems.Count) {
Write-Host ("✘ 菜单自检发现 {0} 个问题:" -f $problems.Count) -ForegroundColor Red
$problems | ForEach-Object { Write-Host " - $_" -ForegroundColor Red }
Write-Host '修复:.\install-explorer-menu.ps1 -Repair' -ForegroundColor Yellow
exit 1
}
Write-Host '✔ 菜单自检通过:5 处父键 / 自引用 / 子项文案与图标 / 命令路径均正常' -ForegroundColor Green
exit 0
}
if ($Repair) { Write-Host '▶ 重新安装(-Repair)' -ForegroundColor Cyan }
# ---------------------------------------------------------------- 清理旧结构
foreach ($loc in $locations) {
foreach ($name in @($menuName, 'SceneMarshalArchive')) {
$old = Join-Path (Split-Path -Parent (Get-RegPath $loc.Relative)) $name
if (Test-Path -LiteralPath $old) { Remove-Item -LiteralPath $old -Recurse -Force }
}
# 兜底模式的平级项
$shellOf = Split-Path -Parent (Get-RegPath $loc.Relative)
Get-ChildItem -LiteralPath $shellOf -ErrorAction SilentlyContinue |
Where-Object { $_.PSChildName -like 'SceneMarshal-*' } |
ForEach-Object { Remove-Item -LiteralPath $_.PSPath -Recurse -Force }
}
# 早期尝试留下的共享子菜单键
foreach ($sub in @('SceneMarshalMenu', 'SceneMarshalMenuBg')) {
$old = "HKCU:\Software\Classes\$sub"
if (Test-Path -LiteralPath $old) { Remove-Item -LiteralPath $old -Recurse -Force }
}
# ---------------------------------------------------------------- 注册
foreach ($loc in $locations) {
$parent = Get-RegPath $loc.Relative
if (-not (Test-Path -LiteralPath $parent)) { New-Item -Path $parent -Force | Out-Null }
if ($Flat) {
# 兜底:每个位置注册 5 个平级项
foreach ($item in $items) {
$key = Join-Path $parent ("SceneMarshal-{0}" -f $item.Action)
$commandKey = Join-Path $key 'command'
New-Item -Path $commandKey -Force | Out-Null
New-ItemProperty -Path $key -Name 'MUIVerb' -Value ("$menuName ▸ " + $item.Text) -PropertyType String -Force | Out-Null
New-ItemProperty -Path $key -Name 'Icon' -Value $item.Icon -PropertyType String -Force | Out-Null
New-ItemProperty -Path $commandKey -Name '(default)' -Value (New-MenuCommand $item.Action $loc.Token) -PropertyType String -Force | Out-Null
}
continue
}
New-ItemProperty -Path $parent -Name 'MUIVerb' -Value $menuName -PropertyType String -Force | Out-Null
New-ItemProperty -Path $parent -Name 'ExtendedSubCommandsKey' -Value $loc.Relative -PropertyType String -Force | Out-Null
New-ItemProperty -Path $parent -Name 'Icon' -Value $iconPath -PropertyType String -Force | Out-Null
$childrenShell = Join-Path $parent 'Shell'
New-Item -Path $childrenShell -Force | Out-Null
foreach ($item in $items) {
$child = Join-Path $childrenShell $item.Key
$commandKey = Join-Path $child 'command'
New-Item -Path $commandKey -Force | Out-Null
New-ItemProperty -Path $child -Name 'MUIVerb' -Value $item.Text -PropertyType String -Force | Out-Null
New-ItemProperty -Path $child -Name 'Icon' -Value $item.Icon -PropertyType String -Force | Out-Null
New-ItemProperty -Path $commandKey -Name '(default)' -Value (New-MenuCommand $item.Action $loc.Token) -PropertyType String -Force | Out-Null
}
}
if ($Flat) {
Write-Host '✔ 已注册平级菜单项(兜底模式)' -ForegroundColor Green
}
else {
Write-Host '✔ 已注册右键级联菜单(ExtendedSubCommandsKey 自引用)' -ForegroundColor Green
}
Write-Host " $menuName ▸" -ForegroundColor Cyan
foreach ($item in $items) {
Write-Host (" {0}" -f $item.Text) -ForegroundColor DarkGray
}
Write-Host ' 位置:文件夹 / 空白处 / .mkv / .mp4 / .avi' -ForegroundColor DarkGray
Write-Host '提示:若菜单未立即出现,重启一次 explorer.exe 或重新登录。' -ForegroundColor Yellow