PowerShell/scripts/edit.ps1

51 lines
1.1 KiB
PowerShell
Raw Normal View History

2023-10-31 12:20:46 +01:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2024-03-18 14:31:48 +01:00
Opens a text editor
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2024-03-18 14:31:48 +01:00
This PowerShell script opens a text editor with the given text file.
.PARAMETER path
Specifies the path to the text file (will be queried if none given)
2021-07-13 21:10:02 +02:00
.EXAMPLE
2024-03-18 14:31:48 +01:00
PS> ./edit.ps1 C:\MyDiary.txt
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2022-01-29 12:47:46 +01:00
.NOTES
2022-09-06 21:42:04 +02:00
Author: Markus Fleschutz | License: CC0
#>
2024-03-18 14:31:48 +01:00
param([string]$path = "")
2024-03-18 14:31:48 +01:00
function TryEditor { param([string]$editor, [string]$path)
try {
2024-03-20 16:14:46 +01:00
Write-Host -noNewline "$editor, "
2024-03-18 14:31:48 +01:00
& $editor "$path"
if ($lastExitCode -ne "0") {
"⚠️ Can't execute '$editor' - make sure it's installed and available"
exit 1
}
exit 0 # success
} catch {
return
2021-03-29 19:30:56 +02:00
}
2024-03-18 14:31:48 +01:00
}
try {
if ($path -eq "" ) { $path = Read-Host "Enter the path to the text file" }
Write-Host -noNewline "Trying "
TryEditor "vim" $path
TryEditor "vi" $path
TryEditor "nano" $path
TryEditor "pico" $path
TryEditor "emacs" $path
TryEditor "notepad.exe" $path
TryEditor "wordpad.exe" $path
Write-Host ""
throw "No text editor found - use 'winget install' to install your favorite text editor."
2021-09-27 10:09:45 +02:00
exit 0 # success
} catch {
2022-04-13 12:06:32 +02:00
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}