docsteveharris/stata

File_2_stata.py and Text_2_stata.py

Closed this issue · 0 comments

I have modified these two files as the corresponding commands did not work on my installation.

I have modified file_2_stata:

import os
import sublime_plugin
import sublime
class file_2_stataCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        #  get path to current directory
        self.view.run_command("save")
        filename = self.view.file_name()
        cmd = """osascript -e 'tell application "Stata" to open POSIX file "%s"' &""" % filename
        os.system(cmd)

I have modified text_2_stata:

import os
import sublime_plugin
import sublime
import subprocess
class text_2_stataCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        #  get path to current directory
        filename = self.view.file_name()
        filepath = os.path.dirname(filename)
        print(filepath)
        #  grab the buffer
        #  if nothing selected then send the line
        all_text = ""
        sels = self.view.sel()
        for sel in sels:
            all_text = all_text + self.view.substr(sel)
        if len(all_text) == 0:
            all_text = self.view.substr(self.view.line(sel)) 
        all_text = all_text + "\n"

        #  write the buffer to file in pwd
        dofile_path = os.path.join(filepath, '.sublime2stata.do')
        # print "%r" % dofile_path
        this_file = open(dofile_path,'w')
        this_file.write(all_text)
        this_file.close()

        cmd = """osascript -e 'tell application "Stata" to open POSIX file "%s"' &""" % dofile_path
        os.system(cmd)

There are three modifications:

  • File_2_stata saves the file before sending it to stata
  • The osascript tells Stata to open the do file, instead of telling the finder to open it.
  • The & at the end of the osascript command is necessary for the script to work quickly.

Also, I have created two new files that send the do file in a new instance of stata (which can be helpful for instance when the first instance of stata is busy), by replacing

        cmd = """osascript -e 'tell application "Stata" to open POSIX file "%s"' &""" % dofile_path
        os.system(cmd)

by

        cmd = "open -n /Applications/Stata/Stata.app"
        os.system(cmd)
        cmd = """osascript -e 'tell application "Stata" to open POSIX file "%s"' &""" % dofile_path
        os.system(cmd)