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") perform action "AXRaise" of w set frontmost of p to true set e to value of attribute "AXFocusedUIElement" of p if e is not missing value then if role of e is "AXMenu" or role of e is "AXTextField" then error "Existing menu or field; reconcile first" end if 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 1 of n if role of r is not "AXButton" then error "Wrong owned row role" if (value of attribute "AXValue" of UI element 1 of UI element 1 of r) is not "Kingy v20 neutral corpus template (Copy)" then error "Wrong newest owned blank" set g to UI element 2 of r if role of g is not "AXGroup" or (value of attribute "AXHasPopup" of g as text) is not "true" then error "Observed popup mismatch" if description of UI element 1 of g is not "More options" then error "Wrong popup control" perform action "AXPress" of g log "STOCK_POPUP_DISPATCHED" set frontmost of p to true end tell set endAt to (current date) + 2 set menuReady to false repeat while (current date) < endAt tell application "System Events" set menuNow to value of attribute "AXFocusedUIElement" of p if menuNow is not missing value then if role of menuNow is "AXMenu" and name of menuNow is "More options" then set rs to menu items of menuNow whose name is "Rename" set ds to menu items of menuNow whose name is "Duplicate" if (count rs) is 1 and (count ds) is 1 then if enabled of item 1 of rs then set menuReady to true exit repeat end if end if end if end if end tell delay 0.05 end repeat if menuReady is false then error "New stock menu not acknowledged" tell application "System Events" if (value of attribute "AXFocusedUIElement" of p) is not menuNow then error "Stock menu focus changed" click item 1 of rs log "STOCK_RENAME_CLICK_DISPATCHED" set frontmost of p to true end tell set endAt to (current date) + 2 set fieldReady to false repeat while (current date) < endAt tell application "System Events" set fieldNow to value of attribute "AXFocusedUIElement" of p if fieldNow is not missing value then if role of fieldNow is "AXTextField" then if value of fieldNow is "Kingy v20 neutral corpus template (Copy)" then set fieldReady to true exit repeat end if end if end if end tell delay 0.05 end repeat if fieldReady is false then error "New stock expected old-name field not acknowledged" if (current application's NSWorkspace's sharedWorkspace()'s frontmostApplication()'s processIdentifier() as integer) is not 9974 then error "Foreground changed before stock commit" tell application "System Events" if (value of attribute "AXFocusedUIElement" of p) is not fieldNow then error "Stock field changed" set value of fieldNow to "Kingy v20 B3 original 15" log "STOCK_VALUE_EDIT_DISPATCHED" key code 36 log "STOCK_RETURN_COMMIT_DISPATCHED" end tell return "New stock naming operation completed once"