这是一个Mac特有的问题。我想在Textmate2上编辑一个脚本,例如mycode.R,并让它在iTerm中运行(终端也可以)。我不需要启动R,我已经在iTerm的顶部窗口运行它了。因此,在iTerm选项卡中应该出现一行:> source("[path]/mycode.R", chdir = TRUE) What I needs等同于您使用组合键Cmd + Shift+S在Rstudio上拥有的内容。我找到了这个答案How can I send selected text (or a line) in TextMate to R running on Terminal,但这是关于发送一行,或者回显整个代码,而我需要的应该更容易。我使用以下代码成功地获取了R.app资源
#!/bin/bash
osascript -e 'tell application "R.app" to activate'
osascript -e "tell application \"R.app\" to cmd \"source(file='"$TM_FILEPATH"',print.eval=TRUE, chdir=TRUE)\"" \
osascript -e 'tell application "TextMate" to activate'但是,如果我将"R.app“替换为"iTerm”、"iTerm2“或"Terminal",脚本将失败。
发布于 2018-08-29 04:12:26
这就是我要找的东西。我真的不精通脚本,这只是(大量)试验和错误的结果。我确信存在一个更正确或更优雅的解决方案。下面是代码。
#!/usr/bin/env bash
[[ -f "${TM_SUPPORT_PATH}/lib/bash_init.sh" ]] && . "${TM_SUPPORT_PATH}/lib/bash_init.sh"
curDir=''
if [[ ${#TM_DIRECTORY} -gt 0 ]]; then
curDir="$TM_DIRECTORY"
fi
osascript \
-e 'on run(theCode)' \
-e 'tell application "iTerm2"' \
-e 'tell current window' \
-e 'tell current tab' \
-e 'tell current session' \
-e 'write text (item 1 of theCode)' \
-e 'end tell' \
-e 'end tell' \
-e 'end tell' \
-e 'end tell' \
-e 'end run' -- "source(\"$TM_FILEPATH\",print.eval=TRUE, chdir=TRUE)"https://stackoverflow.com/questions/51877286
复制相似问题