Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • TextWrangler and Stata integration

    Colleagues,

    I'm using Steve's send selection to Stata script, the script works but returns an error when I attempt to run it. The results are below:
    Code:
    . sysuse auto
    (1978 Automobile Data)
    . erase temp.do
    file temp.do not found
    r(601);
    end of do-file
    The code I'm trying to run has only one line: sysuse auto. I find the script to be extremely handy and ideally I would like to run with no errors.
    Kind regards,
    Konrad
    Version: Stata/IC 13.1

  • #2
    I don't have TextWrangler so I can't try this, but I'm sure that Steve will respond. FYI, although Stata added additional Applescript support in Version 13, if you want to execute only part of a do-file, you still need to save that part out to a temporary file first and then clean it up (it is that process that is causing the error above). Note, however, that doing this doesn't automatically require Applescript; see this code from the TextMate Stata bundle where this is done via a shell script.
    Last edited by Phil Schumm; 28 Jun 2014, 16:24.

    Comment


    • #3
      I use BBedit, the paid versjon of TextWrangler. I sometimes get the file temp.do not found error, but everything else is just fine, the model runs as it is supposed to.

      [Text below is edited after checking]
      With TextWrangler's big brother BBedit, you can use an Apple Script to send a selection from the do-file to Stata. See this post:
      http://www.stata.com/statalist/archi.../msg01340.html
      Last edited by sladmin; 11 Dec 2017, 09:47. Reason: anonymize poster

      Comment


      • #4
        Simplest is to try the built-in .do file editor. Since Stata 11 especially, and even since then, it's gotten better and better, obviating the need for integration into third-party text editors. Try it, you'll like it, Sam-I-Am

        Comment


        • #5
          Originally posted by ben earnhart View Post
          Since Stata 11 especially, and even since then, it's gotten better and better, obviating the need for integration into third-party text editors.
          It has gotten better, and this is a fantastic thing, because for many users, I can now recommend that they simply use the built-in editor (together with the Project Manager) which saves them a lot of hassles. Certainly, if this does everything you need, there's no reason not to use it. That said, for people who spend a lot of their time in a favorite text editor doing many things other than working in Stata, it can be invaluable to be able to do your Stata work in the same environment. And, as far as the Stata do-file editor has come, there is still a lot of functionality it doesn't have (e.g., project-wide search and replace, variable name completion, ability to customize behavior, etc.).

          As I said, I couldn't be happier that StataCorp chose to give the do-file editor some love, and I hope they continue to do so; this provides tremendous benefit for the large majority of users. However, there will always be those who prefer to use their favorite text editor, in the same way that despite the large number of IDEs out there (some of which are very good), there are people still writing code in vi/vim, emacs, etc.

          Comment


          • #6
            Personally, the two things that I am missing most with respect to the Stata editor are concerned with the lack of autocompletion function and ugly colour schemes.
            Last edited by Konrad Zdeb; 30 Jun 2014, 08:57. Reason: Style.
            Kind regards,
            Konrad
            Version: Stata/IC 13.1

            Comment


            • #7
              I also noticed that the temp.do error does not appear if the do file is saved in the working directory. It seems that the temp.do is automatically saved in the same directory where the do file is saved and if that directory is also set as working directory there is no error with the file missing. Ideally, I would like to be able to save my do files in different directories than the working directory. I would edit my previous post but it's too late.
              Kind regards,
              Konrad
              Version: Stata/IC 13.1

              Comment


              • #8
                Originally posted by Konrad Zdeb View Post
                Personally, the two things that I am missing most with respect to the Stata editor are concerned with the lack of autocompletion function and ugly colour schemes.
                The color scheme can be customized via Preferences -> Syntax Highlighting.

                Originally posted by Konrad Zdeb View Post
                It seems that the temp.do is automatically saved in the same directory where the do file is saved and if that directory is also set as working directory there is no error with the file missing. Ideally, I would like to be able to save my do files in different directories than the working directory.
                In that case, you should check out the approach taken in the TextMate Stata bundle. That gives you the option of running the do-file (or selection) from the location of the file or from the project directory (which is what I keep my working directory set to). Even if you don't want to use TextMate, you can use the same scripts for Stata interaction with another editor (though you may have to make a few small modifications).

                Comment


                • #9
                  I took a look at Steve's version of the Send_Selection_To_Stata AppleScript. The script takes the selected text and then appends the lines

                  Code:
                  erase temp.do
                  exit
                  and saves the results in "temp.do" in the same directory as the original file (the one that contains the selected text). The script then tells Stata to

                  Code:
                  do "<full path>/temp.do"
                  This is a bit odd in the sense that Stata erases the do-file that is currently running. Anyways, I tweaked the code to append at the end of "temp.do"
                  Code:
                  cap erase "<full path>/temp.do"
                  exit
                  This should fix the issue where the original file is not located in the same directory as Stata's current directory. You can get my version at:

                  https://dl.dropboxusercontent.com/u/..._20140701.scpt

                  Comment


                  • #10
                    Robert, I will be happy to try it. Thank you for this.
                    Kind regards,
                    Konrad
                    Version: Stata/IC 13.1

                    Comment


                    • #11
                      Someone informed me off-list of the problem with the script in Stata 13. Before I saw this thread, I wrote version 5.

                      Steve

                      Steven JH Samuels
                      Consulting Statistician
                      18 Cantine's Island
                      Saugerties NY 12477 USA
                      Phone: 845-246-0774
                      Fax: 206 202 4783

                      Code:
                      (*
                      Send selection to Stata from BBEdit V5.0
                      Updated for Stata Version 13
                      Based on an original script by Ben Hulley
                      Steve Samuels
                      [email protected]
                      Version 3: updated for BBEdit 10
                      Note: Change name of Stata from "StataMP" as needed.
                      *)
                      
                      tell application "BBEdit"
                          set thecontents to the selection of window 1 of text document 1 as text
                          set theOriginalFile to file of document 1 as alias
                          set ploc to the POSIX path of theOriginalFile
                          if thecontents is "" then
                              display dialog "please select text"
                              return
                          end if
                      end tell
                      
                      set thecontents to thecontents & return & "erase temp.do" & return & "exit"
                      set tempFile to my ComputeTempFileName(theOriginalFile)
                      my write_to_file(thecontents, tempFile, false)
                      tell application "Finder" to set filename to tempFile as text
                      set ptemp to the POSIX path of tempFile
                      tell application "StataMP"
                          activate
                          if version < 13 then
                              open filename
                          else
                              tell application "Finder"
                                  set foldr to POSIX path of (folder of file theOriginalFile as alias)
                              end tell
                              DoCommand "cd " & "\"" & foldr & "\""
                              set _cmd to "do " & "\"" & ptemp & "\""
                              DoCommand _cmd
                          end if
                      end tell
                      
                      on ComputeTempFileName(originalFile)
                          tell application "Finder"
                              set c to container of originalFile
                              set t to (c as string) & "temp.do"
                          end tell
                          return t
                      end ComputeTempFileName
                      
                      on write_to_file(this_data, target_file, append_data)
                          tell application "Finder"
                              try
                                  set the target_file to the target_file as text
                                  set the open_target_file to ¬
                                      open for access file target_file with write permission
                                  if append_data is false then ¬
                                      set eof of the open_target_file to 0
                                  write this_data to the open_target_file starting at eof
                                  close access the open_target_file
                                  return true
                              on error
                                  try
                                      close access file target_file
                                  end try
                                  return false
                              end try
                          end tell
                      end write_to_file
                      Last edited by Steve Samuels; 02 Jul 2014, 15:40. Reason: Added acknowledgement of Robert's fix.
                      Steve Samuels
                      Statistical Consulting
                      [email protected]

                      Stata 14.2

                      Comment


                      • #12
                        Steve, your new version addresses the issue by changing Stata's current working directory to the directory of the original text file that contains the selection. That may not be what the user wants. Here's a modified version that bypasses the issue by creating the temporary file in the user's OS X temporary items directory. All files in that directory are wiped out by a restart so there is no need for the erase step.

                        The compiled version can be downloaded from here.

                        This script requires version 13 of Stata. Change "StataMP" to "StataSE" if that's what you run.

                        Code:
                        tell application "BBEdit"
                            set thecontents to the selection of window 1 of text document 1 as text
                        end tell
                        
                        set tempdo to (path to temporary items as string) & "selected_text.do"
                        write2File(tempdo, thecontents & return, false)
                        
                        tell application "StataMP"
                            activate
                            DoCommand "do " & "\"" & POSIX path of tempdo & "\""
                        end tell
                        
                        on write2File(theFile, theText, append)
                            try
                                set refno to open for access file theFile with write permission
                                if not append then set eof of refno to 0
                                write theText to refno starting at (get eof of refno)
                                close access refno
                            on error
                                close access file theFile
                            end try
                        end write2File

                        Comment


                        • #13
                          Great, Robert! However I can't really claim authorship of the Send-do-file-to Stata and Send-selection-toStata scripts. Ben Hulley wrote the originals and did all the heavy lifting. After Ben abandoned them, I started tweaking them to work with the newest versions of Stata My recent fix was to add the check for Version 13 so that the script would work with Stata 13 as well as with earlier versions.
                          Steve Samuels
                          Statistical Consulting
                          [email protected]

                          Stata 14.2

                          Comment


                          • #14
                            Of course, the compiled version of my "do selection in Stata" script for BBEdit/TextWrangler now includes a mention of Ben Hulley's original contribution.

                            Comment


                            • #15
                              Duly downloaded.
                              Kind regards,
                              Konrad
                              Version: Stata/IC 13.1

                              Comment

                              Working...
                              X