Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.6: Create a new file anywhere via an Automator Service Desktop
Snow Leopard only hintThis a simple Service for creating new files the currently-open folder in Finder, using AppleScript and Automator.

Start by launching Automator, and in the chooser that appears, select Service. This will open a new Automator window, set up to create a Service. In that window, do the following:
  1. Set the Service to receive selected folders in Finder (the two drop-down menus at the top of the work area on the right). This makes it easier to assign a keyboard shortcut to our service, which we'll do at the end.

  2. Drag and drop the 'Run AppleScript' action (in the Utilities Library) into the work area on the right.

  3. Paste in the following AppleScript, then click the Hammer icon (to compile the code):
    on run {input, parameters}
      tell application "Finder"
        set currentPath to insertion location as text
        set x to POSIX path of currentPath
      end tell
      return x
    end run
  4. Drag and drop the 'Set value of a variable' action from the Utilities Library, and create a new variable called, for example, CurrentFolder. (Do this by clicking on the drop-down menu.)

  5. Drag and drop the New Text File action from the Text library into the work area. Then drag CurrentFolder from the Variable panel at the bottom of the Automator window to the Where entry of the New Text File action. Click the Options button in the action, and click the 'Show this action when the workflow runs' box. This makes it possible to change the name and extension of the new file.

  6. Save the service as 'Create New File,' or whatever you like. Test your new action using the Services menu in Finder, to make sure it works (select a folder, Control-click, and you should see your new Service at the bottom of the contextual menu).

  7. On the Keyboard Shortcuts tab of the Keyboard System Preferences panel, select Services in the left-hand column, and your new service (it should be in the Files and Folders section) in the right-hand panel. Assign a keyboard shortcut to the new Service by double-clicking to the right of the Service's name.

  8. Switch back to Automator and change the leftmost drop-down menu (at the top of the work area) to No Input, then save the Service again.
