views:

534

answers:

5

Is there a way to simply create a new document e.g. on the Desktop and open it with e.g. textmate with a simple shortcut or script. I know that the MS Windows approach where you can just create a new empty txt file directly is not working for Mac. I am looking for a method achieving something similar. Any ideas?

+1  A: 

You can write this in the terminal:

touch filename.txt

Or as a script:

#!/bin/sh
touch filename.txt
Emil Vikström
but then I need a filename first … or using some timestamp, hm …
Bernd Plontsch
read about mktemp: http://www.manpagez.com/man/1/mktemp/
slebetman
nice command! :)
Bernd Plontsch
+1  A: 

How about the unix approach of creating an empty file with touch ?

It could be done in a script, and passed to an application.

pavium
+1  A: 

Traditional on the shell is to use the touch command. But in any programming language you can do it without running an external program by opening a file with the O_CREAT flag:

in C:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

close(open("myfile.txt", O_WRONLY | O_CREAT | O_TRUNC, 0664)); 

in Perl:

open TEMP '>', 'myfile.txt';
close TEMP;

in Tcl:

close [open "myfile.txt" w+]
slebetman
+2  A: 

alt text

This uses TextMate's mate command line helper application.

If it's not installed, go to TextMate > Help > Terminal Usage.


#!/bin/bash
cd "$(dirname "$0")"
ntf="Untitled $(date +%s).txt"
touch "$ntf"
mate "$ntf"
  • Save this on your Desktop as "New Text File.command"
  • Make it executable (in Terminal: chmod +x "New Text File.command")
  • Optional: Copy and paste the TextMate icon from TextMate.app's "Get Info" dialog in to your new file's "Get Info" dialog.
frou
perfect! thanks so much. is there also a way to automatically close the terminal window as soon as its process has ended?
Bernd Plontsch
Ah.. I think you can make Terminal close windows automatically, but you tell it to quit (and probably close just one window, too), with a final line of AppleScript: osascript -e 'Tell application "Terminal" to quit'
frou
A: 

There's a few third party tools that add that kind of functionality. The newest I've seen is Templates

RyanWilcox