-- Dynamic owned PID and exact observed template identity. Only two AXPress actions: -- the template's outer popup group, then its sole direct enabled Duplicate item. use framework "AppKit" use scripting additions property visitedCount : 0 property foundRows : {} property foundMenus : {} property expectedName : "" on val(e, a) tell application "System Events" try return value of attribute a of e as text on error return "" end try end tell end val on visitNode(e, depth) if depth > 64 then error "AX inventory depth exceeded" set visitedCount to visitedCount + 1 if visitedCount > 8000 then error "AX inventory bound exceeded" tell application "System Events" set r to role of e if r is "AXMenuBar" then return false if r is "AXMenu" then set end of foundMenus to e set children to UI elements of e end tell repeat with child in children my visitNode(contents of child, depth + 1) end repeat return false end visitNode on inventory(p) set done to false set finishAt to (current date) + 4 repeat while (current date) < finishAt set visitedCount to 0 set foundRows to {} set foundMenus to {} try my visitNode(p, 0) set done to true exit repeat on error errorMessage number errorNumber if errorNumber is not -10000 and errorNumber is not -1719 and errorNumber is not -1728 then error errorMessage number errorNumber end try delay 0.1 end repeat if done is false then error "Complete native inventory unavailable" end inventory on run argv if (count argv) is not 2 then error "Owned PID and fresh action deadline required" set ownedPid to item 1 of argv as integer set actionDeadlineMs to item 2 of argv as real set nowMs to ((current application's NSDate's dateWithTimeIntervalSinceNow:0)'s timeIntervalSince1970() as real) * 1000 if actionDeadlineMs <= nowMs or actionDeadlineMs - nowMs > 60000 then error "Invalid action deadline" tell application "System Events" set p to first application process whose unix id is ownedPid if name of p is not "LM Studio" then error "Wrong native application" set ws to windows of p whose name is "LM Studio" if (count ws) is not 1 then error "Unique owned native window required" set ownedWindow to item 1 of ws if value of attribute "AXMinimized" of ownedWindow is not false then error "Owned native window is minimized" if "AXRaise" is not in (name of actions of ownedWindow) then error "Observed owned Raise unavailable" perform action "AXRaise" of ownedWindow set frontmost of p to true end tell if ((current application's NSWorkspace's sharedWorkspace()'s frontmostApplication()'s processIdentifier()) as integer) is not ownedPid then error "Owned foreground unavailable" set finishAt to (current date) + 8 set settled to false repeat while (current date) < finishAt my inventory(p) if (count foundMenus) > 1 then error "Second native overlay present" if (count foundMenus) is 1 then tell application "System Events" set soleMenu to item 1 of foundMenus set focusedMenu to value of attribute "AXFocusedUIElement" of p if role of soleMenu is "AXMenu" and name of soleMenu is "More options" and focusedMenu is soleMenu then set ds to menu items of soleMenu whose name is "Duplicate" set rs to menu items of soleMenu whose name is "Rename" if (count ds) is 1 and (count rs) is 1 then set duplicateItem to item 1 of ds if enabled of duplicateItem is true and enabled of item 1 of rs is true then set settled to true exit repeat end if end if end if end tell end if delay 0.1 end repeat if settled is false then error "One popup AXPress issued but exact focused More menu not acknowledged; reconcile without retry" tell application "System Events" if (value of attribute "AXFocusedUIElement" of p) is not soleMenu then error "Menu focus changed before Duplicate" if "AXPress" is not in (name of actions of duplicateItem) then error "Duplicate AXPress unavailable" end tell if ((current application's NSWorkspace's sharedWorkspace()'s frontmostApplication()'s processIdentifier()) as integer) is not ownedPid then error "Foreground changed before Duplicate" set nowMs to ((current application's NSDate's dateWithTimeIntervalSinceNow:0)'s timeIntervalSince1970() as real) * 1000 if nowMs >= actionDeadlineMs then error "Prepared native action expired before Duplicate" tell application "System Events" to perform action "AXPress" of duplicateItem return "Exact owned template popup and current sole focused direct Duplicate verified; one Duplicate AXPress issued." end run