38 lines
1.2 KiB
PowerShell
38 lines
1.2 KiB
PowerShell
|
function Get-TextEditorPath {
|
|||
|
<#
|
|||
|
.SYNOPSIS
|
|||
|
Get the path to a text editor executable.
|
|||
|
#>
|
|||
|
[CmdletBinding()]
|
|||
|
[OutputType([System.String])]
|
|||
|
Param()
|
|||
|
|
|||
|
$logLead = Get-LogLeadName
|
|||
|
|
|||
|
# Read the default .txt editor from the registry.
|
|||
|
$defaultEditor = (Get-ItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\txtfile\shell\open\command').'(Default)';
|
|||
|
|
|||
|
# Strip off the placeholder %1's after the .exe in the default application string. Ex "c:\Program Files\notepad++\notepad++.exe %1"
|
|||
|
$exeIndex = $defaultEditor.IndexOf(".exe",[StringComparison]::CurrentCultureIgnoreCase);
|
|||
|
if($exeIndex -lt 0)
|
|||
|
{
|
|||
|
throw "$logLead : Default text editor `"$defaultEditor`" is not an executable. What's up with that?"
|
|||
|
}
|
|||
|
$defaultEditor = $defaultEditor.Substring(0, $exeIndex + 4);
|
|||
|
|
|||
|
# If the default text editor is notepad, see if notepad++ exists instead.
|
|||
|
$nppPath = 'C:\Program Files\Notepad++\notepad++.exe'
|
|||
|
if(($defaultEditor -like "*\NOTEPAD.EXE*") -and (Test-Path $nppPath))
|
|||
|
{
|
|||
|
return $nppPath;
|
|||
|
}
|
|||
|
|
|||
|
# Otherwise just use the default. Hopefully it's not notepad.
|
|||
|
if(!(Test-Path $defaultEditor))
|
|||
|
{
|
|||
|
throw "$logLead : The default text editor path is invalid!"
|
|||
|
}
|
|||
|
|
|||
|
return $defaultEditor;
|
|||
|
}
|