AppleScript to Add Empty Files in Finder Quickly

Today I had the need to add a bunch of named text files to a folder in the Finder on my Mac. I found it a major pain that I had to open up BBEdit, make a new document, then save it to where I wanted it, and then manually copy & rename the files back in the Finder. A lot of effort to get 7 or 8 empty files with different names.

So I threw this little script together instead. The gist is; once launched in the Finder (using FastScripts by Red Sweater naturally. Keystroke: cmd+option+shift+N), a dialog pops up that lets you enter a file title. Then the Finder creates that file in the front-most window. If no window is open, the file is added to your Desktop instead.

I found this to be more than twice as fast as the “traditional” method of making several files. Hopefully it can help you out too. Remember, use it with FastScripts for quick keystroke access. Save as a script in the “Finder” scripts folder (“~/Library/Scripts/Applications/Finder/”).

Here’s the code. Just copy and paste into AppleScript Editor and save as noted…

property defaultFileName : "newFile.txt"
tell me to activate
set theFileName to text returned of (display dialog "Enter a file name:" default answer defaultFileName)
tell application "Finder"
    activate
    if the (count of windows) is not 0 then
        set theFolder to (folder of the front window) as text
        set theFolder to POSIX path of theFolder
    else
        set theFolder to POSIX path of (get path to desktop)
    end if
    set addedFile to (theFolder & theFileName)
    do shell script "touch '" & addedFile & "'"
    if the (count of windows) is not 0 then
        set addedFile to (POSIX file addedFile) as alias
        select addedFile
    end if
end tell

Or, you can just download the compiled script here.

iTunes, App Store, iBookstore, and Mac App Store
About Jim Mitchell

Jim started out with a Mac SE/30 and a whole lot of love for that machine. It was during those early years working with PageMaker, Freehand & Photoshop that he learned the importance of keeping a system in tip-top shape. Now, as a systems admin with more than 20 years of experience under his belt, Jim’s ongoing efforts help keep the Macs of others running smoothly. You can follow Jim on Twitter at @jimmitchell.

Speak Your Mind

*

Stop SOPA