Skip to content

Commit 9c017f4

Browse files
bazzilicgep13
authored andcommitted
(#1176) Extend Install-ChocolateyFileAssociation
This commit adds additional parameter to the function, with the default value that maintains full backwards compatibility. This allows for greater control over the arguments that are passed through when setting up the file association. This allows for scenarios where you might need to do something like `-o "%1"` when the default is to do something like `"%1" "%*"`. Co-authored-by: bazzilic <[email protected]>
1 parent d31ed02 commit 9c017f4

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/chocolatey.resources/helpers/functions/Install-ChocolateyFileAssociation.ps1

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ The file extension to be associated.
4242
.PARAMETER Executable
4343
The path to the application's executable to be associated.
4444
45+
.PARAMETER Arguments
46+
Specifies how the file name and CLI arguments are passed to the executable.
47+
Default value is `"%1" "%*"`; another example: `-o "%1"`, if executable
48+
requires it. Use `%1`, `%2`, `%3`, etc. to refer to first, second, third,
49+
etc. arguments. `%*` refers to all arguments, `$~n` refers to all arguments
50+
starting with argument n (2 <= n <= 9).
51+
4552
.PARAMETER IgnoredArguments
4653
Allows splatting with arguments that do not apply. Do not use directly.
4754
@@ -57,6 +64,7 @@ Install-ChocolateyFileAssociation ".txt" $sublimeExe
5764
param(
5865
[parameter(Mandatory = $true, Position = 0)][string] $extension,
5966
[parameter(Mandatory = $true, Position = 1)][string] $executable,
67+
[parameter(Mandatory=$false, Position=2)][string] $arguments = '"%1" "%*"',
6068
[parameter(ValueFromRemainingArguments = $true)][Object[]] $ignoredArguments
6169
)
6270

@@ -75,7 +83,7 @@ Install-ChocolateyFileAssociation ".txt" $sublimeExe
7583
$fileType = $fileType.Replace(" ", "_")
7684
$elevated = @"
7785
cmd /c "assoc $extension=$fileType"
78-
cmd /c 'ftype $fileType="$executable" "%1" "%*"'
86+
cmd /c 'ftype $fileType="$executable" $arguments'
7987
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT
8088
Set-ItemProperty -Path "HKCR:\$fileType" -Name "(Default)" -Value "$fileType file" -ErrorAction Stop
8189
"@

0 commit comments

Comments
 (0)