title: writing to a new empty file using applescript
body: <p>I have a very peculiar file system I want to automate using Applescript. The script I'm using works for the most part, but there is one small step I can't seem to tune down properly. The script should operate like this; when it is run, the script asks the user to choose a folder. Once selected, the script locates all files within the folder with the a specific extension (in this case jpg) Next, the script creates a blank text document for each jpg file using the picture files name as the new name. Once complete, the new text document should have text inputted from a separate pre stated file. </p>
  
  <p>For example, I run the file and pick a folder with 3 images (cake1.jpg, pie2.jpg, icecream3.jpg) The script creates 3 text documents (cake1.txt, pie2.txt, icecream3.txt) but then needs to copy the text from Script2Text.rtf into each of the new documents created. Here is the portion of the script I have that works and what I'm trying to work with</p>
  
  <pre><code>set aFolder to choose folder "Select folder to be processed"
  set extensionText to text of "txt"
  
  set theDocument to ((path to desktop as Unicode text) &amp; "Script2Text.rtf")
  set pasteText to (read file theDocument from 1)
  
  set astid to AppleScript's text item delimiters
  set AppleScript's text item delimiters to "{"
  set pasteText to text from text item 2 to -1 of pasteText
  
  
  set AppleScript's text item delimiters to "\\marg"
  set part1 to text item 1 of pasteText
  set part2 to text from text item 2 to -1 of pasteText
  set AppleScript's text item delimiters to astid
  set RTFreturn to "\\" &amp; return
  set pasteText to RTFreturn &amp; RTFreturn &amp; part1 &amp; text from paragraph 2 to -1 of part2
  
  tell application "Finder"
      set allFiles to every file of entire contents of aFolder whose name extension is in {"jpg"}
      repeat with x in allFiles
          set FileName to name of x
          if FileName contains ".jpg" then
              set AppleScript's text item delimiters to "jpg"
              if the (count of text items of FileName) is 2 then
                  set NewName to (((first text item of FileName) as string) &amp; extensionText &amp; ((last text item of FileName) as string)) as text
                  tell application "Finder" to make file at aFolder with properties {name:NewName}
  
              else
                  display dialog ("Text appears more than once. No changes made to " &amp; FileName)
              end if
          end if
  
      end repeat
  end tell
  </code></pre>
  
  <p>I can't seem to get the rest of this code to sync and add the text into the file. I know the code for ADDING works if I specify a target file directly, (like where I specify the set theDocument, if I set a second file, then set doc2access to that file, I can write to it no problem) but I can't seem to wrap my head around how to specify the file from the automated creation step, which would help.</p>
  
  <p>Here is the code I found that works in writing to a file. I just need to specify it to write to the files being created.</p>
  
  <pre><code>set doc2access to (open for access file doc2 with write permission)
      try
          set z to -1
          repeat until ((read doc2access from z for 1) is "}")
              set z to z - 1
          end repeat
          write pasteText to doc2access starting at z
          on error msg
              display dialog msg buttons {"OK"} default button 1 with icon stop
  end try
  close access doc2access
  </code></pre>
  
  <p>What I want to do is take the document NewName (which is the variable for the file being created during this portion of the script) and add the text to it. Like if it makes the text files for cake1.txt, instead of writing code to put the text directly into cake1.txt, I want the file to be created WITH the text.</p>
  
  <p>UPDATE
  I was able to fix my issue by replacing </p>
  
  <pre><code>tell application "Finder" to make file at aFolder with properties {name:NewName}
  </code></pre>
  
  <p>with </p>
  
  <pre><code>tell application "Finder" to write pasteText to ((make file at aFolder with properties {name:NewName}) as alias) 
  </code></pre>