我被困住了。我有 SublimeText 插件,我在 Stata(一个统计包)中编写了运行代码。 准备好文件后,我曾经这样做:
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)
使用{command down}击键“1”
只是为了确保选择stata的命令窗口。
问题是什么也没发生!文件已生成,脚本运行(因为它显示“完成”OK),但没有将任何内容粘贴到 stata 命令窗口中。
有人能看到我的错误吗?
最佳答案
获得 Stata 13 后,我的 BBEdit 脚本运行 do 文件时遇到了同样的问题。这是现在有效的方法;也许你可以把它改编成ST。诀窍是使用 DoCommand
脚本命令从 Stata 中 do
do 文件。该脚本无需 DoCommand "cd
行即可运行,但该行可确保程序日志和数据文件保存在与 do 文件相同的文件夹中。
tell application "BBEdit"
save text document 1
set loc to file of text document 1 as alias
end tell
tell application "StataMP"
activate
if version < 13 then
open loc
else
tell application "Finder"
set foldr to POSIX path of (folder of file loc as alias)
set ploc to the POSIX path of loc
end tell
DoCommand "cd " & "\"" & foldr & "\""
DoCommand "do " & "\"" & ploc & "\""
end if
end tell
关于applescript - 从 SublimeText 运行 AppleScript 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20053588/