use framework "AppKit" use scripting additions on run a tell application "System Events" set p to first application process whose unix id is (item 1 of a as integer) set ownedWindow to window "LM Studio" of p if value of attribute "AXMinimized" of ownedWindow then error "Owned window minimized" if "AXRaise" is not in (name of actions of ownedWindow) then error "Observed Raise unavailable" perform action "AXRaise" of ownedWindow set frontmost of p to true if not frontmost of p then error "Owned System Events foreground unavailable" set focusFinishAt to (current date) + 2 repeat while (current date) < focusFinishAt if (current application\'s NSWorkspace\'s sharedWorkspace()\'s frontmostApplication()\'s processIdentifier() as integer) is (item 1 of a as integer) then exit repeat delay 0.05 end repeat if (current application\'s NSWorkspace\'s sharedWorkspace()\'s frontmostApplication()\'s processIdentifier() as integer) is not (item 1 of a as integer) then error "Owned NSWorkspace foreground unavailable" set n to ownedWindow repeat with i in {1,1,1,1,1,1,1,1,1,1,2,1,2,1,1,7,1,1,2,2} set n to UI element (i as integer) of n end repeat set r to UI element (item 2 of a as integer) of n if role of r is not "AXButton" or enabled of r is not true then error "Owned row unavailable" if value of attribute "AXValue" of UI element 1 of UI element 1 of r is not item 3 of a then error "Canonical row identity changed" perform action "AXPress" of r end tell end run