Skip to content

AppleScript support - #3355

Merged
kevinushey merged 2 commits into
rstudio:masterfrom
randy3k:applescript
Aug 25, 2018
Merged

kevinushey merged 2 commits into
rstudio:masterfrom
randy3k:applescript

Conversation

@randy3k

@randy3k randy3k commented Aug 23, 2018

Copy link
Copy Markdown
Contributor

Not the most beautiful way to do it, but it works.

close #3354

Comment thread src/cpp/desktop/DesktopMainWindow.hpp Outdated
static MainWindow *instance;

public:
static MainWindow *getInstance();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of this. Why not have ScriptCommand.mm accept a MainWindow as part of its constructor? Or if that's not possible for some reason, have a method that it can call to 'bind' to a window instance.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if it is possible to dynamically initialize NSScriptCommand, akaik, it is usually initialized automatically by the application. This implementation just mirrors desktop-mac's:

static MainFrameController* instance_;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the evaluateRScriptCommand class could gain a function e.g. - initialize(MainWindow* window) that gets called by the MainWindow class when appropriate? And that could take care of setting the MainWindow instance so that it can be used.

Comment thread src/cpp/desktop/ScriptCommand.h Outdated
/*
* ScriptCommand.h
*
* Copyright (C) 2009-17 by RStudio, Inc.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: 2009-18

Comment thread src/cpp/desktop/ScriptCommand.mm Outdated
#include <boost/algorithm/string/replace.hpp>


std::string jsEscape(std::string str)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a couple escape helpers here: do any of them suffice?

std::string htmlEscape(const std::string& str, bool isAttributeValue = false);
std::string jsLiteralEscape(const std::string& str);
std::string jsonLiteralEscape(const std::string& str);
std::string jsonLiteralUnescape(const std::string& str);
std::string singleQuotedStrEscape(const std::string& str);

Comment thread src/cpp/desktop/ScriptCommand.mm Outdated
{
rCmd = jsEscape(rCmd);
std::string js = "window.desktopHooks.evaluateRCmd(\"" + rCmd + "\")";
rstudio::desktop::MainWindow::getInstance()->webPage()->runJavaScript(QString::fromUtf8(js.c_str()));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: there is a QString::fromStdString() method that you might consider using here

@randy3k

randy3k commented Aug 23, 2018

Copy link
Copy Markdown
Contributor Author

I have added a class RCommandEvaluator. Let me know how you feel about it.

@kevinushey

Copy link
Copy Markdown
Collaborator

Great!

Just to double-check: does this work as expected when there are multiple RStudio windows open (e.g. if you have multiple main windows; perhaps with different projects open)?

@randy3k

randy3k commented Aug 23, 2018

Copy link
Copy Markdown
Contributor Author

it will always send to the newest instance. In order to support multiple instances, we will need communications between the sessions. I may be able to work on it in the future if you could point me to the correct files to look at.

@randy3k

randy3k commented Aug 23, 2018 •

Copy link
Copy Markdown
Contributor Author

a quick search reveals that a new window created as a separated instance:

void ApplicationLaunch::launchRStudio(const std::vector<std::string>& args,
.
Thus there are no communications between them. We will need a session manager in order to achieve the support of multiple sessions.

@kevinushey

Copy link
Copy Markdown
Collaborator

I think it's okay if we just send to the newest RStudio instance. You might even want to set things up so that commands are sent to the most recently focused RStudio window.

@randy3k

randy3k commented Aug 23, 2018

Copy link
Copy Markdown
Contributor Author

I think it's okay if we just send to the newest RStudio instance. You might even want to set things up so that commands are sent to the most recently focused RStudio window.

@kevinushey unfortunately it is not possible without a session manager. Ideally, there should be only one instance and a new session is opened in the same instance, like how Safari works.

@randy3k

randy3k commented Aug 25, 2018

Copy link
Copy Markdown
Contributor Author

@kevinushey I hope the RStudio team would consider merging this into v1.2 release as AppleScript was already supported v1.1.

@kevinushey

Copy link
Copy Markdown
Collaborator

Yes, we will take this for v1.2. Thanks again for the PR!

@kevinushey
kevinushey merged commit b313801 into rstudio:master Aug 25, 2018
@randy3k
randy3k deleted the applescript branch August 25, 2018 20:02
@randy3k

randy3k commented Aug 25, 2018

Copy link
Copy Markdown
Contributor Author

Awesome.

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.

RStudio no longer supports AppleScript

2 participants