PowerShell/scripts/edit.ps1

52 lines
1.2 KiB
PowerShell
Raw Normal View History

2024-10-01 15:11:03 +02: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-09-28 13:01:48 +02:00
Write-Host "$editor.." -noNewline
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" }
2024-09-28 13:01:48 +02:00
Write-Host "Searching for " -noNewline
TryEditor "neovim" $path
2024-03-18 14:31:48 +01:00
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
}