iTerm from RStudio

I can’t abide OS X’s built-in Terminal.app—too janky by half. So like any reasonable obssessive, I use iTerm2.

Now, RStudio helpfully provides a menu item for opening a shell in the current project’s directory. But it calls Terminal.app, with no option to substitue an alternative.

R-bloggers has a helpful post about this. But it’s from way back in the dark ages (January 2014), and the solution there doesn’t seem to work any more thanks to changes in iTerm.

A little tweaking yields a working variant, though—improved, even. Just back up the script at /Applications/RStudio.app/Contents/MacOS/mac-terminal and edit the original to read:

#!/usr/bin/osascript
on run argv
  set dir to quoted form of (first item of argv)

  if application "iTerm" is running then
  	set wasRunning to true
  else
    set wasRunning to false
  end if

  tell application "iTerm"
    activate

  	if wasRunning then
  		tell current window
  			create tab with default profile
  		end tell
  	end if

  	tell last session of current tab of current window
  		set name to "RStudio Session"
  		write text "cd " & dir & "; clear"
  	end tell
  end tell
end run

I also added a keyboard shortcut, ⌘+T, in RStudio under Tools > Modify Keyboard Shortcuts….

One nagging imperfection remains, though. Is the best we can do to drop ourselves into the project directory really write text "cd" & dir? Watching AppleScript scramble to cd and clear pains me each time—so inelegant.

Ah, well. It’s not as though elegance was the whole point of switching to iTerm in the first place…

 
Back