Skip to content

add applescript support - #527

Merged
jjallaire merged 4 commits into
rstudio:masterfrom
randy3k:applescript
Nov 14, 2015
Merged

jjallaire merged 4 commits into
rstudio:masterfrom
randy3k:applescript

Conversation

@randy3k

@randy3k randy3k commented Nov 13, 2015

Copy link
Copy Markdown
Contributor

It is a hack to implement applescript support. Basically it uses MainFrameController evaluateJavaScript to run a piece of javascript which pastes and runs the R code at the console.

The implementation may be hacky, but it works.

untitled

tell application "/Applications/RStudio.app"
    do rscript "a=1"
    do javascript "alert('hi')"
end tell

@jjallaire

Copy link
Copy Markdown
Member

Cool! A few questions:

  1. Do we need to expose the do javascript endpoint or could we just have do rscript (I'd be more comfortable with not allowing javascript)
  2. Can you do "tell application RStudio" or do you need the whole path (i.e. /Applications/RStudio.app). If just "RStudio" doesn't work is there a straightforward way to make it work?
  3. Does this still work if there are multiple versions of the IDE open?

Once we merge this we can clean up the evaluateJavaScript bit and use a supported API for that.

Also, could you send us a signed contributor agreement? (see https://github.com/rstudio/rstudio/blob/master/CONTRIBUTING.md).

@randy3k

randy3k commented Nov 13, 2015

Copy link
Copy Markdown
Contributor Author
  1. No, we don't need to expose do javascript, it just made my life easier when I was testing the code. I could remove the do javascript bit.
  2. Since I have several "RStudio.app" in different directories and applescript doesn't pick up the correct application from time to time, so I use the full path. tell application "RStudio" will work for most users.
  3. I am afraid that it doesn't. Unless, there is a way for the instances to communicate.

PS: sent you the agreement.

@jjallaire

Copy link
Copy Markdown
Member

So for #3 it just fails entirely? (or does it route to one of the instances)

@jjallaire

Copy link
Copy Markdown
Member

Assuming that do rscript is compatible with the comparable verb for R GUI?

@randy3k

randy3k commented Nov 13, 2015

Copy link
Copy Markdown
Contributor Author

I just tried. It will always trigger the first instance.

R GUI uses cmd instead do rscript. The reason I chose do rscript is because Chrome and Safari both use do javascript to evaluate javascript.

@jjallaire

Copy link
Copy Markdown
Member

I think we should use cmd to be consistent with R GUI (that will allow
users to hook up to RStudio by just changing the application name rather
than changing that plus the verb). Trying to keep things as simple as
possible for users attempting to use both programs. Does that make sense?

On Fri, Nov 13, 2015 at 5:09 PM, Randy Lai [email protected] wrote:

I just tried. It will always open the first instance.

R GUI uses cmd instead do rscript. The reason I chose do rscript is
because Chrome and Safari both use do javascript to evaluate javascript.

—
Reply to this email directly or view it on GitHub
#527 (comment).

@randy3k

randy3k commented Nov 13, 2015

Copy link
Copy Markdown
Contributor Author

True. I have renamed the command as cmd. The following will do

tell application "RStudio"
    cmd "a=1"
end tell

@jjallaire

Copy link
Copy Markdown
Member

Great, thanks! I'll merge this and tweak the evaulateJavascript part over the weekend or early next week.

@randy3k

randy3k commented Nov 14, 2015

Copy link
Copy Markdown
Contributor Author

The original javascript doesn't handle successive cmd's correctly (successive cmd's will fire near at the same time, and because javascript is event-based, the cmd's are evaluated asynchronically). The updated javascript uses a queue and adds a little bit delay between cmd's so that the console has enough time to response.

The old code fails to run this in the expected order:

tell application "RStudio"
    repeat with i from 1 to 10
        cmd i
    end repeat
end tell

Besides, I tried the multiple instance thing again. If multiple instances of RStudio are opened, the R code will be sent to a random instance and it could be the last opened instance, or the first. I have no clue to predict where it will be sent...(R GUI suffers from the same issue.)

@jjallaire

Copy link
Copy Markdown
Member

Thanks! Hopefully when I make the change to call directly into the IDE
internals this will also take care of the problem.

On Sat, Nov 14, 2015 at 1:32 AM, Randy Lai [email protected] wrote:

The original javascript doesn't handle successive cmd's correctly
(successive cmd's will fire near at the same time, and because javascript
is event-based, the cmd's are evaluated asynchronically). The updated
javascript adds a little bit delay between cmd's so that the console has
enough time to response.

The old code fails to run this:

tell application "RStudio"
repeat with i from 1 to 10
cmd "cat(" & i & ")"
end repeatend tell

—
Reply to this email directly or view it on GitHub
#527 (comment).

@jjallaire
jjallaire merged commit 4f5a235 into rstudio:master Nov 14, 2015
@randy3k

randy3k commented Nov 14, 2015

Copy link
Copy Markdown
Contributor Author

Thank you for merging this. But I find that the internals also don't handle successive cmd's correctly. The cmd's are evaluated asychnocially,

screen shot 2015-11-14 at 3 48 04 pm

PS: I cannot find any link to report bugs. The issue page on github is disabled and support.rstudio.com only has feature requests and troubleshooting.

@randy3k
randy3k deleted the applescript branch November 14, 2015 20:54
@jjallaire

Copy link
Copy Markdown
Member

Bugs are reported on support.rstudio.com (apologize that this isn't more
clear).

I'm not able to reproduce the behavior that you are seeing (for me the
numbers always print out 1:10 in order).

The communication from our Cocoa front end to JavaScript is definitely
synchronous (the entire system would fall apart if it wasn't as we have
many blocking calls into JavaScript, even ones that return values). If the
AppleScript execution is asynchronous that would explain what you are
seeing but that seems doubtful.

Are you absolutely positive that you are testing against the right version?
Your change is available in our current Preview Release, could you try with
that installed? (https://www.rstudio.com/products/rstudio/download/preview/)

On Sat, Nov 14, 2015 at 3:52 PM, Randy Lai [email protected] wrote:

Thank you for merging this. But I find that the internals also doesn't
handle successive cmd's correctly. The cmd's are evaluated asychnocially,

[image: screen shot 2015-11-14 at 3 48 04 pm]
https://cloud.githubusercontent.com/assets/1690993/11165762/45ff7bfe-8ae7-11e5-9788-c03b984cce9b.png

PS: I cannot find any link to report bugs. The issue page on github is
disabled and support.rstudio.com only has feature requests and
troubleshooting.

—
Reply to this email directly or view it on GitHub
#527 (comment).

@randy3k

randy3k commented Nov 16, 2015

Copy link
Copy Markdown
Contributor Author

I am absolute sure that I am using the preview. Check this.

screen shot 2015-11-16 at 9 55 23 am

for(i=1;i<10;i++) window.desktopHooks.evaluateRCmd(i.toString());

And it shows that the problem lies in the internal communication rather than the Cocoa interface.

@jjallaire

Copy link
Copy Markdown
Member

Okay, you are indeed correct! I think the problem is out of order delivery of rpc calls to the server (I confirmed that even when they appear out of order they were sent in order). This isn't shocking as there is nothing about http that guarantees in order delivery.

My fix is essentially the same one that you put in (100ms delay executed within a command queue): ac9a422

@jjallaire

Copy link
Copy Markdown
Member

@randy3k I see the following error message in the console when starting up RStudio (in development mode) on OS X Yosemite:

2016-01-07 08:47:51.810 RStudio[1622:36786] A call to OSACopyScriptingDefinition() by
NSScriptSuiteRegistry returned an error. Is this application's OSAScriptingDefinition Info.plist entry
valid? If its value is 'dynamic', is there an 'ascr'/'gsdf' Apple event handler registered?

Is there something else we need to do in Info.plist to prevent this error?

@randy3k

randy3k commented Jan 8, 2016

Copy link
Copy Markdown
Contributor Author

@jjallaire
Actually, I have no idea what it is about I am trying to reproduce this,
Is the development mode the debug release with cmake .. -DCMAKE_BUILD_TYPE=Debug .... ?

@jjallaire

Copy link
Copy Markdown
Member

I'm thinking that this might just be an artifact of running in debug mode
(and therefore without the Info.plist being applied). If I run the binary
at /Applications/RStudio.app/Contents/MacOS/RStudio directly from the
command line I don't see these errors. So I think this is a false alarm,
apologies for raising the flag, just wanted to make sure that nothing in El
Capitan had changed to invalidate what you had done.

On Thu, Jan 7, 2016 at 10:28 PM, Randy Lai [email protected] wrote:

@jjallaire https://github.com/jjallaire
Actually, I have no idea what it is about I am trying to reproduce this,
Is the development mode the debug release with cmake ..
-DCMAKE_BUILD_TYPE=Debug .... ?

—
Reply to this email directly or view it on GitHub
#527 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants