title: Creating an AppleScript to do a find in a replace in a given document
body: <p>I'm looking to create aAppleScript that when run will:</p>
  
  <ol>
  <li>Search the document for a given string</li>
  <li>Replace that string with another given string</li>
  </ol>
  
  <p>The strings will always be the same</p>
  
  <p>Search for 
  
  <p>This will be used in textmate - <a href="https://stackoverflow.com/questions/2038073">I was trying to do this in textmate</a></p>
  
  <p>I know I can use textmate's find and replace functionality - I'm just trying to automate a little.</p>
  
  <p>This should only make changes on the current page.</p>
  
  <p>Is this possible?</p>
  
  <p><strong>UPDATE</strong>:
  So I've found some code that has got me started...</p>
  
  <pre><code>tell application "TextMate" to activate
  tell application "System Events"
      keystroke "f" using {command down}
          tell process "TextMate"
              keystroke "&lt;?"
              keystroke tab
              keystroke "&lt;?php"
              click button "Replace All"
          end tell
      keystroke "esc"
  end tell
  </code></pre>
  
  <p>but I get the following error:</p>
  
  <pre><code>error "System Events got an error: Can’t get button \"Replace All\" of process \"TextMate\"." number -1728 from button "Replace All" of process "TextMate"
  </code></pre>
  
  <p>On the find and replace dialog of Textmate the button is labeled "Replace All"  Am I missing something here?</p>