4

how can I create a Finder automaton item that appears in 'Services' for creating a new text file inside a file folder? I use TextMate, so better if the solution is editor-agnostic (rather than using TextEdit).

2

6 Answers 6

1

The way I do it: I create a dummy text file, and store it in some folder in my documents (this is used as a template for each time the service runs). I then create a workflow (service) as follows:

All you have to do now, is right click the folder you want the new file to be placed in, and select the service created by saving this workflow.

To open the newly created file, you can add an "open Finder items" action in which you would obviously select TextMate rather then TextEdit. To make it editor agnostic, you can tell it to open with its default application, and it will do so.

If asking for the name at runtime is very important to you, you can add a get text action, and assign it to a second variable, then add a rename action after the copy action and say replace text, replacing the name of your template file with the text variable. By default, you can't use text variables in the way I described, but the following post provides a work around of doing so. I know it works under 10.6, and may or may not work under earlier version of OSX.

see here for instructions

4
  • That sounds like the right answer to me. Feb 25, 2010 at 12:42
  • Nice and informative answer! I would need one last clarification: is there a way for creating an Automator action that affects the folder you are currently viewing in Finder (rather than the folder you select via right clicking on it)? Feb 25, 2010 at 13:05
  • Not that I know of. That's the only downside. I've been trying to com up with some sort of alternative so I don't need to go up a directory. However, I use the column view, so it's not such a huge deal for me. I'll let you know if I think of something. Keep me posted. I use this same method for creating new C++ classes in certain directories as well. Works like a charm.
    – finiteloop
    Feb 25, 2010 at 17:47
  • 1
    segfault, it seems that the image link of your answer is no longer working. Could you have a look at that? Cheers. May 21, 2011 at 19:21
2

This is not an automator action, but resolved the problem of creating a new file where i wanted: http://ignorethecode.net/blog/2009/05/31/creating-new-documents/. Look at the solution called "An Even Better Solution" (bottom of the page).

0
1

This should work:

do shell script "echo '[text for the text file]' > [path to folder & file].txt"

If you just want an empty text file, try this:

do shell script " > [path to folder and file].txt"

Edit: I read your question again... this is an AppleScript. I have more experience with plain Terminal than with Automator and AppleScript, but I assume you can call an AppleScript from Automator?

1
  • I would rather have this actions to open TextMate (or TextEdit, or any other text editor) with an empty file. Also, I would like the step as to how to add this in Automator so it shows in the Finder menu included in the answer. Feb 15, 2010 at 20:07
1

Okay, I cheated but you still get an Automator service. You will need to have TextMate setup to use the shell (it usually does this when you first install it, so you can use mate from the CLI).

Open Automator and create a new Service

From Utilities drag over a Run Shell Script window.

Drop in the following script:

cd ~/Desktop;
touch newfile.txt;
mate newfile.txt

What this basically does is create a file on your desktop named newfile.txt and then open in TextMate.

alt text

Requested update:

  1. Open Automater and create a new service
  2. Drag over a 'Run Applescript' window
  3. Paste in the following Applescript that was authored by Olivier Fabré:

    on run {input, parameters}
    
        tell application "Finder"
            set currentPath to insertion location as text
            set filePath to POSIX path of currentPath & input
        end tell
    
        tell application "TextMate"
            activate
            open filePath
        end tell
    
        return input
    end run
    
  4. Save the service

    Automator

What this does:

If you select text in an application it will open a blank TextMate file with the name of the selected text, in the frontmost Finder window.

What you are probably trying to do:

Other people have written tutorials on creating a TextMate button that sits in the Finder toolbar that will open blank document in that window, or open a selected file in TextMate. Which is what it sounds like what you want to do and doesn't require Automator.

5
  • This is helpful, but I have several concerns here. a) Touch is not necessary, as mate creates a file if it is not found. b) How can I parametrize the name of the filename? Another issue is that, I want this automator task to work automatically to the folder finder it is showing, not the selected folder. Feb 16, 2010 at 15:34
  • then you may want to spend some time here snippets.dzone.com/posts/show/1037 Feb 17, 2010 at 0:27
  • Nice link, although a little convoluted. Provide a simpler step-by-step process for creating the service I need (service asks for a text file name, creates it in the current finder folder -not the selected folder-, and opens it in TextMate) and you get the correct-answer reputation. ;-) Feb 18, 2010 at 0:51
  • Your update is very explanatory, thank you. It's a shame I can't mark two answers as correct. :-( Feb 25, 2010 at 21:08
  • Thank you for a nice explanation. However, I wanted the name of the filename to be asked rather than to have to select a text on Finder. It's a shame two different answers can be marked as correct. Thanks for your efforts though! Feb 25, 2010 at 21:45
0
try
    tell application "Finder"
        set ans to text returned of (display dialog "" default answer "")

        set opn to 0
        set fn to "new.txt"
        if ans is "," then
            set opn to 1
        else if ans is "/" then
            set opn to 2
        else if character -1 of ans is "," then
            set fn to text 1 thru -2 of ans
            set opn to 1
        else if character -1 of ans is "/" then
            set fn to text 1 thru -2 of ans
            set opn to 2
        end if

        if character -1 of fn is "." then
            set fn to fn & "txt"
        end if

        try
            set p to target of window 1
        on error
            set p to desktop as alias
        end try
        set f to make new file at p with properties {name:fn}
        set selection to f
        if opn is 1 then
            open f
        else if opn is 2 then
            open f using (path to application "TextMate")
        end if
    end tell
end try

I'm using this via FastScripts, but it should also work as an Automator service. And yes, this script is ugly even by AppleScript standards.

0

A modification of finiteloop's answer that's a little cleaner.

  1. Create a new service in Automator
  2. Change 'Service receives selected" to "folders"
  3. Give it the "Set Value of Variable" action, change "Storage 1" to a variable called "dest"
  4. Add the "New Text File" action, and drag the variable "dest" from the Variable pane at the bottom of the screen onto the "Where" field. This is simpler than copying a template file.
  5. Save with a reasonable name.

Now when you click on a folder, you can go to the Services menu at the bottom and find your function.

The function will also appear in the Services settings page; Settings -> Keyboard -> Shortcuts --> Services (left hand side). You can enable and disable it here. You can also assign it a keyboard shortcut. Finally, if you deselect enough items from the Folder submenu (so that there are four or fewer), your function will appear at the bottom of the Context Menu.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .