Friday, March 4, 2011

AppleScript to add a 'new file' capability to Finder

Another convenience function you may wish to add to a button in your Finder toolbar is a 'new file' button that will deposit a blank file named 'NewFile' in the current Finder window directory. It also auto increments a counter if needed to ensure the new file doesn't overwrite an existing file. The code is below:

tell application "Finder"

#Create filename (avoid overwriting)
set current_path to (POSIX path of (the folder of the front window as text))
set good_filename to false
set loop_count to 0

repeat while good_filename is false

if loop_count is equal to 0 then
set NewFileName to (current_path & "/NewFile")
else
set NewFileName to (current_path & "/NewFile" & loop_count)
end if

if exists NewFileName as POSIX file then
else
do shell script "touch " & NewFileName
set good_filename to true
end if

set loop_count to (loop_count + 1)

end repeat

end tell

No comments:

Post a Comment