【发布时间】:2025-11-21 10:35:02
【问题描述】:
我被困住了。我有我在 Stata(一个统计包)中编写运行代码的 SublimeText 插件。 准备好文件后,我以前是这样做的:
cmd = """osascript<< END
tell application "stata"
activate
open POSIX file "%s"
end tell
END""" % dofile_path
os.system(cmd)
... dofile_path 是我希望运行的 stata 文件的路径。
不幸的是,随着 Stata 更新发生了一些变化,现在这会打开 Stata 编辑器而不是主包。因此,我尝试使用系统事件和剪贴板重写它。
cmd = """osascript -e "activate application \"StataMP\"" -e "tell app \"System Events\" to set the clipboard to \"do sublime2stata.do\" " -e "tell app \"System Events\" to keystroke \"v\" using {command down}" -e "tell app \"System Events\" to keystroke return" end tell"""
我也尝试过这样的多行脚本......
cmd = """osascript<< END
tell application \"StataMP\"
activate
end tell
tell application \"System Events\"
keystroke \"1\" using {command down}
set the clipboard to \"do %s\"
keystroke \"v\" using {command down}
keystroke return
end tell
say "finished"
END""" % dofile_path
os.system(cmd)
keystroke "1" using {command down} 只是用来确保选择了 stata 的命令窗口。
问题是什么都没有发生!文件生成,脚本运行(因为它说“完成”OK),但没有任何内容粘贴到 stata 命令窗口中。
谁能看到我的错误?
【问题讨论】:
标签: applescript sublimetext2 stata sublimetext3