Question

I am stuck. I have SublimeText plug-in that I wrote run code in Stata (a statistics package). After preparing the file, I used to do this:

cmd = """osascript<< END
 tell application "stata" 
  activate
  open POSIX file "%s"
 end tell
END""" % dofile_path
os.system(cmd)

... where dofile_path is the path to the stata file I wish to run.

Unfortunately, something changed with a Stata update and this now opens the Stata editor rather than the main package. I therefore have tried to re-write this using System Events and the clipboard.

cmd = """osascript -e "activate application \"StataMP\"" -e "tell app \"System Events\" to set the clipboard to \"do sublime2stata.do\" " -e "tell app \"System Events\" to keystroke \"v\" using {command down}" -e "tell app \"System Events\" to keystroke return"            end tell""" 

I have tried this as a multiline script like this too ...

cmd = """osascript<< END
tell application \"StataMP\"
    activate
end tell
tell application \"System Events\"
    keystroke \"1\" using {command down}
    set the clipboard to \"do %s\"
    keystroke \"v\" using {command down}
    keystroke return
end tell
say "finished"
END""" % dofile_path
os.system(cmd)

The keystroke "1" using {command down} is just there to make sure the command window of stata is selected.

The problem is that nothing happens! The file is generated, the script runs (because it says "finished" OK) but nothing is pasted into the stata command window.

Can anyone see my mistake?

Was it helpful?

Solution 2

So I have seemed to have fixed this, but almost entirely by chance. Firstly, you must go to Preferences>Do-file editor>Advanced and uncheck the box which says "Edit do-files opened from the Finder in Do-file Editor". This is not sufficient though. I also changed the applescript so that it talks to Finder rather than Stata.

cmd = """osascript<< END
tell application "Finder"
  open POSIX file "%s"
end tell
END""" % dofile_path

I'll update this thread if this turns out to be unreliable, but in the meantime I have pushed these changes to the github repository I was maintaining.

OTHER TIPS

After getting Stata 13, I had the same problems with my BBEdit script for running do-files. Here's what works now; perhaps you can adapt it to ST. The trick is to use the DoCommand scripting command to do the do file from within Stata. The script will work without the DoCommand "cd line, but that that line ensures that the program log and data files are saved in the same folder as the do file.

tell application "BBEdit"
    save text document 1
    set loc to file of text document 1 as alias
end tell


tell application "StataMP"
    activate
    if version < 13 then
        open loc
    else
        tell application "Finder"
            set foldr to POSIX path of (folder of file loc as alias)
            set ploc to the POSIX path of loc
        end tell
        DoCommand "cd " & "\"" & foldr & "\""
        DoCommand "do " & "\"" & ploc & "\""
    end if
end tell
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top