#Requires -Version 7.0 <# .SYNOPSIS 在 Windows 资源管理器右键菜单注册 SceneMarshal 菜单(当前用户,无需管理员)。 .DESCRIPTION 默认注册成「SceneMarshal ▸」级联子菜单,子项: 1. 重命名(自动) → auto 2. 归档… → media-archive(交互选分类) 3. 补外挂字幕(转码片源) → extract-subs 4. 入库前体检(只读) → 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 兜底模式:不级联,直接注册 4 个平级菜单项(名称带「SceneMarshal ▸ 」前缀)。 .EXAMPLE .\install-explorer-menu.ps1 .EXAMPLE .\install-explorer-menu.ps1 -Flat #> [CmdletBinding()] param( [switch]$Flat ) $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 } $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 作为显示名 $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-extract-subs'; Text = '补外挂字幕'; Action = 'extract-subs'; Icon = (Get-ItemIcon 'subtitle.ico') }, @{ Key = '04-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' } ) function New-MenuCommand([string]$Action, [string]$Token) { '"' + $pwsh + '" -NoLogo -NoProfile -File "' + $runner + '" -Action ' + $Action + ' -Pause "' + $Token + '"' } function Get-RegPath([string]$relative) { return "HKCU:\Software\Classes\$relative" } # ---------------------------------------------------------------- 清理旧结构 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) { # 兜底:每个位置注册 4 个平级项 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