-
Notifications
You must be signed in to change notification settings - Fork 39
/
SetupContextMenu.ps1
189 lines (162 loc) · 7.13 KB
/
SetupContextMenu.ps1
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
Param(
[bool]$uninstall=$false
)
# Global definitions
$wtProfilesPath = "$env:LocalAppData\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json"
$customConfigPath = "$PSScriptRoot\config.json"
$resourcePath = "$env:LOCALAPPDATA\WindowsTerminalContextIcons\"
$contextMenuIcoName = "terminal.ico"
$cmdIcoFileName = "cmd.ico"
$wslIcoFileName = "linux.ico"
$psIcoFileName = "powershell.ico"
$psCoreIcoFileName = "powershell-core.ico"
$azureCoreIcoFileName = "azure.ico"
$unknownIcoFileName = "unknown.ico"
$menuRegID = "WindowsTerminal"
$contextMenuLabel = "Open Windows Terminal here"
$contextMenuRegPath = "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\$menuRegID"
$contextBGMenuRegPath = "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\$menuRegID"
$subMenuRegRelativePath = "Directory\ContextMenus\$menuRegID"
$subMenuRegRoot = "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\ContextMenus\$menuRegID"
$subMenuRegPath = "$subMenuRegRoot\shell\"
function Add-SubmenuReg ($regPath, $label, $iconPath, $command) {
$cmdRegPath = "$regPath\command"
[void](New-Item -Force -Path $regPath)
[void](New-Item -Force -Path $cmdRegPath)
[void](New-ItemProperty -Path $regPath -Name "MUIVerb" -PropertyType String -Value $label)
[void](New-ItemProperty -Path $cmdRegPath -Name "(default)" -PropertyType String -Value $command)
[void](New-ItemProperty -Path $regPath -Name "Icon" -PropertyType String -Value $iconPath)
}
# Clear register
if((Test-Path -Path $contextMenuRegPath)) {
# If reg has existed
Remove-Item -Recurse -Force -Path $contextMenuRegPath
Write-Host "Clear reg $contextMenuRegPath"
}
if((Test-Path -Path $contextBGMenuRegPath)) {
Remove-Item -Recurse -Force -Path $contextBGMenuRegPath
Write-Host "Clear reg $contextBGMenuRegPath"
}
if((Test-Path -Path $subMenuRegRoot)) {
Remove-Item -Recurse -Force -Path $subMenuRegRoot
Write-Host "Clear reg $subMenuRegRoot"
}
if((Test-Path -Path $resourcePath)) {
Remove-Item -Recurse -Force -Path $resourcePath
Write-Host "Clear icon content folder $resourcePath"
}
if($uninstall) {
Exit
}
# Setup icons
[void](New-Item -Path $resourcePath -ItemType Directory)
[void](Copy-Item -Path "$PSScriptRoot\icons\*.ico" -Destination $resourcePath)
Write-Output "Copy icons => $resourcePath"
# Load the custom config
if((Test-Path -Path $customConfigPath)) {
$rawConfig = (Get-Content $customConfigPath -Encoding UTF8) -replace '^\s*\/\/.*' | Out-String
$config = (ConvertFrom-Json -InputObject $rawConfig)
}
# Setup First layer context menu
[void](New-Item -Force -Path $contextMenuRegPath)
[void](New-ItemProperty -Path $contextMenuRegPath -Name ExtendedSubCommandsKey -PropertyType String -Value $subMenuRegRelativePath)
[void](New-ItemProperty -Path $contextMenuRegPath -Name Icon -PropertyType String -Value $resourcePath$contextMenuIcoName)
[void](New-ItemProperty -Path $contextMenuRegPath -Name MUIVerb -PropertyType String -Value $contextMenuLabel)
if($config.global.extended) {
[void](New-ItemProperty -Path $contextMenuRegPath -Name Extended -PropertyType String)
}
Write-Host "Add top layer menu (shell) => $contextMenuRegPath"
[void](New-Item -Force -Path $contextBGMenuRegPath)
[void](New-ItemProperty -Path $contextBGMenuRegPath -Name ExtendedSubCommandsKey -PropertyType String -Value $subMenuRegRelativePath)
[void](New-ItemProperty -Path $contextBGMenuRegPath -Name Icon -PropertyType String -Value $resourcePath$contextMenuIcoName)
[void](New-ItemProperty -Path $contextBGMenuRegPath -Name MUIVerb -PropertyType String -Value $contextMenuLabel)
if($config.global.extended) {
[void](New-ItemProperty -Path $contextBGMenuRegPath -Name Extended -PropertyType String)
}
Write-Host "Add top layer menu (background) => $contextMenuRegPath"
# Get Windows terminal profile
$rawContent = (Get-Content $wtProfilesPath -Encoding UTF8) -replace '^\s*\/\/.*' | Out-String
$json = (ConvertFrom-Json -InputObject $rawContent);
$profiles = $null;
if($json.profiles.list){
Write-Host "Working with the new profiles style"
$profiles = $json.profiles.list;
} else{
Write-Host "Working with the old profiles style"
$profiles = $json.profiles;
}
$profileSortOrder = 0
# Setup each profile item
$profiles | ForEach-Object {
$profileSortOrder += 1
$profileSortOrderString = "{0:00}" -f $profileSortOrder
$profileName = $_.name
$guid = $_.guid
$configEntry = $config.profiles.$guid
$leagaleName = $profileName -replace '[ \r\n\t]', '-'
$subItemRegPath = "$subMenuRegPath$profileSortOrderString$leagaleName"
$subItemAdminRegPath = "$subItemRegPath-Admin"
if ($configEntry.hidden -eq $null) {
$isHidden = $_.hidden
} else {
$isHidden = $configEntry.hidden
}
$commandLine = $_.commandline
$source = $_.source
$icoPath = ""
# Final values
$iconPath_f = ""
$label_f = ""
$labelAdmin_f = ""
$command_f = ""
$commandAdmin_f = ""
if ($isHidden -eq $false) {
# Decide label
if ($configEntry.label) {
$label_f = $configEntry.label
}
else {
$label_f = $profileName
}
$labelAdmin_f = "$label_f (Admin)"
$command_f = "`"$env:LOCALAPPDATA\Microsoft\WindowsApps\wt.exe`" -p `"$profileName`" -d `"%V\.`""
$commandAdmin_f = "powershell -WindowStyle hidden -Command `"Start-Process powershell -WindowStyle hidden -Verb RunAs -ArgumentList `"`"`"`"-Command $env:LOCALAPPDATA\Microsoft\WindowsApps\wt.exe -p '$profileName' -d '%V\.'`"`"`"`""
if($configEntry.icon){
$useFullPath = [System.IO.Path]::IsPathRooted($configEntry.icon);
$tmpIconPath = $configEntry.icon;
$icoPath = If (!$useFullPath) {"$resourcePath$tmpIconPath"} Else { "$tmpIconPath" }
}
elseif ($_.icon) {
$icoPath = $_.icon
}
elseif(($commandLine -match "^cmd\.exe\s?.*")) {
$icoPath = "$cmdIcoFileName"
}
elseif (($commandLine -match "^powershell\.exe\s?.*")) {
$icoPath = "$psIcoFileName"
}
elseif ($source -eq "Windows.Terminal.Wsl") {
$icoPath = "$wslIcoFileName"
}
elseif ($source -eq "Windows.Terminal.PowershellCore") {
$icoPath = "$psCoreIcoFileName"
}
elseif ($source -eq "Windows.Terminal.Azure") {
$icoPath = "$azureCoreIcoFileName"
}else{
# Unhandled Icon
$icoPath = "$unknownIcoFileName"
Write-Host "No icon found, using unknown.ico instead"
}
if($icoPath -ne "") {
$iconPath_f = If ($configEntry.icon -or $_.icon) { "$icoPath" } Else { "$resourcePath$icoPath" }
}
Write-Host "Add new entry $profileName => $subItemRegPath"
Add-SubmenuReg -regPath:$subItemRegPath -label:$label_f -iconPath:$iconPath_f -command:$command_f
if ($configEntry.showRunAs) {
Add-SubmenuReg -regPath:$subItemAdminRegPath -label:$labelAdmin_f -iconPath:$iconPath_f -command:$commandAdmin_f
}
}else{
Write-Host "Skip entry $profileName => $subItemRegPath"
}
}