dmesg --follow
[ 68692020.000 ] posts.x: Full talk on building a structured query engine for agent context, from schema discovery to self-correcting queries:  |   [ 68692020.000 ] posts.x: When Claude starts a task with no context attached, it behaves like a brand new employee: grepping through the codebase, guessing at what matters…  |   [ 68673720.000 ] posts.x: @davidsenra you will love this!  |   [ 68673600.000 ] posts.x: https://t.co/vhJifvV8y9  |   [ 68673600.000 ] posts.x: This Jay-Z documentary, In 8, is exceptional! Nearly 6 hours of intimate conversation between @sc and the brilliant @RickRubin. It’s rare to get such…  |   [ 68662260.000 ] posts.x: Full talk on turning re-prompted fixes into linters, including the OpenAPI drift fix and the skill-writing whack-a-mole:  |   [ 68662260.000 ] posts.x: Re-prompting an agent to fix the same sloppy pattern over and over isn't a workflow, it's a sign the rule should live in code, not in your head…  |   [ 68650020.000 ] posts.x: ↻ @willcb: me: kinda fucked up how many people are offloading their whole lives to muse or instinct or grokbot or whatever claude: yeah  |   [ 68650020.000 ] posts.x: ↻ @iamdevloper: signs you've become the senior dev: you're the only one who understands the deploy script you're the only one who remembe…  |   [ 68649720.000 ] posts.x: Gooooooooood morning! Brazil Oberon Cerrado. Roasted 110 seconds past first crack. Milk chocolate and almond.  |   [ 68647800.000 ] posts.x: @Saanvi_dhillon “Git expert” is a weird flex, though, isn’t it?  |   [ 68610720.000 ] posts.x: @sujee_dev @dkundel @aiDotEngineer Thanks mate!  |   [ 68600580.000 ] posts.x: Full talk on the Codex app server protocol and what you can build on top of the Codex harness:  |   [ 68600580.000 ] posts.x: A lot of engineers assume building their own agent interface means writing their own agent harness from scratch. @dkundel, Developer Experience Lead…  |  
corey@gallon.me:~/til$

Launching a Web App from Stream Deck

FIGURE 1 ⋅ Launching a Web App from Stream Deck

This is actually much more of a hassle than I’d have expected …

The trick, in my case, is that I have a virtual desktop called Media and, on it, an always open Microsoft Edge browser window also called Media in which I keep the Brain.fm web player open. I want to be able to press a button on the Stream Deck and play or pause Brain.fm from anywhere.

I ended up writing a PowerShell script to accomplish this, using Claude to help. Claude did a relatively terrible job prompting him regularly. The way I ultimately got there quickly was to borrow from Manuel Odendahl’s brilliant suggestions (video and PDF handout) of having the model create a Domain Specific Language (DSL) for the tasks at hand, and then implement specific parts of the DSL.

The resulting utility is available on GitHub here. This was, at first, a hassle but then really effing cool to see how much better the model does with the DSL! h/t Manuel!

<#
.SYNOPSIS
Automates playing Brain.fm in Microsoft Edge on a specific virtual desktop.

.DESCRIPTION
This script navigates to a named virtual desktop, focuses on Microsoft Edge,
switches to the Brain.fm tab, and starts playback. It uses the VirtualDesktop
module and Windows API calls for desktop and window management.

.NOTES
Requires the VirtualDesktop module to be installed.
Feature_Image: ./images/feature_images/20240824_-_Launching_a_Web_App_from_Stream_Deck_3.png

#>

# Import the VirtualDesktop module
# Must be installed as admin by running "Install-Module VirtualDesktop -Scope CurrentUser"
Import-Module VirtualDesktop

Add-Type -AssemblyName System.Windows.Forms

# Add Windows API function declarations for window management
Add-Type @"
using System;
using System.Runtime.InteropServices;

public class User32 {
    [DllImport("user32.dll")]
    public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool SetForegroundWindow(IntPtr hWnd);
}
"@

<#
.SYNOPSIS
Switches to a named virtual desktop.

.PARAMETER name
The name of the virtual desktop to switch to.

