New Text File Here AppleScript

Hello, I made this script because sometimes I have zip files with names what I can’t remember exactly what is the content, or I need to save some instructions or a note about something related to this file or a folder, you can use it selecting a file or folder and then run it, the script will take the name of the selected file or folder as the input name for the new text file, you can modify this name in the dialog before to create the new text file, additionally if you’re missing the .txt extension or you inputted a different extension like .srt, then the script will ask you to confirm this, please note that no matter which extension you inputted, the script will open the new file with the default app on your system, you can localize the text on dialogs/buttons and make it match with your system language.
First I was trying the script from https://www.lifehacker.com.au/2010/01/create-a-new-text-file-in-finder-with-one-click/ and it worked but now is not available and this new version is a more elegant solution.


#New Text File Here AppleScript
#2018 (ɔ) jraulc at gmail dot com

set localeString to user locale of (get system info)

#you can localize to your language here
if localeString starts with "es" then
	#button labels
	set okButton to "Aceptar"
	set cancelButton to "Cancelar"
	set txtButton to "Usar .txt"
	set NoTxtButton to "Sin .txt"
	#labels and dialogs
	set inFileNameStr to "Por favor ingrese el nombre del archivo:"
	set askFileExtStr to "Usted quiere crear el archivo: "
	set noExtStr to "Sin una extencion '.txt'?"
	
else #default if not match with the localized language
	#button labels
	set okButton to "Acept"
	set cancelButton to "Cancel"
	set txtButton to "Use .txt"
	set NoTxtButton to "No .txt"
	#labels and dialogs
	set inFileNameStr to "Please input the file name:"
	set askFileExtStr to "Do you want to create the file: "
	set noExtStr to "Without a .txt extension?"
end if

tell application "Finder"
	set the currentFolder to (folder of the front window as alias)
	try
		set selectedItem to (item 1 of (get selection))
		set fileName to displayed name of selectedItem as string
	on error
		set fileName to name of (folder of the front window as alias) as string
	end try
	set fullFileName to fileName & ".txt"
	set createTextFile to choose file name with prompt inFileNameStr ¬
		default name fullFileName default location currentFolder
	
	set newFile to POSIX path of createTextFile
	
	if newFile does not end with ".txt" then
		set askFileExt to display alert askFileExtStr & (POSIX path of createTextFile) ¬
			message noExtStr as critical buttons {txtButton, NoTxtButton} default button 2
		set fileExt to button returned of askFileExt
		
		if fileExt is equal to txtButton then
			set newFile to newFile & ".txt"
		end if
	end if
	
	do shell script "touch \"" & newFile & "\""
	do shell script "open \"" & newFile & "\""
	
end tell


The link of the script exported to an app (10.11.6) with an icon and ready to add to the finder toolbar:
https://drive.google.com/file/d/12wfMMsDF6X5-p2aKENViFSpFPGgaudxt/view?usp=sharing
Greetings from the Cayman Islands.

Hi. Welcome to MacScripter.

That’s a nice idea! Thanks for posting it.

For a very minor increase in efficiency, you could replace the ‘try … on error’ statement with an ‘if … else’ one. (It takes slightly longer to recover from an error than it does to do a simple test.) Also, you could combine the two shell scripts into one, so that the ‘do shell script’ command only needs to be executed once:

do shell script "touch \"" & newFile & "\" ; open \"" & newFile & "\""

I myself would probably use the File Read/Write commands to create the file and either System Events or the Finder to open it. But these are just minor suggestions and don’t noticeably improve the operation of your script. :slight_smile:


#New Text File Here AppleScript
#2018 (ɔ) jraulc at gmail dot com

set localeString to user locale of (get system info)

#you can localize to your language here
if localeString starts with "es" then
	#button labels
	set okButton to "Aceptar"
	set cancelButton to "Cancelar"
	set txtButton to "Usar .txt"
	set NoTxtButton to "Sin .txt"
	#labels and dialogs
	set inFileNameStr to "Por favor ingrese el nombre del archivo:"
	set askFileExtStr to "Usted quiere crear el archivo: "
	set noExtStr to "Sin una extencion '.txt'?"
	
else #default if not match with the localized language
	#button labels
	set okButton to "Acept"
	set cancelButton to "Cancel"
	set txtButton to "Use .txt"
	set NoTxtButton to "No .txt"
	#labels and dialogs
	set inFileNameStr to "Please input the file name:"
	set askFileExtStr to "Do you want to create the file: "
	set noExtStr to "Without a .txt extension?"
end if

tell application "Finder"
	set the currentFolder to (target of the front Finder window as alias)
	set theSelection to selection
	if (theSelection is {}) then
		set fileName to name of currentFolder --(folder of the front window as alias) as string
	else
		set selectedItem to (item 1 of theSelection)
		set fileName to displayed name of selectedItem --as string
	end if
	set fullFileName to fileName & ".txt"
	set createTextFile to choose file name with prompt inFileNameStr ¬
		default name fullFileName default location currentFolder
	
	set newFile to createTextFile as text -- Now an HFS path.
	
	if newFile does not end with ".txt" then
		set askFileExt to display alert askFileExtStr & (POSIX path of createTextFile) ¬
			message noExtStr as critical buttons {txtButton, NoTxtButton} default button 2
		set fileExt to button returned of askFileExt
		
		if fileExt is equal to txtButton then
			set newFile to newFile & ".txt"
		end if
	end if
end tell

close access (open for access file newFile)

tell application "System Events" to open file newFile