title: Find and replace in text files using AppleScript
body: <p>I am trying to write an applescript which will run via a launch agent. What the script needs to do is edit a user preference plist file so that default save locations are specific to that user. I am aware that this can be done by just setting "~/documents" as the location in the template plist. But Premier Pro for example also needs to write scratch files to a local drive. For simplicity I would like each user to have these put in a locations based on their username. This script will only need to run if the local profile has just been created from a template at first log on.</p>
  
  <p>I have started by using some sample code found on this site and just making a simple test below. This test should edit a txt file and replace one word with another. This script is currently not working. When tested it opens up test.txt in TextEdit but does nothing more. No errors are displayed either.</p>
  
  <p>Thank you in advance</p>
  
  <p>John.</p>
  
  <pre><code>replaceText("replace this", "replace with this", "/Volumes/USB_Drive/test.txt")
  
  
  on replaceText(search_string, replacement_text, this_document)
  tell application "TextEdit"
      open this_document
      set AppleScript's text item delimiters to the search_string
      set this_text to the text of the front document as list
      set AppleScript's text item delimiters to the replacement_text
      set the text of the front document to (this_text as string)
      close this_document saving yes
  end tell
  end replaceText
  </code></pre>