Advertisement
iwishportalwasreal

ConvertToUPPERCASE_clipboard_watcher-01.ps1

May 23rd, 2024 (edited)
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 2.53 KB | Source Code | 0 0
  1. <#
  2. 2024-05-23 Thu. 10:32a.
  3. ConvertToUPPERCASE_clipboard_watcher-01.ps1
  4. https://github.com/EverythingIsCodeCodeCode/public_GitHub_data/blob/main/ConvertToUPPERCASE_clipboard_watcher-01.ps1
  5. https://pastebin.com/fNXhi6S9
  6. Made with ChatGPT.
  7. This PowerShell script will watch the clipboard, convert text in it to UPPERCASE, and place it back in the clipboard so that it can be pasted.
  8. Only tested to work in Windows.  Mac test in VSCode failed.
  9. To make a desktop icon shortcut, modify it to something similar to the line below:
  10. powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\Path\To\Your\Script\UppercaseConverter.ps1"
  11. #>
  12.  
  13. # Function to process clipboard content
  14. function Process-Clipboard {
  15.     $previousClipboardText = ""
  16.  
  17.     while ($true) {
  18.         try {
  19.             # Get the current clipboard content
  20.             $currentClipboardText = Get-ClipboardText
  21.  
  22.             # Check if the clipboard text is not null or empty and has changed
  23.             if (![string]::IsNullOrEmpty($currentClipboardText) -and $currentClipboardText -ne $previousClipboardText) {
  24.                 # Convert the text to uppercase
  25.                 $upperText = $currentClipboardText.ToUpper()
  26.  
  27.                 # Set the new text to the clipboard
  28.                 Set-ClipboardText -Text $upperText
  29.  
  30.                 # Update previous clipboard text to avoid duplicate processing
  31.                 $previousClipboardText = $upperText
  32.  
  33.                 # Print to console for debugging
  34.                 Write-Output "Converted clipboard content to uppercase: $upperText"
  35.             }
  36.  
  37.             # Wait for a short period before checking again
  38.             Start-Sleep -Milliseconds 500
  39.         } catch {
  40.             # Handle any exceptions that might occur (e.g., clipboard being empty)
  41.             Write-Output "Error accessing clipboard: $_"
  42.             Start-Sleep -Milliseconds 500
  43.         }
  44.     }
  45. }
  46.  
  47. # Function to get clipboard text
  48. function Get-ClipboardText {
  49.     try {
  50.         Add-Type -AssemblyName System.Windows.Forms
  51.         [System.Windows.Forms.Clipboard]::GetText()
  52.     } catch {
  53.         Write-Output "Error getting clipboard text: $_"
  54.         return $null
  55.     }
  56. }
  57.  
  58. # Function to set clipboard text
  59. function Set-ClipboardText {
  60.     param (
  61.         [string]$Text
  62.     )
  63.     try {
  64.         Add-Type -AssemblyName System.Windows.Forms
  65.         [System.Windows.Forms.Clipboard]::SetText($Text)
  66.     } catch {
  67.         Write-Output "Error setting clipboard text: $_"
  68.     }
  69. }
  70.  
  71. # Run the clipboard processing function
  72. Process-Clipboard
  73.  
Tags: powershell
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement