首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Textmate: iTerm的源R脚本

Textmate: iTerm的源R脚本
EN

Stack Overflow用户
提问于 2018-08-16 20:37:31
回答 1查看 153关注 0票数 1

这是一个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资源

代码语言:javascript
复制
#!/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",脚本将失败。

EN

回答 1

Stack Overflow用户

发布于 2018-08-29 04:12:26

这就是我要找的东西。我真的不精通脚本,这只是(大量)试验和错误的结果。我确信存在一个更正确或更优雅的解决方案。下面是代码。

代码语言:javascript
复制
#!/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)"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51877286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档