Created
November 22, 2023 06:50
-
-
Save kiennq/aa12ddc05023e590e327c8e0de2df82e to your computer and use it in GitHub Desktop.
Get Clipboard content in via WinRT API as powershell script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add-Type -AssemblyName System.Runtime.WindowsRuntime | |
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0] | |
function Await($WinRtTask, $ResultType) { | |
$asTask = $asTaskGeneric.MakeGenericMethod($ResultType) | |
$netTask = $asTask.Invoke($null, @($WinRtTask)) | |
$netTask.Wait(-1) | Out-Null | |
$netTask.Result | |
} | |
$null = [Windows.ApplicationModel.DataTransfer.Clipboard, Windows.ApplicationModel.DataTransfer, ContentType=WindowsRuntime] | |
$content = [Windows.ApplicationModel.DataTransfer.Clipboard]::GetContent() | |
$formats = $content.AvailableFormats; | |
$formats | ForEach-Object -Process { | |
echo $_ | |
Await ($content.GetTextAsync($_)) ([String]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment