-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen.ps1
More file actions
52 lines (41 loc) · 1.66 KB
/
Copy pathopen.ps1
File metadata and controls
52 lines (41 loc) · 1.66 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
param(
[string]$Extension,
[string]$FilePath
)
$destinationDir = "C:\Program Files\oogit"
$assocBackupPath = "$destinationDir\${Extension}_assoc_backup.txt"
$watcherPath = "$destinationDir\watcher.ps1"
$supportedExtensions = @(
"docx", "docm", "dotx", "dotm",
"xlsx", "xlsm", "xltx", "xltm", "xlsb", "xlam",
"pptx", "pptm", "potx", "potm", "ppam", "ppsx", "ppsm",
"sldx", "sldm", "thmx"
)
if (-not ($supportedExtensions -contains $Extension)) {
Write-Error "Incorrect file extension: $Extension"
exit 1
}
if (-not (Test-Path $assocBackupPath)) {
Write-Error "Unsupported file extension: $Extension"
exit 1
}
$originalCommand = Get-Content -Path $assocBackupPath -Raw
$expanded = [Environment]::ExpandEnvironmentVariables($originalCommand)
function Replace-CommandPlaceholders($command, $filePath) {
$fileUrl = '"file:///' + $filePath + '"'
$filePathQuoted = '"' + $filePath + '"'
$command = $command -replace '(["\']?%1["\']?)', $filePathQuoted
$command = $command -replace '(["\']?%l["\']?)', $filePathQuoted
$command = $command -replace '(["\']?%L["\']?)', $filePathQuoted
$command = $command -replace '(["\']?%u["\']?)', $fileUrl
$command = $command -replace '(["\']?%U["\']?)', $fileUrl
$command = $command -replace '(["\']?%\*["\']?)', $filePathQuoted
return $command
}
$finalCommand = Replace-CommandPlaceholders $expanded $FilePath
$process = Start-Process -FilePath "cmd.exe" -ArgumentList "/C $finalCommand" -PassThru
$watcherProcess = Start-Process -FilePath "powershell.exe" -ArgumentList "-ExecutionPolicy Bypass -File \"$watcherPath\" \"$FilePath\"" -PassThru
$process.WaitForExit()
try {
Stop-Process -Id $watcherProcess.Id -Force
}