如何通过AppleScript向R控制台发送代码?SendCode插件故障求助
Sublime Text SendCode插件(Issue #150):Big Sur下AppleScript向R控制台发送代码失败的替代方案
我在排查Sublime Text的SendCode插件问题(对应Issue #150)时发现:macOS Big Sur系统上,通过AppleScript向R控制台发送代码时,cmd命令无法正常工作。
基础的R激活代码可以正常运行:
tell application "R" activate end tell
但加入cmd命令发送文本就会报错,而相同的AppleScript在Ventura系统上可正常运行。以下是无需升级系统的解决方案:
用System Events模拟键盘输入
绕开cmd命令限制,通过模拟键盘操作把代码输入到R控制台:tell application "R" activate delay 0.2 -- 给R激活留缓冲时间 end tell tell application "System Events" keystroke "your_code_snippet" -- 替换为目标代码 keystroke return -- 触发执行 end tell若代码较长,建议先复制到剪贴板再模拟粘贴:
set targetCode to "long_code_block_you_want_to_run" set the clipboard to targetCode tell application "R" activate delay 0.2 end tell tell application "System Events" keystroke "v" using command down -- 粘贴代码 keystroke return end tell升级适配Big Sur的R版本
部分旧版R在Big Sur下对AppleScript的cmd命令支持存在缺陷,无需升级系统,直接将R更新到适配Big Sur的最新稳定版,可能直接解决该问题。修改SendCode插件的AppleScript逻辑
找到SendCode插件目录下platforms/osx路径中的AppleScript脚本,将原有的cmd调用逻辑替换为上述模拟键盘输入的代码,修改后重启Sublime Text即可生效。
内容的提问来源于stack exchange,提问作者Fran Villamil
相关产品推荐
相关产品推荐

