Baby's First Applescript

From enfascination

Jump to: navigation, search

I wanted to use vim to develop R code. I am on a Mac. R lets you specify an external editor (go from R->vim), but you need Applescript as glue to go the other way, vim->R, and execute code from vim in R. The Applescript below, saved in Applescript Editor, creates a script that can be run from the command line and will execute the first command line argument as a source file. It should be pretty easy to run any other program besides R that allows this kind of thing.

-- in shell "osascript RunR.scpt ~/path/script.r"
-- from vim: ":! osascript ~/bin/RunRb.scpt `pwd`/% "
-- in .vimrc: "cmap runr ! osascript ~/bin/RunRb.scpt `pwd`/% "

on run argv --for command line
	tell application "R"
		activate
		with timeout of 90000 seconds
			cmd "source(" & quoted form of POSIX path of item 1 of argv & ")" --R code
		end timeout
	end tell
end run