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

2

9

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!

Paralyse answered 29/8, 2012 at 18:8 Comment(3)
By the way your link to your service is broken!Fugger
I wrote and updated the tutorial how to get this option into context menu with automator filipmolcik.com/new-file-mac-osEnyo
youtube.com/watch?v=sE_V0vzNTWQFlatware
F
7

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

Fugger answered 11/11, 2012 at 4:20 Comment(1)
See also superuser.com/questions/106943/…Aposematic
H
3

It's an open bug in 10.7 and 10.8

Use this Workaround

on run {input, parameters}
    activate application "System Events"
    activate application "Finder"
    tell application "Finder"
        set pwdAlias to insertion location as alias
        set pwdAlias to (container of pwdAlias) as alias
    end tell
    return POSIX path of pwdAlias
end run
Him answered 29/8, 2012 at 20:7 Comment(1)
hey thanks for the quick reply! well I've tried that script but it gives me really random results; I'm trying to create a document in My Documents and the document is being creating in my user folder ~. So I'm doing this a bit differently by using the path from the input(s): set pwd to (the POSIX path of first item of input). It is not as generic as I would have liked but it is doing the job!Paralyse

© 2022 - 2024 — McMap. All rights reserved.