1

This is a Mac specific question. I would like to source a script that I'm editing on Textmate2, e.g. mycode.R, and make it run in iTerm (terminal would do as well). I do not need to start R, I have the top window of iTerm already running it. So in the iTerm tab should appear the line: > source("[path]/mycode.R", chdir = TRUE) What I needs equivalent to what you have on Rstudio with the key combination Cmd + Shift+S. I found this answer How can I send selected text (or a line) in TextMate to R running on Terminal, but this is about sending a line, or echo the entire code, while what I need should be easier. I succeeded in sourcing to R.app using the following code

#!/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'

But if I replace "R.app" by "iTerm", "iTerm2" or "Terminal", the script fails.

millo
  • 55
  • 5

1 Answers1

0

This does what I was looking for. I'm really not proficient with scripting, this is just result of (a lot of) trial and error. I'm sure there exists a more correct or elegant solution. Here is the code.

#!/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)"
millo
  • 55
  • 5