引言

今天给PicGo的项目想要增加在WSL下的剪切板图片读取,于是乎构思出如下想法:

WSL -> powershell.exe -> 获取Windows剪切板图片

方法

PicGo-Core的框架下,后半部分已经是现成的了:

# Adapted from https://github.com/octan3/img-clipboard-dump/blob/master/dump-clipboard-png.ps1
param($imagePath)

# https://github.com/PowerShell/PowerShell/issues/7233
# fix the output encoding bug
[console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding

Add-Type -Assembly PresentationCore
function main {
    $img = [Windows.Clipboard]::GetImage()

    if ($img -eq $null) {
        "no image"
        Exit 1
    }

    if (-not $imagePath) {
        "no image"
        Exit 1
    }

    $fcb = new-object Windows.Media.Imaging.FormatConvertedBitmap($img, [Windows.Media.PixelFormats]::Rgb24, $null, 0)
    $stream = [IO.File]::Open($imagePath, "OpenOrCreate")
    $encoder = New-Object Windows.Media.Imaging.PngBitmapEncoder
    $encoder.Frames.Add([Windows.Media.Imaging.BitmapFrame]::Create($fcb)) | out-null
    $encoder.Save($stream) | out-null
    $stream.Dispose() | out-null

    $imagePath
    #xa0fix windows 10 native cmd crash bug when "picgo upload"
    #xa0https://github.com/PicGo/PicGo-Core/issues/32
    Exit 1
}

try {
    # For WIN10
    $file = Get-Clipboard -Format FileDropList
    if ($file -ne $null) {
        Convert-Path $file
        Exit 1
    }
} catch {
    # For WIN7 WIN8 WIN10
    main
}

main

(脚本名称为windows10.ps1)

使用方法是传入要保存到的路径。举例:

powershell.exe -noprofile -noninteractive -nologo -sta -executionpolicy unrestricted -file windows10.ps1

对于WSL下的脚本,我们可以用同样的参数传入,只不过需要对路径动一下手脚。其中用到的关键方法:

  • awkstring进行截取 (注:它的index从1开始,不是0)
  • wslpath -wWSL路径转换为Windows路径
#!/bin/sh
# grab the paths
scriptPath=$(echo $0 | awk '{ print substr( $0, 1, length($0)-6 ) }')"windows10.ps1"
imagePath=$(echo $1 | awk '{ print substr( $0, 1, length($0)-18 ) }')
imageName=$(echo $1 | awk '{ print substr( $0, length($0)-17, length($0) ) }')

# run the powershell script
res=$(powershell.exe -noprofile -noninteractive -nologo -sta -executionpolicy unrestricted -file $(wslpath -w $scriptPath) $(wslpath -w $imagePath)"\\"$imageName)

# note that there is a return symbol in powershell result
noImage=$(echo "no image\r")

# check whether image exists
if [ "$res" = "$noImage" ] ;then
    echo "no image"
else
    echo $1
fi

使用方法:

sh <path-to-script> <path-to-image>
最后修改:2021 年 07 月 29 日 05 : 20 PM
真的不买杯奶茶嘛....qwq