.EXAMPLE
Switch-ToNamedDesktop -name "Media"
#>
function Switch-ToNamedDesktop {
    param([string]$name)
    $desktops = Get-DesktopList
    $targetDesktop = $desktops | Where-Object { $_.Name -eq $name }
    if ($targetDesktop) {
        try {
            Switch-Desktop -Desktop $targetDesktop.Number
            Write-Host "Switched to virtual desktop: $name"
        } catch {
            Write-Host "Error switching to desktop: $_"
        }
    } else {
        Write-Host "Virtual desktop '$name' not found."
        Write-Host "Available desktops: $($desktops | ForEach-Object { $_.Name } | Join-String -Separator ', ')"
    }
}

<#
.SYNOPSIS
Sets focus on an application window.

.PARAMETER name
The name of the application to focus on.

.EXAMPLE
Set-ApplicationFocus -name "Edge"
#>
function Set-ApplicationFocus {
    param([string]$name)
    $processes = Get-Process | Where-Object { $_.MainWindowTitle -ne "" }
    Write-Host "Searching for application with name containing '$name'"
    Write-Host "Running processes with non-empty window titles:"
    $processes | ForEach-Object { Write-Host "  - $($_.ProcessName): $($_.MainWindowTitle)" }

    $app = $processes | Where-Object { $_.ProcessName -like "*$name*" -or $_.MainWindowTitle -like "*$name*" } | Select-Object -First 1
    if ($app) {
        [User32]::ShowWindow($app.MainWindowHandle, 9) # 9 = SW_RESTORE
        [User32]::SetForegroundWindow($app.MainWindowHandle)
        Write-Host "Set focus on application: $($app.ProcessName) (Window Title: $($app.MainWindowTitle))"
        return $true
    } else {
        Write-Host "Application with name containing '$name' not found."
        return $false
    }
}

<#
.SYNOPSIS
Switches to a specific tab in Microsoft Edge.

.PARAMETER tabTitle
The title of the tab to switch to.

.EXAMPLE
Switch-EdgeTab -tabTitle "brain.fm"
#>
function Switch-EdgeTab {
    param(
        [string]$tabTitle
    )
    $edge = Get-Process | Where-Object { $_.ProcessName -eq "msedge" -and $_.MainWindowTitle -ne "" } | Select-Object -First 1
    if ($edge) {
        # Activate the Edge window
        [User32]::ShowWindow($edge.MainWindowHandle, 9) # 9 = SW_RESTORE
        [User32]::SetForegroundWindow($edge.MainWindowHandle)
        Write-Host "Activated Edge window: $($edge.MainWindowTitle)"

        # Use correct keyboard shortcut to search tabs
        $wshell = New-Object -ComObject wscript.shell
        $wshell.SendKeys("^+a")  # Ctrl+Shift+A to open tab search
        Start-Sleep -Milliseconds 500  # Wait for search to open
        $wshell.SendKeys($tabTitle)
        Start-Sleep -Milliseconds 500  # Wait for search results
        $wshell.SendKeys("{ENTER}")

        Write-Host "Attempted to switch to tab '$tabTitle' in Edge"
        return $true
    } else {
        Write-Host "No Edge window with a non-empty title found."
        return $false
    }
}

# Main execution
try {
    # Switch to the "Media" virtual desktop
    Switch-ToNamedDesktop -name "Media"

    # Set focus on the Edge browser window
    $focusSuccess = Set-ApplicationFocus -name "Media"

    # Only proceed if focus was successful
    if ($focusSuccess) {
        # Switch to the tab with brain.fm
        $switchSuccess = Switch-EdgeTab -tabTitle "brain.fm"

        if ($switchSuccess) {
            Write-Host "Navigation completed successfully."
            # Wait for 0.5 seconds
            Start-Sleep -Milliseconds 500
            # Send a space key press to start playing Brain.fm
            [System.Windows.Forms.SendKeys]::SendWait(' ')

        } else {
            Write-Host "Navigation partially completed. Failed to switch to the desired tab."
        }
    } else {
        Write-Host "Navigation failed. Could not focus on Microsoft Edge."
    }
} catch {
    Write-Host "An error occurred: $_"
}
corey@gallon.me:~$ tail -f /writing Attach to the stream. An email when I have something worth sending. Replies encouraged!
corey@gallon.me:~$ ls -lt /til ↑2024-08-24 Comments are Failures in Coding
▸2024-08-24 Launching a Web App from Stream Deck ⋅ you are here
↓2024-08-24 Programs Do Things