我正在开发一个 Sublime Text 插件(Python),我有一些 shell 脚本是它的一部分。由于 Package Control 为您提供压缩包,因此这些脚本不能作为 zip 文件中的文件运行,因此我将它们存储为字符串并使用 subprocess.Popen 和 bash -c 运行它们。该脚本的 Mac 版本需要 osascript,我无法从字符串运行它。脚本的 osascript 部分如下(一切正常之前):
osascript <<END
tell application "Terminal"
tell window 1
activate
set w to do script "clear; bash -c \"ssh -t $simulator_user@$simulator_host '$token $build_args'; echo; echo Press any key to continue; read -n 1; exit\""
end tell
end tell
END
到目前为止,我已经成功地实现了第一个activate
语句,osascript -e 'tell application "Terminal" to tell window 1 to activate';
但是第二个语句给我带来了很多麻烦,主要是由于所有的引号。我一直在"""triple quote"""
为我的字符串使用 Python 格式。我得到的最远是这样的
script = """"clear; bash -c "ssh -t $simulator_user@$simulator_host '$token $build_args'; echo; echo Press any key to continue; read -n 1; exit"\""""
darwin_osa2 = """osascript -e 'tell application "Terminal" to tell window 1 to set w to do script {}';""".format(script)
但我得到类似“83:87:语法错误:标识符不能在这个“”之后。(-2740)" 和其他同样非特定的错误。有谁知道格式化第二条语句以与 bash -c 一起使用的正确方法(或者更好的是,一种更简单的运行方法)?