-
Notifications
You must be signed in to change notification settings - Fork 45
/
SendTo Clipboard as File.ps1
66 lines (52 loc) · 1.73 KB
/
SendTo Clipboard as File.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<#
.SYNOPSIS
Take file name argument and put into clipboard as file
.DESCRIPTION
So can copy path in VS Code, run this script then paste file in Explorer or a path in the clipboard from using "Copy as Path" in Explorer
.NOTES
Modification History:
2024/10/07 @guyrleech Script born (original code from ChatGPT)
#>
[CmdletBinding()]
Param
(
)
[string]$message = $null
Add-Type -AssemblyName PresentationCore , PresentationFramework
if( [System.Windows.Clipboard]::ContainsText() )
{
$files = New-Object -TypeName System.Collections.Specialized.StringCollection
[string]$filePath = $null
$filePath = Get-Clipboard
if( -Not [string]::IsNullOrEmpty( $filePath ))
{
## maybe quoted via copy as path if has spaces
$filePath = $filePath.Trim( ' "''' )
if( Test-Path -Path $filePath )
{
$null = $files.Add( $filePath )
$dataObject = New-Object -TypeName System.Windows.DataObject
$dataObject.SetFileDropList( $files )
Write-Verbose -Message "Putting `"$filePath`" in file drop list"
# Set the DataObject to the clipboard
[System.Windows.Clipboard]::SetDataObject( $dataObject , $true )
}
else
{
$message = "`"$filePath`" - file not found"
}
}
else
{
$message = "No text on clipboard"
}
}
else
{
$message = "Data on clipboard is not text"
}
if( -Not [string]::IsNullOrEmpty( $message ) )
{
[System.Windows.MessageBox]::Show( $message , (( Split-Path -Path (& { $MyInvocation.ScriptName }) -Leaf ) -replace '\.\w+$' ), [System.Windows.MessageBoxButton]::OK , [System.Windows.MessageBoxImage]::Error )
Throw $message
}