It is possible to run JavaScript with Python? There are any library that makes this possible?
I need to execute some JavaScript, I know that this is possible with some Java libraries, but I prefer Python.
Can someone give me a clue on this?
Best Regards,
You can check spidermonkey
If you already use PyQt and QWebView in it, displaying custom html, the function evaluateJavaScript of QWebFrame may be useful for you:
# Python
def runJavaScriptText(self, jsText):
jsText = 'hello()' # just to fit javascript example
self.webView.page().currentFrame().evaluateJavaScript(jsText)
// Javascript
function hello() {
alert('hello');
};
Using spidermonkey would give you a tightier integration of your code, but as a workaround, you could make the javascript get run in a browser using Selenium remote-control:
http://seleniumhq.org/projects/remote-control/
(there are ways of doing that without needing a "physical" display for the browser, using VNC servers, for example)
Does it need to be CPython ?
And does the Javascript need a browser client environment ?
If not, you can probably call Rhino from Jython.
( Note also that Jython release is only at 2.5.2 )
Yes you can execute JavaScript from Python. I find the easiest way is through Python's bindings to the webkit library - here is an example. In my experience selenium and spidermonkey are harder to get working.
Related
I am trying to execute some AppleScript from Objective-C using NSAppleScript... however, the code I am trying is the new JavaScript for automation in Yosemite. It does not appear to do anything when it is run, however, normal AppleScript works fine.
[NSApp activateIgnoringOtherApps:YES];
NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource:
#"\n\
iCal = Application(\"Calendar\");\n\
iCal.includeStandardAdditions = true;\n\
iCal.activate();\n\
iCal.displayAlert(\"testing\");\n\
"];
[scriptObject executeAndReturnError: nil];
How can I get this to run?
Thanks
NSAppleScript is hardwired to compile source code as AppleScript. You'd need to use OSAKit instead, which is shoddy and undocumented, but at least allows you to specify which language to use when compiling from source. Alternatively, you could kludge a workaround by piping your source to osacompile -l JavaScript, then loading the resulting compiled .scpt file into NSAppleScript.
However, it's not clear from your example why you're using NSAppleScript. If you want to execute user-supplied scripts you should probably look at NSUserAppleScriptTask: it's even crappier than NSAppleScript, but it's designed to run the script in a subprocess outside your app's sandbox. (NSAppleScript runs scripts in-process, which in a sandboxed app prevents user scripts talking to arbitrary applications.)
OTOH, if you're only using NSAppleScript to run your own code, you'd be much better using Scripting Bridge (which is crappy and broken, but may be sufficient if your needs are modest), or use AppleScript via the AppleScript-ObjC bridge, which allows your ObjC code to use AppleScript-based 'classes' much as if they were native Cocoa classes. Given that AppleScript is the only supported solution that knows how to speak Apple event correctly (JXA's riddled with flaws too), I'd recommend the latter.
I'm working with a tool that only allows for Javascript as the scriptting language. With the script, I need to launch a process. How would I go about this?
The javascript code is running on the client that will launch the process. The javascript interpeter is RhinoJS.
So my question remains:
1. Is there a way that I can call a specific Java class from Rhino [ProcessBuilder]?
or
2. Is there a way to launch an executable from Javascript? [I've tried the UniversalXPConnect route, but it turns out that the version of Rhino I'm using doesn't really worry about permissions]
That was quick [I found the answer right after I asked]:
var pb = new java.lang.ProcessBuilder("notepad.exe", "c:\test");
pb.start();
Basically RhinoJS has a quirk to allow it to directly access Java functionality. So basically once should just launch the process from there.
I've worked some years now in Perl building Web scrapers, and given the problem that spam represents, and how scraping Web pages would turn out to be much more easier to those folks if, for example, Perl's LWP::UserAgent could handle its cup of JavaScript, I'm amazed no one has built a JS engine for it yet.
What am I missing here?
Thanks in advance. Regards.
PS: I'm not a spammer. Just curious.
Would you mean something like JavaScript::SpiderMonkey, a Perl interface to a JavaScript engine used by Mozilla?
WWW::Selenium?
TMTOWTDI.
Another option is WWW::Scripter, with the Javascript or AJAX plugin.
There is also Win32::IE::Mechanize, Mozilla::Mechanize. But the previously mentioned WWW::Selenium is the most DWIW and well-supported if you have access to browsers and can run the Selenium server. Selenium is a Java critter that runs the browser interactions for you. It has IDEs for several browsers and can write code for you—by recording browser actions—in several languages including Perl or you can hand write it. It’s test-centric, where it excels, but there is no reason not to be use it for general automation/scraping.
You can try to install SpiderMonkey and -in your perl program- execute javascript in backticks, and capture the result, just like from any other unix command line tool. Spidermonkey has a command line option for that, similar to perl's -e command line option. The spidermonkey binary is called "js", thus:
/path/to/spidermonkey/bin/js -e "print(10);"
> 10
I think you can also install v8-shell as an alternative engine, but then you must install 'scons' first, which is available on unix-only.
./v8-shell -e 'print("10*10 = " + 10*10)'
I'm looking for a way to write some simple scripts in javascript, like I would in ruby. For example, I might write a script like:
var str = "Hello World";
console.log(str);
And i want to able to call it from my command line like this:
js hello_world.js
Is there some sort of Javascript runtime, that includes a standard library that would allow me to do this style of script development? I know there is node.js, but that is specific to a web server, right?
Node.js has emerged as the clear winner here, at least for me.
http://nodejs.org/
Well, there's JScript.NET:
http://www.devsource.com/c/a/Techniques/JScriptNET-Applications-at-the-Command-Line/1/
As mentioned in this previous question, you might want to check out Rhino, an implementation of Javascript written in Java. Specifically, the page on the JavaScript shell might be of interest.
Hope this helps.
what about mozilla's spidermonkey ?
apt-cache search javascript interpreter
spidermonkey-bin - standalone JavaScript/ECMAScript (ECMA-262) interpreter
libjenkins-htmlunit-core-js-java - Jenkins branch of the HtmlUnit Core JS Interpreter
gnome-js-common - Common modules for GNOME JavaScript interpreters
With Phantom JS you can easily run a javascript from command line and it naively supports DOM, CSS Selector and JSON.
Why do you want to do this in javascript? Why not use a language that is suitable for this type of command line environment such as Perl? Javascript's main purpose is to faciltate client-side browser control.
I am attempting to create an html document parser with Python. I am very familiar with jQuery and I would like to use its traversing functionality to parse these html files and return the data gathered with jQuery back to my Python program.
Is there any way to use javascript scripts through Python? Or is this just a pipe dream?
You might not need to do this. There is a Python module called PyQuery that directly emulates the API for jQuery. It works exactly as you would expect it to in almost every way. Give it a shot!
jQuery itself does not contain an HTML/XML parser at all. It uses the browser to do all its parsing. Thus, even if you figure out how to run Javascript from Python, it won't do you any good.
jQuery doesn't parse HTML - it traverses the DOM. You'd need an entire rendering engine (e.g. WebKit) if you wanted to use jQuery to work on the HTML.
Well from your question it seems you will require python-javascript bridge like
Pyjamas http://pyjs.org/ , PyPy http://codespeak.net/pypy/dist/pypy/doc/ , skulpt http://www.skulpt.org/ . Or my personal favorite PyXPCOM http://pyxpcomext.mozdev.org/ it installs a python backend directly into the firefox browser and using xpi stubs one can make bidirectioal calls ( mind you very complicated )