-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-context-menu.ps1
More file actions
280 lines (235 loc) · 8.98 KB
/
Copy pathinstall-context-menu.ps1
File metadata and controls
280 lines (235 loc) · 8.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#Requires -RunAsAdministrator
<#
.SYNOPSIS
Installs AI tools context menu entries for Windows Explorer using a plugin system.
.DESCRIPTION
This script adds a context menu structure to Windows Explorer.
It dynamically loads tool configurations from the 'tools/' directory.
It also cleans up legacy "CLI Tools" menu entries.
#>
param (
[switch]$Uninstall
)
$ErrorActionPreference = "Stop"
$scriptDir = $PSScriptRoot
$toolsDir = Join-Path $scriptDir "tools"
$rootIconsDir = Join-Path $scriptDir "icons"
# --- Helper Functions ---
function Write-ColorOutput {
param([string]$Message, [string]$Color = "White")
Write-Host $Message -ForegroundColor $Color
}
function Test-RegistryKey {
param([string]$Path)
return (Test-Path -Path $Path)
}
function New-RegistryKeySafe {
param([string]$Path, [switch]$Force)
try {
if (-not (Test-RegistryKey $Path)) {
New-Item -Path $Path -Force:$Force -ErrorAction Stop | Out-Null
return $true
}
return $true # Exists
}
catch {
Write-ColorOutput " [Error] Creating key '$Path': $_" "Red"
return $false
}
}
function Set-RegistryValueSafe {
param(
[string]$Path,
[string]$Name,
[string]$Value,
[string]$PropertyType = "String"
)
try {
# Ensure value is treated as string for paths
$val = "$Value"
Set-ItemProperty -Path $Path -Name $Name -Value $val -Type $PropertyType -ErrorAction Stop
return $true
}
catch {
Write-ColorOutput " [Error] Setting '$Name' in '$Path': $_" "Red"
return $false
}
}
function Remove-LegacyMenu {
Write-ColorOutput "Checking for legacy menus..." "Gray"
$legacyPaths = @(
"Registry::HKEY_CURRENT_USER\Software\Classes\Directory\Background\shell\CLITools",
"Registry::HKEY_CURRENT_USER\Software\Classes\Directory\shell\CLITools",
"Registry::HKEY_CLASSES_ROOT\Directory\Background\shell\CLITools",
"Registry::HKEY_CLASSES_ROOT\Directory\shell\CLITools"
)
foreach ($path in $legacyPaths) {
if (Test-Path $path) {
try {
Remove-Item -Path $path -Recurse -Force -ErrorAction Stop
Write-ColorOutput " Removed legacy key: $path" "Green"
} catch {
Write-ColorOutput " [Warning] Failed to remove legacy key '$path': $_" "Yellow"
}
}
}
}
function Test-CommandAvailability {
param([string]$CommandName)
if (-not $CommandName) { return $true } # Skip if no command to check
return (Get-Command $CommandName -ErrorAction SilentlyContinue)
}
function Get-ToolPlugins {
$plugins = @()
if (-not (Test-Path $toolsDir)) {
Write-ColorOutput "Tools directory not found at $toolsDir" "Red"
return $plugins
}
$toolFolders = Get-ChildItem -Path $toolsDir -Directory
foreach ($folder in $toolFolders) {
$configFile = Join-Path $folder.FullName "tool.conf.ps1"
if (Test-Path $configFile) {
try {
# Safe load of hashtable. Requires PowerShell 5.1+
$config = Import-PowerShellDataFile -Path $configFile
if (-not $config) {
throw "Configuration file is empty or invalid."
}
# Resolve Icon Path
if ($config.Icon) {
$localIconPath = Join-Path $folder.FullName $config.Icon
$centralizedIconPath = Join-Path $rootIconsDir $config.Icon
if (Test-Path $localIconPath) {
$config.Icon = $localIconPath
} elseif (Test-Path $centralizedIconPath) {
$config.Icon = $centralizedIconPath
} else {
# Check if it looks like a system icon (dll,index)
if ($config.Icon -notmatch ",") {
# If local file missing, fallback to shell32 generic
$config.Icon = "shell32.dll,29" # Generic shortcut icon
Write-ColorOutput " [Info] Icon file missing for $($config.Name), using default." "DarkGray"
}
}
}
$plugins += $config
}
catch {
Write-ColorOutput " [Warning] Failed to load config for $($folder.Name): $_" "Yellow"
}
}
}
return $plugins
}
function Get-PreferredPowerShell {
if (Get-Command "pwsh" -ErrorAction SilentlyContinue) {
return "pwsh"
}
return "powershell.exe"
}
function Add-ContextMenuItem {
param(
[string]$BasePath,
[hashtable]$Tool
)
$safeName = $Tool.Name -replace '[^a-zA-Z0-9]', ''
$menuKeyPath = "$BasePath\AI_Menu\shell\$safeName"
$commandKeyPath = "$menuKeyPath\command"
# Verify command availability
if ($Tool.Command) {
if (-not (Test-CommandAvailability $Tool.Command)) {
Write-ColorOutput " [Warning] Tool '$($Tool.Command)' not found in PATH. Entry will be created but might not work." "Yellow"
}
}
$ok = New-RegistryKeySafe -Path $menuKeyPath -Force
if (-not $ok) { return $false }
$ok = Set-RegistryValueSafe -Path $menuKeyPath -Name "(Default)" -Value $Tool.Name
if ($Tool.Icon) {
Set-RegistryValueSafe -Path $menuKeyPath -Name "Icon" -Value $Tool.Icon | Out-Null
}
$ok = New-RegistryKeySafe -Path $commandKeyPath -Force
if (-not $ok) { return $false }
# Resolve Shell Command (Auto-detect if configured as 'powershell.exe')
$shellCmd = $Tool.ShellCommand
if ($shellCmd -eq "powershell.exe") {
$shellCmd = Get-PreferredPowerShell
}
$fullCommand = "`"$shellCmd`" $($Tool.Arguments)"
Set-RegistryValueSafe -Path $commandKeyPath -Name "(Default)" -Value $fullCommand | Out-Null
return $true
}
function Remove-ContextMenuItem {
param([string]$BasePath)
$aiMenuPath = "$BasePath\AI_Menu"
if (Test-RegistryKey $aiMenuPath) {
try {
Remove-Item -Path $aiMenuPath -Recurse -Force -ErrorAction Stop
return $true
} catch {
Write-ColorOutput " [Error] Removing key '$aiMenuPath': $_" "Red"
return $false
}
}
return $true
}
# --- Main Execution ---
# Registry paths
$registryPaths = @{
FolderBackground = "Registry::HKEY_CLASSES_ROOT\Directory\Background\shell"
Folder = "Registry::HKEY_CLASSES_ROOT\Directory\shell"
Drive = "Registry::HKEY_CLASSES_ROOT\Drive\shell"
}
# Admin Check
$isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
Write-ColorOutput "ERROR: Run as Administrator required." "Red"
exit 1
}
# Version Check
if (-not (Get-Command Import-PowerShellDataFile -ErrorAction SilentlyContinue)) {
Write-ColorOutput "ERROR: This script requires PowerShell 5.1 or later." "Red"
Write-ColorOutput "The 'Import-PowerShellDataFile' cmdlet is missing. Please upgrade PowerShell." "Yellow"
exit 1
}
Write-ColorOutput "`n=== AI Context Menu Manager ===" "Cyan"
if ($Uninstall) {
Write-ColorOutput "Uninstalling..." "Yellow"
# Remove Legacy
Remove-LegacyMenu
foreach ($pathType in $registryPaths.Keys) {
$basePath = $registryPaths[$pathType]
if (Remove-ContextMenuItem -BasePath $basePath) {
Write-ColorOutput " Removed from $pathType" "Green"
}
}
Write-ColorOutput "Uninstallation Complete.`n" "Cyan"
exit 0
}
# Install Mode
Write-ColorOutput "Scanning for tools in '$toolsDir'..." "Gray"
$plugins = Get-ToolPlugins
Write-ColorOutput "Found $($plugins.Count) tools." "Green"
# Remove Legacy first to clean up
Remove-LegacyMenu
foreach ($pathType in $registryPaths.Keys) {
$basePath = $registryPaths[$pathType]
Write-ColorOutput "`nProcessing: $pathType" "Green"
$aiMenuPath = "$basePath\AI_Menu"
New-RegistryKeySafe -Path $aiMenuPath -Force | Out-Null
Set-RegistryValueSafe -Path $aiMenuPath -Name "MUIVerb" -Value "AI Tools" | Out-Null
Set-RegistryValueSafe -Path $aiMenuPath -Name "SubCommands" -Value "" | Out-Null
# Using the custom main_ai.ico if it exists, otherwise falling back to Star
$mainIconPath = Join-Path $rootIconsDir "main_ai.ico"
if (Test-Path $mainIconPath) {
Set-RegistryValueSafe -Path $aiMenuPath -Name "Icon" -Value $mainIconPath | Out-Null
} else {
Set-RegistryValueSafe -Path $aiMenuPath -Name "Icon" -Value "shell32.dll,43" | Out-Null
}
$shellPath = "$aiMenuPath\shell"
New-RegistryKeySafe -Path $shellPath -Force | Out-Null
foreach ($tool in $plugins) {
Write-ColorOutput " Adding: $($tool.Name)" "Gray"
Add-ContextMenuItem -BasePath $basePath -Tool $tool | Out-Null
}
}
Write-ColorOutput "`nInstallation Complete!" "Cyan"