Guest User

Send Selection to R

a guest
Dec 6th, 2010
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Textwrangler script
  2. -- Gets the Selected text from Textwrangler and executes it in R.
  3. -- Works for R.app or for command line R if R has already been started
  4. -- and is in the formost terminal window
  5. --
  6. -- Author: Spencer Bliven
  7.  
  8. global myapp, editor, CurrLine
  9.  
  10. -- Uncomment your favorite program for running R
  11. set myapp to "Terminal"
  12. --set myapp to "iTerm"
  13. --set myapp to "R"
  14. --set myapp to ""
  15.  
  16. set editor to "TextWrangler"
  17.  
  18. set CurrLine to my getCurrentLine()
  19.  
  20. if myapp is equal to "Terminal" then
  21.     tell application "Terminal"
  22.         activate
  23.         set mytab to selected tab of front window
  24.         do script CurrLine in mytab
  25.     end tell
  26. else if myapp is equal to "iTerm" then
  27.     tell application "iTerm"
  28.         activate
  29.         set term to current terminal
  30.         set ses to current session of term
  31.         write ses text CurrLine
  32.     end tell
  33. else if myapp is equal to "R" then
  34.     tell application "R"
  35.         activate
  36.         cmd CurrLine
  37.     end tell
  38. else if myapp is equal to "" then
  39. else
  40.     display dialog "Don't know how to send lines to " & myapp
  41. end if
  42.  
  43. to getCurrentLine() -- gets either the selected text or the line the cursor is on
  44.     tell window 1 of application editor
  45.         set sel to selection
  46.         set SelLength to length of sel
  47.        
  48.         if SelLength > 0 then
  49.             return sel as string
  50.         else
  51.             try
  52.                 set lineNum to startline of sel
  53.             on error
  54.                 -- startline is sometimes missing from sel. Access it by the class name instead
  55.                 set lineNum to «class SLin» of sel
  56.             end try
  57.             set lineTxt to line lineNum as string
  58.             return lineTxt
  59.         end if
  60.     end tell
  61. end getCurrentLine
Advertisement
Add Comment
Please, Sign In to add comment