0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PowershellCore でクリップボードの画像をファイルに保存

Last updated at Posted at 2020-01-18

2020/03/13 追記
powershell 7 で [Windows.Forms.Clipboard]::GetImage() が利用可能になったので下記内容は不要になりました。


結論:旧来の Windows PowerShell を使用します。

コード

function Convert-ClipboardImage2File {
    <#
        .SYNOPSIS
        クリップボード内の画像をファイルに保存する
        .PARAMETER basename
        ファイル名(拡張子以外)
        .PARAMETER extension
        拡張子
    #>
    param (
        [string]$basename,
        [string]$extension = ".png"
    )
    if (-not $basename) {
        $t = Get-Date
        $basename = "{0:d4}{1:d2}{2:d2}{3:d2}{4:d2}{5:d2}" -f $t.Year, $t.Month, $t.Date, $t.Hour, $t.Minute, $t.Second
    }
    $fullpath = Join-Path $pwd.Path -ChildPath ($basename + $extension)
    if (Test-Path $fullpath) {
        Write-Host ("'{0}{1}' already exists!" -f $basename, $extension) -ForegroundColor Magenta
        return
    }
    $execute = (powershell.exe "Get-Clipboard -Format Image|Set-Variable img; if(-not `$img){return}; `$img.save('$fullpath'); return 1")
    if (-not $execute){
        Write-Host "ERROR: no image in clipboard..." -ForegroundColor Magenta
        return
    }
    Write-Host "save clipboard image as " -NoNewline
    Write-Host ("'{0}{1}'" -f $basename, $extension) -ForegroundColor Cyan
}

コマンド自体を文字列として powershell の引数に渡し、その標準出力を $execute に格納しています。

クロスプラットフォームになるなどで期待の大きい pwsh ですが、主な用途が文字列加工や検索といった事務作業だった身としてはクリップボード機能が使えなくなったのは痛いですね…。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?