Creating an automator service to create a new document in the current directory

guitio2002 picture guitio2002 · Aug 29, 2012 · Viewed 9.8k times · Source

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.

I've been doing that using Automator: Automator project Sorry everything's in French ^^

Anyway here's the AppleScript that I'm using to retrieve the current working directory:

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

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.

Everything's is working fine except that the script seems to always be late! 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: Selecting Create a new document The document is created at the wrong location

But then if I try a second time, the document is being created properly at the expected location: Trying again to create a new document This time the document is created properly!

And this is very systematic it happens every time!!

Sorry if I'm not very clear, it is not so easy to explain!

Well otherwise, I'm running Mountain Lion and here's the Automator project attached: create_new_document

To add the service just unzip and put the file under ~/Library/Services/

Hope to get some answers but I fear that this is just an Automator bug!

Answer

markhunte picture markhunte · Nov 11, 2012

Try thisenter image description here

  • Depending on what you want to be clicking.

Set the Services selected to: 'folders' or files or folders. in 'Finder.app'

  • Get first Finder Window path Action

You can download the Get first Finder Window path Action from my blog post here The download is at the bottom of the post. The Action gets the posix path of the frontmost finder window. Since you are clicking on a folder in a window. that window will be the one returned.

  • Set Value of Variable

  • Get Specified Text

The next action 'New Text File' needs some input. If it does not get any, no file will be created. You can leave the text field blank. Just having the action in place works.

  • New Text File

Drag the Variable 'path' or what ever you named it on to the Where: drop down menu.

you can click the double blue lines at the bottom of the Automator window to toggle the workflow Variable List

workflow Variable List

Save your service. And try it. (It may take a short while to show up in the contextual Menu.)