title: AppleScript: Create new folder from file name and move file into that folder
body: <p>Basically, I'm trying to create a folder action in Automator so whenever a file is added to a specific folder, it will create a subfolder that matches the filename (without the extension), then move the file into that subfolder.</p>
  
  <p>So far, I have successfully used a post from this site (<a href="https://stackoverflow.com/questions/4204372/create-new-folder-named-with-a-specified-file-name-in-automator">Create new folder named with a specified file name in Automator</a>) to create a script that will create the new folder. However, I have been unable to modify the script to move the original file into the new folder.</p>
  
  <p>Any help would be appreciated. Here is the full script I am working with for reference:</p>
  
  <pre><code>    on run {input, parameters} -- make new folders from base file names
  
      set output to {}
  
      repeat with anItem in the input -- step through each item in the input
  
          set anItem to anItem as text
          tell application "System Events" to tell disk item anItem
              set theContainer to path of container
              set {theName, theExtension} to {name, name extension}
          end tell
          if theExtension is in {missing value, ""} then
              set theExtension to ""
          else
              set theExtension to "." &amp; theExtension
          end if
          set theName to text 1 thru -((count theExtension) + 1) of theName -- the name part
  
          tell application "Finder"
              make new folder at folder theContainer with properties {name:theName}
              set end of output to result as alias
          end tell
      end repeat
  
      return input -- or output
  end run
  </code></pre>
  
  <p>Thanks in advance!</p>