[crarko adds: This worked as described in testing, except I couldn't get the keyboard shortcut to work when in Column View -- it worked fine in List and Icon view modes. In Column View, though, it would simply try to select the file whose first letter matched the key used in the shortcut.]
    •    
  • Currently 3.47 / 5
  You rated: 4 / 5 (19 votes cast)
 
[23,815 views]  

10.6: Create a new file anywhere via an Automator Service | 20 comments | Create New Account
Click here to return to the '10.6: Create a new file anywhere via an Automator Service' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.6: Create a new file anywhere via an Automator Service
Authored by: slb on May 12, '10 01:42:54PM

I know, I could test this to see what it's doing, but I can't be the only one that read this and wondered - "what does this do"?



[ Reply to This | # ]
10.6: Create a new file anywhere via an Automator Service
Authored by: joeyblades on May 12, '10 01:50:41PM

and why do I want to do it???



[ Reply to This | # ]
10.6: Create a new file anywhere via an Automator Service
Authored by: auco on May 13, '10 05:52:16AM

if you ever opened Automator, you'd knew. It really depends on what you want to save in your new file...
(or: if you need to ask why you'd need it, you most likely don't need it ;-)



[ Reply to This | # ]
10.6: Create a new file anywhere via an Automator Service
Authored by: joeyblades on May 13, '10 09:35:09AM

Well, I use Automator, but I've never needed to create an arbitrary new file, which seems to be what you are trying to do. I get the impression that your real goal is to be able to assign this action of creating a file to a contextual menu...

Frankly, I don't understand why that is important to you, but perhaps it's just a difference in style. I usually start my activity at the application level and when I need a new file, command-N does the trick. I can't think of any time that I need a new file where I'm not going to work on it with some application...

Of course, I'm still just guessing at what problem you're trying to solve...



[ Reply to This | # ]
10.6: Create a new file anywhere via an Automator Service
Authored by: leamanc on May 13, '10 07:40:40AM

I assume this is an attempt to duplicate a feature in Windows Explorer and various Linux GUI file managers: Right-click and "Create New File Here..." (Often with the option of what kind of file, like .txt, .html, etc.).



[ Reply to This | # ]
10.6: Create a new file anywhere via an Automator Service
Authored by: schutt on May 14, '10 07:07:45AM

Yeah, that's what this does, but only for text files, and you have to have a folder selected, instead of just clicking in a folder. The "Create new X file here" is one the two useful features Win has that Mac doesn't. There used to be a program that added this feature, but it broke in 10.6. Since I most often use the "new file here" option for text or source code files, this is a good enough solution.



[ Reply to This | # ]
10.6: Create a new file anywhere via an Automator Service
Authored by: zsaeid on May 14, '10 07:30:33AM

To make it more clear, you dont need to have folder selected, thas why we come back to the automator in step 8. You can create a file in the folder you are working.



[ Reply to This | # ]
10.6: Create a new file anywhere via an Automator Service
Authored by: zsaeid on May 14, '10 07:49:50AM

As stated in the last step of the hint, you should comeback to automator (after assigning a keyboard shortcut) and change the "the service receives selected folder... " to "no input", to be able to use this service in the folder you are working in (without need to select some folder).



[ Reply to This | # ]
10.6: Create a new file anywhere via an Automator Service
Authored by: schutt on Jun 23, '10 03:33:41PM

D'oh, I skipped that step.



[ Reply to This | # ]
10.6: Create a new file anywhere via an Automator Service
Authored by: BiL Castine on May 13, '10 09:05:48AM

Thanks for this hint. I have been needing this but never got around to figuring out exactly how to implement it.



[ Reply to This | # ]
10.6: Create a new file anywhere via an Automator Service
Authored by: osxpounder on May 13, '10 02:11:41PM

I find it useful to create a new *text* file in the currently open folder. I do that for one of two reasons:

1. I want to make a text note about the contents that are (or will soon be) in that folder.
I use an Applescript for that. I made a .app of it and placed it on my Finder's toolbar.

2. I want to make a very brief, immediately readable note about the contents of that folder. In this case, I simply save the text file, empty, typing text only in the file's title as I save it. Example: I save a text file named "THIS FOLDER CONTAINS COMPRESSED VERSIONS ONLY". I leave off the extension ".txt", and that's my visual cue that the file only exists to show me that text in its name. I use the same Applescript for that, same toolbar item.

The Applescript I use is probably very simple. Not being a scripter, I must have gotten it from a hint on a website (probably this macosxhints.com site). If I could alter it to automatically add the ".txt" extension, I would. I like file extensions. They tell me important info, at a glance. As it stands, I type that extension myself.

Apart from text files, I like being able to create new folders with a simple keyboard shortcut.



[ Reply to This | # ]
10.6: Create a new file anywhere via an Automator Service
Authored by: hefbe on May 14, '10 07:51:03AM

How can I create a new service on OS X 10.5?



[ Reply to This | # ]
10.6: Create a new file anywhere via an Automator Service
Authored by: tedw on May 14, '10 01:50:37PM

services are only available in snow leopard. if you wanted to do something like this in leopard, you'd just make an applescript (possibly a toolbar app like osxpounder uses above).

Edited on May 14, '10 01:51:05PM by tedw



[ Reply to This | # ]
10.6: Create a new file anywhere via an Automator Service
Authored by: eliogiovanni on May 15, '10 06:50:58AM

Hi zsaeid.
I am a newbie in Automator: I made step 7 (cmd + P) and step 8. If I am inside my test folder with shortcut cmd P I open a new file .txt inside the folder; if I press ctrl click from outside the folder after selecting it, in the menu Services I read no more the option Create New File, which I had selecting Folders and not changing it in No Input as suggested in step 8. Is it correct?
Thanks!

eliogiovanni



[ Reply to This | # ]
10.6: Create a new file anywhere via an Automator Service
Authored by: zsaeid on May 15, '10 11:00:23AM

Hi,

yes it is correct. It only works inside the current folder while nothing is selected. If you prefer to create a new file in a selected folder, come back to your service in automator and change no input to folders as before.

Best wishes.



[ Reply to This | # ]
Suggestion: Try Document Palette
Authored by: klktrk on May 16, '10 11:48:00PM

http://www.coldpizzasoftware.com/documentpalette/

I am not affiliated in any way. I love the fact that I can create template files. One quick keystroke, and I can pick whether to create a blank text file, blank rtf file, perl script, html page, etc etc, all up to me to set up as I need it. Excellent interface.

---
The Apotek
http://theapotek.com
The Executioner's Summary:
http://www.last.fm/label/Broken+Hill+Music/playlists/6761?autostart



[ Reply to This | # ]
10.6: Create a new file anywhere via an Automator Service
Authored by: mkoistinen on May 22, '10 08:12:19AM

Crarko - I had this trouble too -- when I tried to use a shortcut of "only" ctrl- and a letter. I finally got it tow work by using ctrl-opt-cmd-f as my shortcut, and now it works perfectly.



[ Reply to This | # ]
10.6: Create a new file anywhere via an Automator Service
Authored by: dtarin on Aug 04, '10 06:27:51PM

How about just opening up a terminal window.. (Cmd-space Terminal). Type 'cd'. Then drag the folder where you want to create a new text file and press enter.

Then type:

touch my-new-file.text

Press enter and then close terminal window.

done.

Yeah I know it's not a fancy context menu gui thing but those few steps only takes about 5 seconds.



[ Reply to This | # ]
10.6: Create a new file anywhere via an Automator Service
Authored by: xan666 on Aug 06, '10 08:24:25AM

I question the script you've put there.. it seems to function equally well without it.

tested on OS-X 10.6.4 this only allows new files to be created when right-clicking on a folder, not empty space as desired.

adding the prompt for filename/type is useless as well as it forces you to specify a full path, which takes longer then the whole rest of the process if the file is to be put within a deep folder - your better off just making untitled.txt and renaming it after.

I use just the Set Value of Variable, and New Text File actions
receives from "Files or Folders" in "Any Application"
the variable "Variable", and "Where" share "Path 1"

the result is a "Name of Workflow" option when you right click a folder, this method allows the new file to be placed INSIDE the folder, with the predetermined name. (you can specify this, but as I said above, your entering the ENTIRE path as punishment)..

I only mention this because I found this link looking to improve my current solution
thinking this would help me right-click anywhere

Also note: this script doesn't allow for multiple files to be created without intermediate name changes. says "the action 'whatever you called it' encountered a error" it would seem this service it meant to be written with 2 scripts, one to allow the service anywhere, and one to check if the name is taken, and if so, make the new name Untitled(i+1).txt (or other similar solution)

thanks anyways,

test system:
OS 10.6.4, Darwin 10.4.0
Automator v2.1(247)
MC737LL/a (core i7 MBP 15)



[ Reply to This | # ]
10.6: Create a new file anywhere via an Automator Service
Authored by: mihassan on Aug 10, '10 03:36:42AM
Hi, I am very new to AppleScript, the original script in this thread is one of the first few that I tried. I have extended the code to check if the file already exists, and if it exists the file name is changed. Also, if the user puts a different name for the file, the script remembers that and next time shows that title by default. I could not create the contextual menu for the blank space in finder window though. Here is the code:
property defaultFileName : "newTextFile"

on run {input, parameters}
	
	try
		tell application "Finder" to set the sourceFolder to (folder of the front window) as alias
	on error
		-- no open folder windows
		set the sourceFolder to path to desktop folder as alias
	end try
	
	set flagFileExists to true
	set indexFile to ""
	
	repeat while flagFileExists
		set indexFile to (indexFile + 1)
		tell application "Finder"
			set flagFileExists to (exists file (defaultFileName & indexFile & ".txt") in sourceFolder)
		end tell
	end repeat
	
	try
		tell me
			activate
			set newFileName to text returned of (display dialog "Enter new file name" default answer "" & defaultFileName & indexFile)
		end tell
		if newFileName is not equal to "" & defaultFileName & indexFile then
			set defaultFileName to newFileName
		end if
		set newFile to "" & sourceFolder & newFileName & ".txt"
		
		if not flagFileExists then
			set touchScript to "touch " & quoted form of (POSIX path of newFile)
			set openScript to "open " & quoted form of (POSIX path of newFile)			
			do shell script touchScript
			do shell script openScript
		else
			display dialog "File already exists"
		end if
	end try
	return input
end run


[ Reply to This | # ]