set file_extension to "txt"
tell application "Finder"
set save_folder to insertion location as alias
set save_folder_name to name of save_folder
set new_file to choose file name with prompt "Enter file name:" default location save_folder
set whole_file_name to ((new_file as text) & "." & file_extension)
set new_file_POSIX to POSIX path of new_file
get exists of whole_file_name
if result is true then
beep
set new_file_alias to whole_file_name as alias
set file_name to name of new_file_alias
if save_folder_name = "Desktop" then
display alert "\"" & file_name & "\" already exists. Do you want to replace it?" message "A file with the same name already exists on the Desktop. Replacing it will overwrite its current contents." buttons {"Replace", "Cancel"} as warning
else
display alert "\"" & file_name & "\" already exists. Do you want to replace it?" message "A file with the same name already exists in the folder " & save_folder_name & ". Replacing it will overwrite its current contents." buttons {"Replace", "Cancel"} as warning
end if
if button returned of result = "Replace" then
do shell script "rm -rf '" & new_file_POSIX & "." & file_extension & "'"
delay 0.2
do shell script "touch '" & new_file_POSIX & "." & file_extension & "'"
delay 0.2
set extension hidden of new_file_alias to true
delay 0.1
select file whole_file_name
open file whole_file_name
end if
else
do shell script "touch '" & new_file_POSIX & "." & file_extension & "'"
set new_file_alias to whole_file_name as alias
set file_name to name of new_file_alias
delay 0.2
set extension hidden of new_file_alias to true
delay 0.1
select file whole_file_name
open file whole_file_name
end if
end tell