How to Send a Keyboard Shortcut to a Process

I know that System Events can send keys to processes, but it does not work when the process is not frontmost. Maybe it’s possible with ASObjC. ChatGPT created a working swift script for me, but GPT fails to translate it to working AppleScript (I don’t want my Script to be dependent on Xcode Command Line Tools):

#!/usr/bin/env swift
import Cocoa

func sendShortcutToProcess(bundleIdentifier: String, keyCode: CGKeyCode, modifierFlags: CGEventFlags) {
    if let targetProcess = NSRunningApplication.runningApplications(withBundleIdentifier: bundleIdentifier).first {
        let pid = targetProcess.processIdentifier
        guard let eventSource = CGEventSource(stateID: .hidSystemState) else {
            print("Failed to create event source")
            return
        }
        
        let keyDownEvent = CGEvent(keyboardEventSource: eventSource, virtualKey: keyCode, keyDown: true)!
        keyDownEvent.flags = modifierFlags
        keyDownEvent.postToPid(pid)
        
        let keyUpEvent = CGEvent(keyboardEventSource: eventSource, virtualKey: keyCode, keyDown: false)!
        keyUpEvent.flags = modifierFlags
        keyUpEvent.postToPid(pid)
    }
}

// Usage: Call the function with the desired bundle identifier, key code, and modifier flags
sendShortcutToProcess(bundleIdentifier: "com.roon.Roon", keyCode: 126, modifierFlags: .maskCommand)