(* Let a user create a new file in the current folder *) property defaultFilePrefix : "New" property templateFolderName : "New File Templates" on run tell application "Finder" activate -- not sure why we'd have to do this, but... set theFolder to (my getCurrentFolder()) set templateFolder to my getDocsFolder(templateFolderName) set fileTemplates to name of files of templateFolder -- Get them started if there aren't any templates if (count of fileTemplates) is less than 1 then set newTextFilePath to POSIX path of templateFolder & "Text File.txt" do shell script "touch " & (quoted form of newTextFilePath) set fileTemplates to {"Text File.txt"} end if -- Otherwise, let the operator select from the file templates set templateFileName to first item of (choose from list fileTemplates with prompt "What kind of file would you like to create?" without multiple selections allowed and empty selection allowed) set templateFile to file result of templateFolder as alias set theFileName to my makeUniqueName("New " & templateFileName, theFolder) set templateFilePath to POSIX path of templateFile set newFilePath to (POSIX path of theFolder) & theFileName -- Copy it over, using cp -R to handle packages do shell script "cp -R " & (quoted form of templateFilePath) & " " & (quoted form of newFilePath) reveal file theFileName of theFolder -- put us into file rename mode tell application "System Events" to keystroke return end tell end run on getCurrentFolder() tell application "Finder" activate set theFolder to (path to desktop folder as alias) repeat with i from 0 to (count of windows) try get folder of window i set theFolder to result exit repeat end try end repeat return theFolder as alias end tell end getCurrentFolder on makeUniqueName(filename, destination) tell application "Finder" set i to 0 set AppleScript's text item delimiters to "." set tiName to text items of filename set AppleScript's text item delimiters to "" set tiCount to count of tiName if tiCount > 1 then set baseExt to last item of tiName set basename to items 1 through (tiCount - 1) of tiName as string else set basename to filename set baseExt to "" end if repeat if i = 0 then set targetName to basename & "." & baseExt else set targetName to basename & " " & i & "." & baseExt end if set i to i + 1 try get file targetName of destination on error return targetName end try end repeat end tell end makeUniqueName (* Return an appropriately named folder in the current user's Documents folder. If it does not exist, create one. *) on getDocsFolder(folderName) tell application "Finder" try return folder folderName of (path to documents folder) as alias on error errmsg number errnum from errobj if errnum is -1700 then make new folder at (path to documents folder) as alias with properties {name:folderName} return result as alias else error errmsg number errnum end if end try end tell end getDocsFolder