用AppleScript点击无效,继续用pyautogui.click()
目标:点击下图中 CheckBox 元素
第一步:获取这个元素的位置,并打印出value,确认是开关是关的(value=0)再继续
set targetbox to checkbox 1 of group 1 of scroll area 1 of scroll area 1 of group 1 of group 1 of sheet 1 of window "Login Items & Extensions"set theValue to value of targetbox
第二步:点击目标元素
1. 用click 事件, 不生效,也无报错
set targetbox to checkbox 1 of group 1 of scroll area 1 of scroll area 1 of group 1 of group 1 of sheet 1 of window "Login Items & Extensions"if targetbox exists thenclick targetbox
2. 用perform action “AXPress”, 不生效,也无报错
perform action "AXPress" of targetbox
3. 用osascript 执行AppleScript,报错10006, 猜测是安全限制问题
do shell script "osascript -e 'tell application \"System Events\" to tell process \"System Extensions (Login Items (System Settings))\" to set value of checkbox \"checkbox 1 of group 1 of scroll area 1 of scroll area 1 of group 1 of group 1 of sheet 1\" of window \"Login Items & Extensions\" to 1'"
4. 直接改value 从0 → 1 , 不生效,也无报错
set value of targetbox to 1
5. 点击元素的坐标,无报错,不生效
trytell checkbox 1 of group 1 of scroll area 1 of scroll area 1 of group 1 of group 1 of sheet 1 of window "Login Items & Extensions"set {xPosition, yPosition} to positionset {xSize, ySize} to sizeend tell-- modify offsets if hot spot is not centered:click at {xPosition + (xSize div 2), yPosition + (ySize div 2)}return {xPosition + (xSize div 2), yPosition + (ySize div 2)}end try
6. 尝试点击其他按钮
点击同层级按钮的坐标,也不行
点击 上上上上层按钮【Done】,可以点也就是说 Done 这层是可以操作的,Done兄弟的下下下下一层就不能操作了
以下是我的猜测:
从UI Browser中看到这个element 不能显示树状结构,且Application 也变成了 “LoginItems” 而非一开始的“Systems Settings”,所以是到了某个加密的元素了。
AppleScript 使用 macOS 提供的 API 来控制和自动化 macOS 应用程序。所以API不会破解macOS的加密元素。
而pyautogui 是跨平台的 GUI 自动化,尤其是基于屏幕坐标的操作,所以在AppleScript获取到元素位置后,再尝试用pyautogui.click()事件。
方法一:写click.py,其中包含pyautogui.click()方法,然后直接在AppleScript中run python click.py
方法二:在python文件中运行 AppleScript(以下方法中还包含根据元素名称点击元素的下一个button)
def enable_macOS15_extension(self,software_name):log.info("enabling %s", software_name)applescript = f"""if major_version is greater than or equal to 15 thendelay 10-- 获取目标窗口set targetWindow to window "{self.get_L10N_text("LoginItems_Extensions")}"-- 获取目标元素的所有兄弟元素(同层次元素)set siblingElements to UI elements of group 3 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of targetWindow-- 初始化变量set targetElementFound to falseset nextElement to missing value-- 遍历所有同层次元素,找到目标元素并获取其下一个元素repeat with i from 1 to count of siblingElementsset currentElement to item i of siblingElementsif value of currentElement contains "{software_name}" thenset targetElementFound to trueelse if targetElementFound thenset nextElement to currentElementexit repeatend ifend repeat-- 点击目标元素的下一个元素的信息if nextElement is not missing value thenclick nextElementdelay 3elsereturn "not found Antivirus Extension button"end if--点击弹窗中的CheckBoxset targetbox to checkbox 1 of group 1 of scroll area 1 of scroll area 1 of group 1 of group 1 of sheet 1 of targetWindowif targetbox exists thenif value of targetbox is 0 thentrytell checkbox 1 of group 1 of scroll area 1 of scroll area 1 of group 1 of group 1 of sheet 1 of targetWindowset {{xPosition, yPosition}} to positionset {{xSize, ySize}} to sizeend tell-- modify offsets if hot spot is not centered:return {{xPosition + (xSize div 2), yPosition + (ySize div 2)}}end tryelsereturn "CheckBox value is 1"end ifelsereturn "Not Found CheckBox"end ifelsereturn "Not macOS15" end if """wrappedscript = self.wrapper(applescript)output = run_applescript(wrappedscript)checkbox_1 = output.split(",")x_position = int(checkbox_1[0])y_position = int(checkbox_1[1])log.info("CheckBox button at %s, %s",x_position, y_position)pyautogui.click(x=x_position,y=y_position)time.sleep(10)