; AutoIt v3 script to run Stata commands 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 Eva Poen (eva.poen at unisg.ch), 27 June 2004.
; AutoIt is available at http://www.autoitscript.com/autoit3/.
; Declare variables
Global $Statapath, $Statawin, $commands, $tempfile, $tempfile2
; 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.2"
; 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"
; 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)
; Clear clipboard
ClipPut("")
; Copy selected lines from editor to clipboard
Send("^c")
; Pause avoids problem with clipboard, may be AutoIt or Windows bug
Sleep(100)
$commands = ClipGet()
; Terminate script if nothing selected
If $commands = "" Then
Exit
EndIf
; Create file name in system temporary directory
$tempfile = EnvGet("TEMP") & "\Statacmd.tmp"
; Open file for writing and check that it worked
$tempfile2 = FileOpen($tempfile,2)
If $tempfile2 = -1 Then
MsgBox(0,"Error: Cannot open temporary file","at [" & $tempfile & "]")
Exit
EndIf
; Write commands to temporary file, add CR-LF at end
; to ensure last line is executed by Stata
FileWrite($tempfile2,$commands & @CRLF)
FileClose($tempfile2)
; 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 temporary file
; Double quotes around $tempfile needed in case path contains blanks
ClipPut("do " & '"' & $tempfile & '"')
; 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 temporary file
; Double quotes around $dofile needed in case path contains blanks
ClipPut("do " & '"' & $tempfile & '"')
; Pause avoids problem with clipboard, may be AutoIt or Windows bug
Sleep(100)
Send("^v" & "{Enter}")
EndIf
|