Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows support #4

Closed
kylebarron opened this issue Nov 14, 2017 · 13 comments
Closed

Windows support #4

kylebarron opened this issue Nov 14, 2017 · 13 comments

Comments

@kylebarron
Copy link
Owner

The key part of StataEditor is here.

import Pywin32.setup
import win32com.client
import win32api
...
def StataAutomate(stata_command):
	""" Launch Stata (if needed) and send commands """
	try:
		sublime.stata.DoCommandAsync(stata_command)

	except:
		win32api.WinExec(settings.get("stata_path"))
		sublime.stata = win32com.client.Dispatch("stata.StataOLEApp")
		sublime.stata.DoCommand("cd " + getDirectory())
		sublime.stata.DoCommandAsync(stata_command)
@kylebarron
Copy link
Owner Author

Possible solution 1:

The StataEditor package uses the above python packages to create a COM connection to Stata. Instead of reinventing the wheel, try to call that python package from Javascript. Make an ajax request to python:

$.ajax({
  type: "POST",
  url: "~/pythoncode.py",
  data: { param: text}
}).done(function( o ) {
   // do something
});

Note that I think this uses the jQuery package. Here are links to use that with Coffeescript:
Coffeescript Cookbook jQuery and Ajax
jQuery with CoffeeScript

Other javascript-ajax-python links: 1, 2.

Possible solution 2:

Do this directly from javascript/coffeescript.

Calling a VBScript function from Javascript
How can i call VBscript from Javascript
SO example VBScript
What does win32com.client.Dispatch() do?
Quick Start to Client side COM and Python

General References

Stata Automation Reference
OLE Automation in JavaScript

  • Info on:
What Automation is
What binding is
How to create a new instance of an Automation object
How to retrieve an existing instance of an Automation object
How to work with application instances
How to improve the performance of your scripts
How to work with collection objects

OLE Automation Wikipedia
Component Object Model (COM)

Note that Javascript is not necessarily the same as JScript.

@mcaceresb
Copy link

The python code is storing stata.StataOLEApp, and that is why you get a persistent session. The thing to do would be to create a "stata" object in atom in javascript. Not sure whether that is possible, but I don't think you can get a persistent session using python. As best I can tell, it works for sublime because "sublime' is an object in python which can store "stata".

Perhaps this: https://www.npmjs.com/package/win32com? You'd need to rewrite the snippet in js/cson, but the syntax looks pretty similar.

@kylebarron
Copy link
Owner Author

Yeah, I see that stata.StataOLEApp. I've been in the midst of working to install the node module as we speak. The github repo hasn't been updated in 5 years so it might not work. I'll see

@mcaceresb
Copy link

Fingers crossed. FWIW, the hash repo I used for gtools hasn't been updated in 3 years and it works just fine, but I understand it feels a bit like rolling the dice. Best of luck!

@kylebarron
Copy link
Owner Author

Thanks!!! I got it to work.

To document my progress:

  • Had to install node-activex. This is what creates the connection. Ideally, you should be able to do just npm install winax, but that didn't exactly work on my computer.
    image

  • I had to fidget with my environment variables...

    • I have a %PYTHON% variable at C:\Program File\Python36
      image

    • My %PATH% looks like:
      image

  • I also did

    npm --add-python-to-path='true' --debug install --global windows-build-tools

    this had to be done in an administrative level command prompt. See this issue.

Once everything is correctly installed, the following worked:

var winax = require('winax');
var con = new ActiveXObject("stata.StataOLEApp", {
    activate: true,
    async: true,
    type: true
});
con.DoCommandAsync('sysuse auto, clear');

image

To do:

  • Figure out how to get this to install with the fewest number of manual dependencies.
  • Do they need to install python?
  • Get this working inside Atom.

@kylebarron
Copy link
Owner Author

Still trying to get it to work in Atom. It works in the node console, but not in Atom's dev console. Not sure why it can't find the module.

image

@kylebarron
Copy link
Owner Author

Possibly helpful: https://electronjs.org/docs/tutorial/using-native-node-modules

I need to read more about electron

@kylebarron
Copy link
Owner Author

Likely impossible to have this work from Atom. Atom is built on Electron, which is built on Chrome, and Chrome doesn't support ActiveX.

See:

@kylebarron
Copy link
Owner Author

You can still do batch mode:

"C:\Program Files (x86)\Stata15\StataMP-64.exe" /e do test.do

Probably the only possibility

@kylebarron
Copy link
Owner Author

https://atom.io/packages/ide-bash

npm install -g bash-language-server

You need to install that language server separately as it depends on native node modules

look into this and see if there's something like this I could do for Stata

@kylebarron
Copy link
Owner Author

@kylebarron
Copy link
Owner Author

It works!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

From an admin-level command prompt, install windows-build-tools

npm install --global --production windows-build-tools

Then rebuild winax against the current electron version. Get the current electron version with

process.versions.electron

Then rebuild with:

npm rebuild winax --runtime=electron --target=1.6.15 --disturl=https://atom.io/download/atom-shell --build-from-source

@kylebarron
Copy link
Owner Author

Fixed with 1059ae8

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

No branches or pull requests

2 participants