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

An AppleScript to create a file in the current folder Desktop
Even the best GUIs are challenged to ease the navigation through the plethora of files and folders on a typical set up. Say, for example, that after a bunch of searching or drilling down, you finally have before you the Finder window displaying the folder in which you want to work, and you want to create a file -- right there. Normally, I would launch my app of choice, say BBEdit, create my new file, and in the 'Save As...' dialogue, navigate all the way back down to wherever I had already found in the Finder to save the file in the right, deeply nested, spot.

Or, as I just discovered [Hints history, May 2001: Jump to locations in open/save dialogs], you can drag and drop the Finder folder window to the "Save As..." dialogue box to save that step (remember the trusty Default Folder cdev?). Still, this way, you have to have the windows positioned just so..

So below is a very basic AppleScript to create a file using the Unix command touch. I saved it as an application in Script Editor, and then added the resulting application to the toolbar above Finder windows (drag and hover, then drop the app when you see the green "plus" icon), so I can create a file in any Finder window I am looking at.
try
  tell application "Finder" to set the this_folder ¨
   to (folder of the front window) as alias
on error -- no open folder windows
  set the this_folder to path to desktop folder as alias
end try

set thefilename to text returned of (display dialog ¨
 "Create file named:" default answer "filename.txt")
set thefullpath to POSIX path of this_folder & thefilename
do shell script "touch \"" & thefullpath & "\""
I tend to drag the resulting file onto an app using QuickSilver, so I don't really mind that the creator/filetype are set to TextEdit. BBEdit, for example, then learns which directory I want to be in and this saves me a bunch of time navigating about. I am sure there are neater ways to do this, but it works for me. Credits due to other authors on this site for respective code fragments...
    •    
  • Currently 3.88 / 5
  You rated: 5 / 5 (8 votes cast)
 
[31,259 views]  

An AppleScript to create a file in the current folder | 9 comments | Create New Account
Click here to return to the 'An AppleScript to create a file in the current folder' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to create a file in the current folder
Authored by: Tulse on Feb 24, '05 09:54:33AM
If you're willing to shell out some bucks, the $15 Document Palette will do the same thing, and allow you to set the filetype as well.

[ Reply to This | # ]
An AppleScript to create a file in the current folder
Authored by: lsequeir on Feb 24, '05 10:37:54AM

I found out that this functionality is already there in Panther :-)
If you do "Save as..." and drag the title icon of the desired Finder window to the sidebar of the Save dialog, it will switch to that folder.
Pretty nice, mac way of doing things!


---
Luís



[ Reply to This | # ]
An AppleScript to create a file in the current folder
Authored by: edalytical on Feb 25, '05 01:29:31PM
Some other cool things about Document Palette:

It uses a keyboard shortcut. It allows you to add any file .txt, .rtf, .rtfd, .doc, .xls, .html, .ooutline, or whatever. It retains type/creator codes. It doesn't just create blank files, you can add content to the files and use them like you would stationery pads or template files.



[ Reply to This | # ]
Exposť is Our Friend
Authored by: Bedhead on Feb 24, '05 12:20:28PM
you can drag and drop the Finder folder window to the "Save As..." dialogue box to save that step (remember the trusty Default Folder cdev?). Still, this way, you have to have the windows positioned just so..
If you click on the title bar of the Finder window, hit F9, drag to the Save As... dialog box, then hit F9 again, you can get around the "window placement" issue. This technique is even more effective if you have a button on your multi-button mouse set to F9!

[ Reply to This | # ]
An AppleScript to create a file in the current folder
Authored by: biedawo on Feb 24, '05 12:36:42PM

This is a common behavior for me, as well, and an area that I feel Apple needs to improve.

I'm currently demoing "Default Folder X" for exactly this reason--Save As... dialogs now have a popup menu showing currently open Finder windows. (I can't believe that Apple doesn't include this--I've been sending it as feedback since 10.0)

I wrote a contextual menu item that allows me to right-click in a window and select "New Text File", which operates pretty much like the "New Folder" menu item. You can download it from: http://tina.stanford.edu/~hills/Tidbits

Incidentally, I believe that "Contextual Menu Workshop" has some options for adding applescripts to the finder's contextual menus...



[ Reply to This | # ]
An AppleScript to create a file in the current folder
Authored by: PCheese on Feb 24, '05 07:50:31PM
I spent some time this afternoon writing a more complete solution... this one sets the file and creator type of the new file. If you use it often, save it as a stay-open application so it'll be faster on subsequent uses.

[ Reply to This | # ]
Trouble with newer version of script
Authored by: PCheese on Feb 24, '05 08:40:26PM
I'm trying to make an even better script to add custom file/creator types without modifying the source code, yet AppleScript is acting strangely... can anyone help?

[ Reply to This | # ]
Problems fixed
Authored by: PCheese on Feb 25, '05 05:28:57PM
Never mind, trouble fixed.

Check out the new version!

[ Reply to This | # ]
Bad link
Authored by: cooker47 on Dec 04, '08 10:41:24AM

This link directs you to http://pcheese.ferazel.net/scripts/Create%20File.applescript which comes up as a text file in your browser and redirects you in turn to http://pcheese.ferazel.net/scripts/createfile.html.

Download and unzip the referenced .zip file to get Create File.app.

You can do "Show Package Contents" in Finder to see the original Applescript file main.scpt under Contents/Resources/Scripts.

[ Reply to This | # ]