OS &程序详细信息
Script Editor.app中的运行问题
因此,从这个链接出发,我试图构建一个启动/结束一个Sidecar连接的AppleScript,并通过GUI脚本编写控制中心(而不是系统首选项,或者包括屏幕镜像菜单栏项)来实现这一点。
我似乎把它的一部分放下了,但实际上无法单击屏幕镜像按钮(或复选框,因为它在AppleScript中是分类的);它什么也不做。下面是我到目前为止掌握的代码:
set deviceName to "iPad"
set sysVer to system version of (system info) as real
tell application "System Events"
tell its application process "ControlCenter"
activate
-- Click the Control Center menu and give it time to draw
click menu bar item "Control Center" of menu bar 1
delay 1
if sysVer ≥ 12.3 then
-- Get the Screen Mirroring "checkbox" and click it
set screenMirroringToggle to (checkbox 1 of window "Control Center" whose title is "Screen Mirroring")
click screenMirroringToggle
-- Do stuff that gets the iPad button to start/end Sidecar
else
-- Do stuff for other versions of macOS Monterey or Big Sur
end if
end tell
end tell我试过的东西
我对AppleScript还不太熟悉,所以我尝试在迭代中构建脚本。我首先尝试在CC中获得所有可能的复选框,如下所示:
if sysVer ≥ 12.3 then
-- Get all checkboxes in the Control Center menu
set ccCheckboxes to title of (every checkbox of window "Control Center")
return ccCheckboxes
end if返回此列表(链接键盘和鼠标不是我要找的--即通用控制,而不是Sidecar):
{"Wi‑Fi", "Focus", "Bluetooth", "AirDrop", "Screen Mirroring", "Link keyboard and mouse", "Airplay Audio"}设置屏幕镜像复选框不会引发任何错误(并且可以返回),但是单击命令不会(似乎)对此做任何事情:
set myToggle to (checkbox 1 of window "Control Center" whose title is "Screen Mirroring")
click myToggle为了确保click命令确实做了一些事情,我通过以下方法尝试使用AirDrop复选框:
set myToggle to (checkbox 1 of window "Control Center" whose title is "AirDrop")
click myToggle这就像预期的那样工作;CC中的AirDrop图标被切换(在“只接触”和“关闭”之间切换)。然后,我继续试着用上面ccCheckboxes中返回的所有其他复选框,除了屏幕镜像以外的所有东西都可以:所有开关(Wi、Focus、蓝牙、AirDrop)在打开和关闭之间切换,其余的(链接键盘和鼠标,Airplay音频)打开它们的第二个窗口。我是做错什么了还是这只是个虫子?
编辑: macOS Ventura (13.0+)
对于将来偶然发现这件事的人来说。
在这个版本上,GUI脚本的破坏似乎比蒙特雷更严重。大多数GUI条目的名称都从title属性移到了它们的description中,所以您必须这样做:
set controlCenterMBI to (menu bar items of menu bar 1 whose description is "Control Center")或者,我建议在Automator中创建一个工作流,并使用Watch Me Do记录您的点击并将其作为快捷方式导入,然后使用CLI命令shortcuts run $SHORTCUT_NAME运行该快捷方式。
发布于 2022-11-11 04:48:33
编辑: Ventura 13.0.1的解决方案
所以看起来很难在文图拉做。正如您所说的,标题现在在描述中,不幸的是,我发现只有“控制中心”本身在菜单上是正确的。“控制中心”下拉窗口中的所有项目都缺少名称、标题和描述,至少从我所能知道的情况来看是这样。所以,在我找到正确的对象之前,基本上是反复试验。我注意到的其他事情与蒙特利不同:
`
tell application "System Events"
tell its application process "ControlCenter"
tell its menu bar 1
-- click on and open Control Center drop down
tell (UI elements whose description is "Control Center")
click
end tell
end tell
-- interact with Control Center window
tell its window "Control Center"
delay 0.5
-- click screen mirroring button
set screenMirroringButton to button 2 of group 1
-- click screenMirroringButton click doesn't work
perform action 1 of screenMirroringButton
end tell
end tell
end tell`
蒙特利
我在12.0.1上找到了一个适用于我的解决方案,不幸的是,我被困在这个版本中,无法在较新的版本上进行测试,因此它可能对您不起作用,但值得一试。
只需将“单击”改为“执行操作2”即可。这是奇怪的,但它的工作(在12.0.1)。
-- Get the Screen Mirroring "checkbox" and click it
set screenMirroringToggle to (checkbox 1 of window "Control Center" whose title is "Screen Mirroring")
perform action 2 of screenMirroringToggle如果你想看我的applescript去这里。我能够选择一个设备在屏幕镜像下拉,这也可以帮助你什么是你想要的。
https://stackoverflow.com/questions/71691357
复制相似问题