Skip to content

Commit

Permalink
Merge pull request #230 from btklab/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
btklab authored Aug 1, 2024
2 parents b670d8d + 6c04f6c commit 796c7c9
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 2 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [unreleased]

- NA
- Added [ClipImageFrom-File] function
- Fixed [clip2file] `-Files` option

## [7.2.3] - 2024-07-29

Expand Down Expand Up @@ -742,6 +743,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[Cast-Integer]: src/Cast-Integer_function.ps1
[Edit-Property]: src/Edit-Property_function.ps1

[ClipImageFrom-File]: src/ClipImageFrom-File_function.ps1


[unreleased]: https://github.com/btklab/posh-mocks/compare/7.2.3..HEAD
[7.2.3]: https://github.com/btklab/posh-mocks/releases/tag/7.2.3
[7.2.2]: https://github.com/btklab/posh-mocks/releases/tag/7.2.2
Expand Down
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ cat README.md `
| Set-Clipboard
```

- [Add-LineBreakEndOfFile][], [Add-LineBreak][], [Add-Quartile][], [Add-Stats][], [Apply-Function][], [Cast-Date][], [Cast-Decimal][], [Cast-Double][], [Cast-Integer][], [ConvImage][], [Decrease-Indent][], [Delete-Field][], [Detect-XrsAnomaly][], [Drop-NA][], [Edit-Function][], [Edit-Property][], [Execute-RMarkdown][], [Execute-TinyTeX][], [Get-AppShortcut][], [Get-ClipboardAlternative][], [Get-DateAlternative][], [Get-Histogram][], [Get-OGP][], [Get-Ticket][], [GetValueFrom-Key][], [Grep-Block][], [GroupBy-Object][], [Inkscape-Converter][], [Invoke-Link][], [Join2-Object][], [Measure-Quartile][], [Measure-Stats][], [Measure-Summary][], [Override-Yaml][], [Plot-BarChart][], [Rename-Normalize][], [Replace-ForEach][], [Replace-NA][], [Select-Field][], [Set-NowTime2Clipboard][], [Shorten-PropertyName][], [Shutdown-ComputerAFM][], [Sleep-ComputerAFM][], [Sort-Block][], [Test-isAsciiLine][], [Transpose-Property][], [Trim-EmptyLine][], [Unique-Object][], [Unzip-Archive][], [addb][], [addl][], [addr][], [addt][], [cat2][], [catcsv][], [chead][], [clip2file][], [clip2hyperlink][], [clip2img][], [clip2normalize][], [clip2push][], [clip2shortcut][], [conv][], [count][], [csv2sqlite][], [csv2txt][], [ctail][], [decil][], [delf][], [dot2gviz][], [filehame][], [fillretu][], [flat][], [flow2pu][], [fpath][], [fval][], [fwatch][], [gantt2pu][], [getfirst][], [getlast][], [grep][], [gyo][], [han][], [head][], [image2md][], [jl][], [json2txt][], [juni][], [keta][], [kinsoku][], [lastyear][], [lcalc2][], [lcalc][], [linkcheck][], [linkextract][], [list2table][], [logi2dot][], [logi2pu][], [man2][], [map2][], [math2tex][], [mdfocus][], [mdgrep][], [mind2dot][], [mind2pu][], [movw][], [nextyear][], [pawk][], [percentile][], [pu2java][], [push2loc][], [pwmake][], [pwsync][], [retu][], [rev2][], [rev][], [say][], [sed-i][], [sed][], [self][], [seq2pu][], [sleepy][], [sm2][], [summary][], [table2md][], [tac][], [tail-f][], [tail][], [tarr][], [tateyoko][], [teatimer][], [tenki][], [tex2pdf][], [thisyear][], [toml2psobject][], [uniq][], [vbStrConv][], [watercss][], [wrap][], [yarr][], [ycalc][], [ysort][], [zen][]
- [Add-LineBreakEndOfFile][], [Add-LineBreak][], [Add-Quartile][], [Add-Stats][], [Apply-Function][], [Cast-Date][], [Cast-Decimal][], [Cast-Double][], [Cast-Integer][], [ClipImageFrom-File][], [ConvImage][], [Decrease-Indent][], [Delete-Field][], [Detect-XrsAnomaly][], [Drop-NA][], [Edit-Function][], [Edit-Property][], [Execute-RMarkdown][], [Execute-TinyTeX][], [Get-AppShortcut][], [Get-ClipboardAlternative][], [Get-DateAlternative][], [Get-Histogram][], [Get-OGP][], [Get-Ticket][], [GetValueFrom-Key][], [Grep-Block][], [GroupBy-Object][], [Inkscape-Converter][], [Invoke-Link][], [Join2-Object][], [Measure-Quartile][], [Measure-Stats][], [Measure-Summary][], [Override-Yaml][], [Plot-BarChart][], [Rename-Normalize][], [Replace-ForEach][], [Replace-NA][], [Select-Field][], [Set-NowTime2Clipboard][], [Shorten-PropertyName][], [Shutdown-ComputerAFM][], [Sleep-ComputerAFM][], [Sort-Block][], [Test-isAsciiLine][], [Transpose-Property][], [Trim-EmptyLine][], [Unique-Object][], [Unzip-Archive][], [addb][], [addl][], [addr][], [addt][], [cat2][], [catcsv][], [chead][], [clip2file][], [clip2hyperlink][], [clip2img][], [clip2normalize][], [clip2push][], [clip2shortcut][], [conv][], [count][], [csv2sqlite][], [csv2txt][], [ctail][], [decil][], [delf][], [dot2gviz][], [filehame][], [fillretu][], [flat][], [flow2pu][], [fpath][], [fval][], [fwatch][], [gantt2pu][], [getfirst][], [getlast][], [grep][], [gyo][], [han][], [head][], [image2md][], [jl][], [json2txt][], [juni][], [keta][], [kinsoku][], [lastyear][], [lcalc2][], [lcalc][], [linkcheck][], [linkextract][], [list2table][], [logi2dot][], [logi2pu][], [man2][], [map2][], [math2tex][], [mdfocus][], [mdgrep][], [mind2dot][], [mind2pu][], [movw][], [nextyear][], [pawk][], [percentile][], [pu2java][], [push2loc][], [pwmake][], [pwsync][], [retu][], [rev2][], [rev][], [say][], [sed-i][], [sed][], [self][], [seq2pu][], [sleepy][], [sm2][], [summary][], [table2md][], [tac][], [tail-f][], [tail][], [tarr][], [tateyoko][], [teatimer][], [tenki][], [tex2pdf][], [thisyear][], [toml2psobject][], [uniq][], [vbStrConv][], [watercss][], [wrap][], [yarr][], [ycalc][], [ysort][], [zen][]

