技术问询:使用AppleScript无法点击微软远程桌面的New按钮
解决AppleScript无法点击Microsoft Remote Desktop的“New”按钮问题
我之前也碰到过类似的UI脚本操控坑,下面给你几个实用的排查和解决方向:
先搞定最关键的权限问题
- 打开系统设置 > 隐私与安全性 > 辅助功能
- 确保
Microsoft Remote Desktop和你用来运行脚本的工具(比如Script Editor)都已被勾选——没有这个权限的话,System Events根本没法操控应用的UI元素。
方案1:优化控件定位逻辑(先激活+等待窗口就绪)
有时候脚本跑得太快,窗口还没完全加载,或者控件层级和你用Accessibility Inspector看到的有细微差异,试试这段代码:
tell application "Microsoft Remote Desktop" activate -- 先把应用调到前台,确保窗口处于活跃状态 end tell tell application "System Events" tell process "Microsoft Remote Desktop" -- 等待主窗口加载完成,避免找不到控件 repeat until exists window 1 delay 0.5 end repeat -- 尝试去掉多余的group层级,直接定位toolbar里的New按钮 click button "New" of toolbar 1 of window 1 end tell end tell
方案2:用AXIdentifier精准定位
打开Accessibility Inspector,选中“New”按钮,查看它的AXIdentifier属性(一般是类似com.microsoft.rdc.mac.mainWindow.newButton的字符串),用这个标识符定位比依赖层级更靠谱:
tell application "Microsoft Remote Desktop" activate end tell tell application "System Events" tell process "Microsoft Remote Desktop" repeat until exists window 1 delay 0.5 end repeat click (UI element whose AXIdentifier is "com.microsoft.rdc.mac.mainWindow.newButton") of window 1 end tell end tell
方案3:用快捷键替代UI点击(最稳定的方式)
如果UI定位总是出问题,直接用Microsoft Remote Desktop的内置快捷键⌘N来新建连接,完全不依赖控件结构:
tell application "Microsoft Remote Desktop" activate tell application "System Events" keystroke "n" using command down end tell end tell
额外排查小技巧
- 再用Accessibility Inspector仔细核对控件层级:不同版本的Microsoft Remote Desktop可能调整UI结构,比如“New”按钮可能直接在toolbar下,而不是嵌套在group 1里。
- 确保运行脚本时,Microsoft Remote Desktop的主窗口是可见的,没有被最小化或遮挡。
内容的提问来源于stack exchange,提问作者Mandar
相关产品推荐
相关产品推荐

