Sidecar控制中心的开放式屏幕镜像



OS&程序详细信息

  • macOS Monterey(12.3+(:这只适用于12.3或更高版本,因为随着通用控制的引入,激活/停用Sidecar的方法发生了一些变化(Sidecar按钮不是在CC显示模块中,而是移到了屏幕镜像中(
  • 设备:M1 MacBook Pro 13英寸(2020年末(。不确定这是否重要,但认为这会有助于以防万一
  • 应用程序:在内置Script Editor.app中构建和运行

问题

因此,从这个链接开始,我试图构建一个AppleScript来启动/结束Sidecar连接,并通过控制中心的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

为了确保点击命令真的能起到作用,我通过以下方式使用AirDrop复选框进行了尝试:

set myToggle to (checkbox 1 of window "Control Center" whose title is "AirDrop")
click myToggle

这符合预期;CC中的AirDrop图标被切换(在"仅联系人"one_answers"关闭"之间切换(。然后,我继续尝试上面ccCheckboxes中返回的所有其他复选框,所有屏幕镜像都可以工作:所有开关(Wi-Fi、Focus、Bluetooth、AirDrop(都可以在打开和关闭之间切换,其余的(链接键盘和鼠标、Airplay Audio(打开它们的辅助窗口。我是做错了什么,还是这只是一个bug?

编辑: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运行该快捷方式。

编辑:Ventura 13.0.1的解决方案

因此,在文图拉似乎更难做到这一点。正如你所说,标题现在正在描述中,不幸的是,我发现这只适用于";控制中心";在菜单栏上。"控制中心"下拉窗口中的所有项目都缺少名称、标题和描述,至少从我所能知道的情况来看是这样。所以,在我找到正确的物体之前,这基本上是反复试验。我注意到的其他与蒙特雷不同的地方:

  1. 屏幕镜像现在是一个按钮,而不是复选框
  2. 对点击事件执行动作2现在不起作用;动作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

如果你想看我的苹果说明书,就到这儿去。我可以在"屏幕镜像"下拉列表中选择一个设备,这也可以帮助您实现所需功能。

已解决:通过快捷方式从iPad启动Sidecar并"通过ssh运行脚本">

我已经解决并记录了问题和解决方案!应该在macosVentura&支持苹果Sidecar&快捷方式应用程序。

只需从我的gitHub repo获取脚本:gitHub.com/ManuelTDev/SideCar_via_ssh

文档也作为自述文件附加!

最新更新