Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function AddFileToClipboard{
- param([string] $FilePath)
- $files = [System.Collections.Specialized.StringCollection]::new()
- $files.Add($FilePath) > $null
- [System.Windows.Clipboard]::SetFileDropList($files)
- }
- function Clipboard{
- param(
- [Parameter(ValueFromPipeline=$true, Mandatory=$false)]
- [AllowNull()]
- $In = $null
- )
- begin{
- $Value = $null
- }
- process{
- if($null -eq $Value){
- $Value = $In
- }elseif($Value -is [System.Collections.ArrayList]){
- $Value.Add($In) > $null
- }else{
- $cache = $Value
- $Value = [System.Collections.ArrayList]::new()
- $Value.Add($cache) > $null
- $Value.Add($In) > $null
- }
- }
- end{
- if ($null -ne $Value) {
- if($isWindows){
- if($Value -is [System.IO.FileSystemInfo]){
- AddFileToClipboard $Value.FullName
- return
- }elseif($Value -is [System.Collections.IEnumerable]){
- $allFiles = $true
- $counter = 0
- $lastFile = $null
- foreach($file in $Value){
- if(-not($file -is [System.IO.FileSystemInfo])){
- $allFiles = $false
- break;
- }else{
- $lastFile = $file
- $counter += 1
- }
- }
- if($counter -eq 1){
- AddFileToClipboard $lastFile.FullName
- return
- }elseif($allFiles){
- $sameParent = $true
- $parentDir = Split-Path -Parent $lastFile.FullName
- foreach($file in $Value){
- $currentParentDir = (Split-Path -Parent $file.FullName)
- if($parentDir -ne $currentParentDir){
- $sameParent = $false
- break;
- }
- }
- if($sameParent){
- AddFileToClipboard $parentDir
- return
- }
- }
- }
- }
- try{
- Set-Clipboard -Value $Value
- }catch{
- Set-Clipboard -Value $Value.ToString()
- }
- return
- }
- $content = Get-Clipboard
- if($content -ne ""){
- return $content
- }
- if(-not $isWindows){
- return ""
- }
- $content = [System.Windows.Clipboard]::GetFileDropList()
- if($null -ne $content){
- return $content | map {fileref $_}
- }
- return ""
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement