use framework "AppKit" use scripting additions tell application "System Events" set p to first application process whose unix id is 9974 set w to item 1 of (windows of p whose name is "LM Studio") if value of attribute "AXMinimized" of w then error "Owned window minimized" perform action "AXRaise" of w set frontmost of p to true set untilAt to (current date) + 3 repeat while (current date) < untilAt if (current application's NSWorkspace's sharedWorkspace()'s frontmostApplication()'s processIdentifier() as integer) is 9974 then exit repeat delay 0.05 end repeat if (current application's NSWorkspace's sharedWorkspace()'s frontmostApplication()'s processIdentifier() as integer) is not 9974 then error "Actual owned foreground missing" key code 53 log "NEUTRAL_ESCAPE_DISPATCHED" return "Neutral Escape in verified owned foreground; no content input" end tell