Added function GetScreenshotsFolder()

This commit is contained in:
Markus Fleschutz 2022-12-19 07:57:35 +01:00
parent b4ef446aee
commit 99e3a3538f
2 changed files with 32 additions and 16 deletions

View File

@ -12,16 +12,21 @@
Author: Markus Fleschutz | License: CC0 Author: Markus Fleschutz | License: CC0
#> #>
try { function GetScreenshotsFolder {
if ($IsLinux) { if ($IsLinux) {
$Path = Resolve-Path "$HOME/Pictures/Screenshots" $Path = "$HOME/Pictures"
if (-not(Test-Path "$Path" -pathType container)) { throw "Pictures folder at $Path doesn't exist (yet)" }
if (Test-Path "$Path/Screenshots" -pathType container) { $Path = "$Path/Screenshots" }
} else { } else {
$Path = [Environment]::GetFolderPath('MyPictures') $Path = [Environment]::GetFolderPath('MyPictures')
$Path = "$Path\Screenshots" if (-not(Test-Path "$Path" -pathType container)) { throw "Pictures folder at $Path doesn't exist (yet)" }
if (Test-Path "$Path\Screenshots" -pathType container) { $Path = "$Path\Screenshots" }
} }
if (-not(Test-Path "$Path" -pathType container)) { return $Path
throw "Screenshots folder at 📂$Path doesn't exist (yet)"
} }
try {
$Path = GetScreenshotsFolder
Set-Location "$Path" Set-Location "$Path"
"📂$Path" "📂$Path"
exit 0 # success exit 0 # success

View File

@ -2,19 +2,32 @@
.SYNOPSIS .SYNOPSIS
Saves a single screenshot Saves a single screenshot
.DESCRIPTION .DESCRIPTION
This PowerShell script takes a single screenshot and saves it into a target folder (the user's pictures folder by default). This PowerShell script takes a single screenshot and saves it into a target folder (default is the user's screenshots folder).
.PARAMETER TargetFolder .PARAMETER TargetFolder
Specifies the target folder (the user's pictures folder by default) Specifies the target folder (the user's screenshots folder by default)
.EXAMPLE .EXAMPLE
PS> ./save-screenshot PS> ./save-screenshot
screenshot saved to C:\Users\Markus\Pictures\2021-10-10T14-33-22.png screenshot saved to C:\Users\Markus\Pictures\Screenshots\2021-10-10T14-33-22.png
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
Author: Markus Fleschutz | License: CC0 Author: Markus Fleschutz | License: CC0
#> #>
param([string]$TargetFolder = "$HOME/Pictures") param([string]$TargetFolder = "")
function GetScreenshotsFolder {
if ($IsLinux) {
$Path = "$HOME/Pictures"
if (-not(Test-Path "$Path" -pathType container)) { throw "Pictures folder at $Path doesn't exist (yet)"}
if (Test-Path "$Path/Screenshots" -pathType container) { $Path = "$Path/Screenshots" }
} else {
$Path = [Environment]::GetFolderPath('MyPictures')
if (-not(Test-Path "$Path" -pathType container)) { throw "Pictures folder at $Path doesn't exist (yet)" }
if (Test-Path "$Path\Screenshots" -pathType container) { $Path = "$Path\Screenshots" }
}
return $Path
}
function TakeScreenshot { param([string]$FilePath) function TakeScreenshot { param([string]$FilePath)
Add-Type -Assembly System.Windows.Forms Add-Type -Assembly System.Windows.Forms
@ -28,9 +41,7 @@ function TakeScreenshot { param([string]$FilePath)
} }
try { try {
if (-not(test-path "$TargetFolder" -pathType container)) { if ("$TargetFolder" -eq "") { $TargetFolder = GetScreenshotsFolder }
throw "Target folder at 📂$TargetFolder doesn't exist"
}
$Time = (Get-Date) $Time = (Get-Date)
$Filename = "$($Time.Year)-$($Time.Month)-$($Time.Day)T$($Time.Hour)-$($Time.Minute)-$($Time.Second).png" $Filename = "$($Time.Year)-$($Time.Month)-$($Time.Day)T$($Time.Hour)-$($Time.Minute)-$($Time.Second).png"
$FilePath = (Join-Path $TargetFolder $Filename) $FilePath = (Join-Path $TargetFolder $Filename)