Advertisement
Guest User

Untitled

a guest
Oct 8th, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. function AddFileToClipboard{
  2.     param([string] $FilePath)
  3.     $files = [System.Collections.Specialized.StringCollection]::new()
  4.     $files.Add($FilePath) > $null
  5.     [System.Windows.Clipboard]::SetFileDropList($files)
  6. }
  7.  
  8. function Clipboard{
  9.     param(
  10.         [Parameter(ValueFromPipeline=$true, Mandatory=$false)]
  11.         [AllowNull()]
  12.         $In = $null
  13.     )
  14.     begin{
  15.         $Value = $null
  16.     }
  17.     process{
  18.         if($null -eq $Value){
  19.             $Value = $In
  20.         }elseif($Value -is [System.Collections.ArrayList]){
  21.             $Value.Add($In) > $null
  22.         }else{
  23.             $cache = $Value
  24.             $Value = [System.Collections.ArrayList]::new()
  25.             $Value.Add($cache) > $null
  26.             $Value.Add($In) > $null
  27.         }
  28.     }
  29.     end{
  30.         if ($null -ne $Value) {
  31.             if($isWindows){
  32.                 if($Value -is [System.IO.FileSystemInfo]){
  33.                     AddFileToClipboard $Value.FullName
  34.                     return
  35.                 }elseif($Value -is [System.Collections.IEnumerable]){
  36.                     $allFiles = $true
  37.                     $counter = 0
  38.                     $lastFile = $null
  39.                     foreach($file in $Value){
  40.                         if(-not($file -is [System.IO.FileSystemInfo])){
  41.                             $allFiles = $false
  42.                             break;
  43.                         }else{
  44.                             $lastFile = $file
  45.                             $counter += 1
  46.                         }
  47.                     }
  48.                     if($counter -eq 1){
  49.                         AddFileToClipboard $lastFile.FullName
  50.                         return
  51.                     }elseif($allFiles){
  52.                         $sameParent = $true
  53.                         $parentDir = Split-Path -Parent $lastFile.FullName
  54.                         foreach($file in $Value){
  55.                             $currentParentDir = (Split-Path -Parent $file.FullName)
  56.                             if($parentDir -ne $currentParentDir){
  57.                                 $sameParent = $false
  58.                                 break;
  59.                             }
  60.                         }
  61.                         if($sameParent){
  62.                             AddFileToClipboard $parentDir
  63.                             return
  64.                         }
  65.                     }
  66.                 }
  67.             }
  68.             try{
  69.                 Set-Clipboard -Value $Value
  70.             }catch{
  71.                 Set-Clipboard -Value $Value.ToString()
  72.             }
  73.             return
  74.         }
  75.         $content = Get-Clipboard
  76.         if($content -ne ""){
  77.             return $content
  78.         }
  79.         if(-not $isWindows){
  80.             return ""
  81.         }
  82.         $content = [System.Windows.Clipboard]::GetFileDropList()
  83.         if($null -ne $content){
  84.             return $content | map {fileref $_}
  85.         }
  86.         return ""
  87.     }
  88.    
  89. }
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement