Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 documentThe 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 documentThis 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!

like image 217
guitio2002 Avatar asked Aug 29 '12 18:08

guitio2002


People also ask

How do I add a service to my Mac?

On your Mac, select an item, such as a file on the desktop or text in a document. Open the app menu (to the right of the Apple menu ), choose Services, then choose a service.

What is a workflow Mac?

Workflow, the simplest type, is a document which is run from within the Automator app. More useful is Application, which allows you to create a standalone app onto which you can drop a file in order to trigger the action. Service creates a recipe which lives in the Services menu.


1 Answers

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.)

like image 177
markhunte Avatar answered Sep 27 '22 19:09

markhunte