**General**: This repository contains a set of filter commands for pattern matching on amorphous strings. The filter commands expect `UTF-8` + `space-delimited` + `line-oriented string data` (text object) through the pipeline as input. The output is also a string, not a typical PowerShell object. Some functions accept Object-input through pipeline, and some functions are "like PowerShell cmdlets" that output object. The design of the filters is similar to Unix/Linux shells such as Bash.

Expand Down Expand Up @@ -12091,6 +12091,30 @@ Mode LastWriteTime Length Name
-a--- 2023/03/18 22:32 171680 2023-03-18___hoge.png
```

#### [ClipImageFrom-File] (Alias: clipimage) - Clips an image from the specified image file.<a id="clipimage"></a>

[ClipImageFrom-File]: src/ClipImageFrom-File_function.ps1

Reads the specified image file and stores it on the clipboard. The input path for the image file can be one of the following: clipboard, file object via pipeline, or file path via pipeline.

- Usage:
- `man2 clipimage`
- `ClipImageFrom-File [[-Files] <String[]>] [-MSPaint]`

Example:

```powershell
# read from clipped image file
ClipImageFrom-File
# read from file object via pipeline
ls a.png | ClipImageFrom-File
# read from file path via pipeline
(ls a.png).FullName | ClipImageFrom-File
```


#### [clip2normalize] - Text normalizer for japanese on windows <a id="clip2normalize"></a>

[clip2normalize]: src/clip2normalize_function.ps1
Expand Down
1 change: 1 addition & 0 deletions operator.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ if ( $IsWindows ){
. $pwshSrcDir/clip2push_function.ps1
. $pwshSrcDir/clip2hyperlink_function.ps1
. $pwshSrcDir/clip2shortcut_function.ps1
. $pwshSrcDir/ClipImageFrom-File_function.ps1
}
. $pwshSrcDir/Decrease-Indent_function.ps1
. $pwshSrcDir/Unzip-Archive_function.ps1
Expand Down
154 changes: 154 additions & 0 deletions src/ClipImageFrom-File_function.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<#
.SYNOPSIS
ClipImageFrom-File - Clips an image from the specified image file.
Reads the specified image file and stores it on the clipboard.
The input path for the image file can be one of the following:
clipboard, file object via pipeline, or file path via pipeline.
Usage:
# read from clipped image file
ClipImageFrom-File
# read from file object via pipeline
ls a.png | ClipImageFrom-File
# read from file path via pipeline
(ls a.png).FullName | ClipImageFrom-File
.LINK
clip2file, clip2push, clip2shortcut, clip2img, clip2txt, clip2normalize, ClipImageFrom-File
#>
function ClipImageFrom-File {
Param(
[Parameter( Mandatory=$False, ValueFromPipeline=$True, Position=0 )]
[Alias("f")]
[string[]] $Files,

[Parameter(Mandatory=$False)]
[Alias('p')]
[switch] $MSPaint,

[Parameter( Mandatory=$False )]
[Alias("u")]
[switch] $UrlDecode
)
## init filepath array
[string[]] $readLineAry = @()
if ( $input.Count -gt 0 ){
## get file path from pipeline text
[string[]] $readLineAry = $input `
| ForEach-Object {
if ( ($_ -is [System.IO.FileInfo]) -or ($_ -is [System.IO.DirectoryInfo]) ){
## from filesystem object
[string] $oText = $_.FullName
} elseif ( $_ -is [System.IO.FileSystemInfo] ){
## from filesystem object
[string] $oText = $_.FullName
} else {
## from text
[string] $oText = $_
}
Write-Output $oText
}
[string[]] $readLineAry = ForEach ($r in $readLineAry ){
if ( $r -ne '' ){ $r.Replace('"', '') }
}
} elseif ( $Files.Count -gt 0 ){
## get filepath from option
[string[]] $readLineAry = $Files | `
ForEach-Object { (Get-Item -LiteralPath $_).FullName }
} else {
## get filepath from clipboard
if ( $True ){
### get filepath as object
Add-Type -AssemblyName System.Windows.Forms
[string[]] $readLineAry = [Windows.Forms.Clipboard]::GetFileDropList()
}
if ( -not $readLineAry ){
### get filepath as text
[string[]] $readLineAry = Get-Clipboard | `
ForEach-Object { if ($_ -ne '' ) { $_.Replace('"', '')} }
}
}
## test
if ( -not $readLineAry ){
Write-Error "no input file." -ErrorAction Stop
}
## sort file paths
[string[]] $sortedReadLineAry = $readLineAry | Sort-Object
## output text with prefix
[object[]] $obj = foreach ( $f in $sortedReadLineAry ){
if ( ( $f -match '^file:///' ) -or ( $UrlDecode ) ){
[string] $f = [uri]::UnEscapeDataString( $($f -replace '^file:///', '') )
}
Get-Item -LiteralPath $f
}
# main
## load assembly
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
[String[]] $imagePaths = $obj | ForEach-Object { $_.FullName }
[String] $imagePath = $imagePaths[0]
if (Test-Path -LiteralPath $imagePath) {
$image = [System.Drawing.Image]::FromFile($imagePath)
$bitmap = New-Object System.Drawing.Bitmap $image
$dataObject = New-Object System.Windows.Forms.DataObject
$dataObject.SetData([System.Windows.Forms.DataFormats]::Bitmap, $bitmap)
[System.Windows.Forms.Clipboard]::SetDataObject($dataObject, $true)
Write-Host "Image loaded to clipboard successfully." -ForegroundColor "Green"
Write-Host "Image: $imagePath" -ForegroundColor "Green"
} else {
Write-Host "File not found: $imagePath" -ForegroundColor "Yellow"
}
# invoke clipboard
if ($MSPaint){
if ($IsWindows){
Start-Process -FilePath "${HOME}\AppData\Local\Microsoft\WindowsApps\mspaint.exe"
}
}
return
}
# set alias
[String] $tmpAliasName = "clipimage"
[String] $tmpCmdName = "ClipImageFrom-File"
[String] $tmpCmdPath = Join-Path `
-Path $PSScriptRoot `
-ChildPath $($MyInvocation.MyCommand.Name) `
| Resolve-Path -Relative
if ( $IsWindows ){ $tmpCmdPath = $tmpCmdPath.Replace('\' ,'/') }
# is alias already exists?
if ((Get-Command -Name $tmpAliasName -ErrorAction SilentlyContinue).Count -gt 0){
try {
if ( (Get-Command -Name $tmpAliasName).CommandType -eq "Alias" ){
if ( (Get-Command -Name $tmpAliasName).ReferencedCommand.Name -eq $tmpCmdName ){
Set-Alias -Name $tmpAliasName -Value $tmpCmdName -PassThru `
| ForEach-Object{
Write-Host "$($_.DisplayName)" -ForegroundColor Green
}
} else {
throw
}
} elseif ( "$((Get-Command -Name $tmpAliasName).Name)" -match '\.exe$') {
Set-Alias -Name $tmpAliasName -Value $tmpCmdName -PassThru `
| ForEach-Object{
Write-Host "$($_.DisplayName)" -ForegroundColor Green
}
} else {
throw
}
} catch {
Write-Error "Alias ""$tmpAliasName ($((Get-Command -Name $tmpAliasName).ReferencedCommand.Name))"" is already exists. Change alias needed. Please edit the script at the end of the file: ""$tmpCmdPath""" -ErrorAction Stop
} finally {
Remove-Variable -Name "tmpAliasName" -Force
Remove-Variable -Name "tmpCmdName" -Force
}
} else {
Set-Alias -Name $tmpAliasName -Value $tmpCmdName -PassThru `
| ForEach-Object {
Write-Host "$($_.DisplayName)" -ForegroundColor Green
}
Remove-Variable -Name "tmpAliasName" -Force
Remove-Variable -Name "tmpCmdName" -Force
}
4 changes: 4 additions & 0 deletions src/clip2file_function.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ function clip2file {
[string[]] $readLineAry = ForEach ($r in $readLineAry ){
if ( $r -ne '' ){ $r.Replace('"', '') }
}
} elseif ( $Files.Count -gt 0 ){
## get filepath from option
[string[]] $readLineAry = $Files | `
ForEach-Object { (Get-Item -LiteralPath $_).FullName }
} else {
## get filepath from clipboard
if ( $True ){
Expand Down

0 comments on commit 796c7c9

Please sign in to comment.