use framework "AppKit" use scripting additions on ownForeground(p, w, wantedPid) tell application "System Events" perform action "AXRaise" of w set frontmost of p to true end tell set focusFinishAt to (current date) + 3 repeat while (current date) < focusFinishAt if (current application's NSWorkspace's sharedWorkspace()'s frontmostApplication()'s processIdentifier() as integer) is wantedPid then exit repeat delay 0.05 end repeat if (current application's NSWorkspace's sharedWorkspace()'s frontmostApplication()'s processIdentifier() as integer) is not wantedPid then error "Owned actual foreground unavailable" end ownForeground on run a set wantedPid to item 1 of a as integer tell application "System Events" set p to first application process whose unix id is wantedPid set w to window "LM Studio" of p if value of attribute "AXMinimized" of w then error "Owned window minimized" end tell my ownForeground(p, w, wantedPid) tell application "System Events" set n to w 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 "Canonical 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 log "STOCK_CANONICAL_ROW_SELECTED" set popup to UI element 2 of r if role of popup is not "AXGroup" or value of attribute "AXHasPopup" of popup is not true then error "Canonical row popup unavailable" if description of UI element 1 of popup is not "More options" then error "Canonical popup identity changed" perform action "AXPress" of popup log "STOCK_CANONICAL_POPUP_DISPATCHED" end tell my ownForeground(p, w, wantedPid) set menuFinishAt to (current date) + 3 set mm to missing value repeat while (current date) < menuFinishAt tell application "System Events" set currentFocus to value of attribute "AXFocusedUIElement" of p if currentFocus is not missing value then if role of currentFocus is "AXMenu" and name of currentFocus is "More options" then set mm to currentFocus exit repeat end if end if end tell delay 0.05 end repeat if mm is missing value then error "Stock popup acknowledged without unique focused More menu; no Duplicate dispatched" tell application "System Events" set ds to menu items of mm whose name is "Duplicate" set rs to menu items of mm whose name is "Rename" if (count ds) is not 1 or (count rs) is not 1 then error "Owned current menu actions ambiguous" if enabled of item 1 of ds is not true or enabled of item 1 of rs is not true then error "Owned menu actions disabled" if (current application's NSWorkspace's sharedWorkspace()'s frontmostApplication()'s processIdentifier() as integer) is not wantedPid then error "Owned foreground changed before stock Duplicate" if (value of attribute "AXFocusedUIElement" of p) is not mm then error "Owned menu changed before stock Duplicate" click item 1 of ds log "STOCK_DUPLICATE_DISPATCHED" end tell return "One stock canonical Duplicate dispatched; optional Rename omitted; zero prompt or Send actions" end run