[R-SIG-Mac] Applescript window selection question

John Daschbach john.daschbach at pnl.gov
Fri Sep 7 22:33:37 CEST 2007

This is really an Applescript question, but I thought I would ask here.
What I really want is a reliable way to source() in R a buffer in Emacs.

How do you specify a certain window in the R interface using Applescript?

I am trying to use the R gui from Emacs.  The code at the end does this,
some of the time.  

The problem is that "tell window 1" addresses the window with focus in R, so
this only works when the console has focus.  If the Quartz window or the
Help window (or any other) has focus the Applescript fails.  This also
usually, but not always fails after an exception is thrown in the gui (R

The window order in Applescript is the order from front to back.  However, I
have been getting errors trying to get a window from it's name, e.g.

    set nn to (get window whose name contains "Con")

This throws an error.  Even

set wn to (get name of window 1)
set nn to (get window whose name contains wn)

Does not work.



In Emacs: 

(defun R-source-buffer ()
    (let ((rbuf (current-buffer)))
      (save-buffer-to-load-file "/tmp/tmp.R")
      (shell-command "~/Common/Languages/Applescript/App/R01.app&")
      ;;(kill-buffer "*Async Shell Command*"))
      (pop-to-buffer rbuf))))
;;  File name needs to be the same in the Applescript application
(defun save-buffer-to-load-file (fname)
    (let ((ts (buffer-substring-no-properties (point-min) (point-max))))
      (write-region (point-min) (point-max) fname)))

In Applescript:

tell application "R"
    tell application "System Events"
        tell process "R"
            tell window 1
                keystroke "source('/tmp/tmp.R')"
                keystroke return
            end tell
        end tell
    end tell
end tell

More information about the R-SIG-Mac mailing list