Posts

Showing posts from December, 2023

Replacing default paste behavior in Windows 11

Clipboard URL Shortener Implementation Guide

Clipboard URL Shortener Implementation Guide

Powershell Script

This PowerShell script checks the clipboard for a YouTube URL. If it finds one, it converts it to a shortened youtu.be URL and updates the clipboard with this new URL.

Add-Type -AssemblyName System.Windows.Forms

$clipboardText = Get-Clipboard

if ($clipboardText -match 'https://(www\.|music\.)?youtube\.com/watch\?v=([a-zA-Z0-9_-]+)') {
    $videoID = $matches[2]
    $shortUrl = "https://youtu.be/$videoID"
    Set-Clipboard -Value $shortUrl  # Update the clipboard with the shortened URL
} else {
    Set-Clipboard -Value $clipboardText  # Retain the original clipboard text if it's not a YouTube URL
}
        

AutoHotkey Script

This AutoHotkey script intercepts the Ctrl+V keystroke to execute the PowerShell script and then perform a paste operation using Shift+Insert.

^v::
{
    RunWait("C:\Users\joe\bin\yurl.ps1", "", "Hide") ;
    Send("+{Insert}")  ; Perform paste operation using Shift+Insert
}
return
        

How It Works

When you press Ctrl+V, the AutoHotkey script runs the PowerShell script. If the clipboard contains a YouTube URL, the PowerShell script converts it to a shortened youtu.be URL and updates the clipboard. The AutoHotkey script then simulates a Shift+Insert keystroke to paste the (potentially modified) clipboard content, replacing the default Windows paste behavior with shortened URLs. To avoid breaking pasting images, simply use shift+insert if you know the content is graphical. Still working around this.