http://huebler.info/2005/20050310_Stata_editor.html

Script 1: Run a do-file from an external editor

; AutoIt v3 script to run a Stata do-file from an external text editor.
; Version 2.2, Friedrich Huebler (fhuebler at gmail.com), 26 September 2005.
; Updated by Nicholas Winter (nw53 at cornell.edu), 25 May 2005.
; Adapted from a script by Dimitriy V. Masterov
; (dvmaster at lily.src.uchicago.edu), 23 June 2004.
; AutoIt is available at http://www.autoitscript.com/autoit3/.

; Declare variables
Global $statapath, $statawin, $dofile

; NOTE: Edit $statapath and $statawin before script is compiled
; Path to Stata executable
$statapath = "C:/Program Files/Stata9/wsestata.exe"
; Title of Stata window
$statawin = "Stata/SE 9.1"

; EXAMPLE: For Intercooled Stata 8.2 delete preceding block and use commands below
; $statapath = "C:/Program Files/Stata8/wstata.exe"
; $statawin = "Intercooled Stata 8.2"

; NOTE: Edit this block of commands to match the editor used
; EmEditor or TextPad: path of do-file is passed to AutoIt
$dofile = $CmdLine1

; Alternative method to obtain path of do-file with EmEditor
; Get path of do-file from title of EmEditor window
; $dofile = WinGetTitle("")
; Remove unwanted text from window title to keep path only
; $dofile = StringReplace($dofile," – EmEditor","")

; Alternative method to obtain path of do-file with TextPad
; Get path of do-file from title of TextPad window
; $dofile = WinGetTitle("")
; Remove unwanted text from window title to keep path only
; $dofile = StringReplace($dofile,"TextPad – ","")
; $dofile = StringReplace($dofile,"[","")
; $dofile = StringReplace($dofile,"]","")

; If more than one Stata window is open, the window
; that was most recently active will be matched
Opt("WinTitleMatchMode",2)

; Reduce SendKeyDelay and WinWaitDelay to speed up script
Opt("SendKeyDelay", 1)
Opt("WinWaitDelay", 200)

; Check if Stata is already open, run it if not
If WinExists($statawin) Then
WinActivate($statawin)
WinWaitActive($statawin)
; Activate Stata Command Window and select text (if any)
Send("^4")
Send("^a")
; Run saved do-file
; Double quotes around $dofile needed in case path contains blanks
ClipPut("do " & '"' & $dofile & '"')
; Pause avoids problem with clipboard, may be AutoIt or Windows bug
Sleep(100)
Send("^v" & "{Enter}")
Else
Run($statapath)
WinWaitActive($statawin)
; Activate Stata Command Window
Send("^4")
; Run saved do-file
; Double quotes around $dofile needed in case path contains blanks
ClipPut("do " & '"' & $dofile & '"')
; Pause avoids problem with clipboard, may be AutoIt or Windows bug
Sleep(100)
Send("^v" & "{Enter}")
EndIf

Comments on script 1: In most cases, the script must be edited in two places before it can work on other computers:

1. The variables $statapath and $statawin must match your configuration. $statapath contains the location of the Stata executable, which can be found with the Windows Explorer. The text in the variable $statawin is taken from the title of the main Stata window.
2. The variable $dofile stores the location of the do-file. With EmEditor and TextPad, the path can be passed to the AutoIt script via the command line; this may also be possible with other editors. An alternative is to extract the path from the title of the editor window; the script shows how this can be done with EmEditor and TextPad. The AutoIt Window Info tool, a component of the AutoIt package, can help you find some of the information that is required with the alternative approach (window title, etc.).

What the script does: If Stata is already open, the script sends the path of the do-file from the editor to the Command window in Stata, preceded by "do" and followed by Enter. If more than one instance of Stata is open, the do-file is executed in the Stata window that was most recently active. If Stata is not open, the script starts Stata and then runs the do-file.

Important limitation: A do-file must be saved before running script 1 because the script looks for the saved version of the document that is currently being edited. An alternative would be to select the entire text and run script 2 below, which passes selected lines to Stata.