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 missing value then error "No focused stock menu" if role of e is not "AXMenu" or name of e is not "More options" then error "Wrong current stock menu" set rs to menu items of e whose name is "Rename" if (count rs) is not 1 or enabled of item 1 of rs is not true then error "Unique stock Rename unavailable" click item 1 of rs set frontmost of p to true end tell set untilAt to (current date) + 2 set ready to false repeat while (current date) < untilAt tell application "System Events" set field to value of attribute "AXFocusedUIElement" of p if field is not missing value then if role of field is "AXTextField" then if value of field is "Kingy v20 neutral corpus template (Copy)" then set ready to true exit repeat end if end if end if end tell delay 0.05 end repeat if ready is false then return "Stock Rename clicked once; expected owned field absent; zero edit and Return" if (current application's NSWorkspace's sharedWorkspace()'s frontmostApplication()'s processIdentifier() as integer) is not 9974 then error "Foreground changed before stock name commit" tell application "System Events" if (value of attribute "AXFocusedUIElement" of p) is not field then error "Stock name field focus changed" set value of field to "Kingy v20 B3 original 15" key code 36 end tell return "Stock Rename clicked once; exact owned old-name field edited and Return committed once"