title: Creating an automator service to create a new document in the current directory
body: <p>so I'm trying to create a service that will be located in the contextual menu of the Finder and that would allow to create a new document in the current directory.</p>
  
  <p>I've been doing that using Automator:
  <img src="https://i.stack.imgur.com/h2jIJ.png" alt="Automator project">
  Sorry everything's in French ^^</p>
  
  <p>Anyway here's the AppleScript that I'm using to retrieve the current working directory:</p>
  
  <pre><code>on run {input, parameters}
      tell application "Finder"
          set pwdAlias to insertion location as alias
          if not (exists folder pwdAlias) then
              set pwdAlias to (container of pwdAlias) as alias
          end if
      end tell
      set pwd to POSIX path of pwdAlias
      return pwd
  end run
  </code></pre>
  
  <p>Then I'm setting this value to a variable, then creating a new text document using the variable as the path for the document and finally I'm using the command Reveal in Finder to show the created document.</p>
  
  <p>Everything's is working fine except that the script seems to always be <strong>late</strong>!
  What I mean is that when I open a new Finder window and select my service, it is systematically creating the document on the previous window as shown below:
  <img src="https://i.stack.imgur.com/Uj62h.png" alt="Selecting Create a new document">
  <img src="https://i.stack.imgur.com/VXcys.png" alt="The document is created at the wrong location"></p>
  
  <p>But then if I try a second time, the document is being created properly at the expected location:
  <img src="https://i.stack.imgur.com/R6ioR.png" alt="Trying again to create a new document">
  <img src="https://i.stack.imgur.com/gYMyX.png" alt="This time the document is created properly!"></p>
  
  <p>And this is very systematic it happens every time!!</p>
  
  <p>Sorry if I'm not very clear, it is not so easy to explain!</p>
  
  <p>Well otherwise, I'm running Mountain Lion and here's the Automator project attached: <a href="https://www.dropbox.com/s/d4j6a6xz5k0drxs/Cr%C3%A9er%20un%20nouveau%20document.zip" rel="noreferrer">create_new_document</a></p>
  
  <p>To add the service just unzip and put the file under ~/Library/Services/</p>
  
  <p>Hope to get some answers but I fear that this is just an Automator bug!</p>