I made a command line tool years ago that worked fine up until macOS Monterey. The code below no longer works for a command line tool. It works fine if I use a macOS application. I need a command line tool so I can trigger the executable from Dictation.
(sandbox settings in Xcode is turned off. Also, Privacy > Accessibility is allowed in System Preferences)
var ml = NSEvent.mouseLocation
ml.y = NSHeight(NSScreen.screens[0].frame) - ml.y
let location = CGPoint(x: ml.x, y: ml.y)
let eventDown = CGEvent(mouseEventSource: nil, mouseType: .leftMouseDown, mouseCursorPosition: location, mouseButton: .left)
eventDown?.post(tap: .cghidEventTap)
let eventUp = CGEvent(mouseEventSource: nil, mouseType: .leftMouseUp, mouseCursorPosition: location, mouseButton: .left)
eventUp?.post(tap: .cghidEventTap)