in frontmost folder in finder: new txt&folder with name from dialog

See posts below for better scripts that do the same thing (or more), with less code.

# I launch this with an app like apptivate or keyboard maestro
set PATH_OF_YOUR_TEMPLATE_TXT_FILE to text_here
tell application "Finder"
	display dialog "new_name_dialogue" default answer " "
	set new_name to (text returned of result)
	set Selected_Finder_Item to (folder of the front window) as text
	make new folder at Selected_Finder_Item with properties {name:new_name}
	set Path_Of_New_Folder to Selected_Finder_Item & new_name & ":" as text
	duplicate file PATH_OF_YOUR_TEMPLATE_TXT_FILE to Path_Of_New_Folder
	set Path_Of_X to Path_Of_New_Folder & "x.txt" as string
	set name of file Path_Of_X to (new_name as text) & ".txt"
	set Path_Of_New_TXT to Path_Of_New_Folder & new_name & ".txt"
	--	"display dialog Path_Of_New_TXT" was useful as a test, it made it clear you forgot to add ":"
	delay 0.1
	set Path_Of_New_TXT_As_Text to Path_Of_New_TXT as text
	
	tell application "Finder"
		open Path_Of_New_TXT_As_Text
	end tell
	delay 0.1
	tell application "TextEdit"
		activate
	end tell
end tell

This is a different way to do the same thing :cool:

tell application "Finder"
	set currentFolder to folder of front window
	set newFolderName to text returned of (display dialog "Name the new folder:" default answer "")
	set newFolder to make new folder at currentFolder with properties {name:newFolderName}
	set newFileName to newFolderName
end tell

set newFilePath to (POSIX path of (newFolder as alias)) & newFileName & ".txt"
do shell script "touch " & quoted form of newFilePath

set newFile to POSIX file newFilePath as text
tell application "Finder"
	set extension hidden of alias newFile to true
	open newFile
end tell

I have a, yet different, script as an applet in the Finder toolbar: ‘New file here’. Lets me select a template from a list.

Hi, Ole.

Thanks for posting your script. From a coding point of view, though, all of your coercions to text are unnecessary and two of them are actually wrong given the subsequent use of the results:

   set Selected_Finder_Item to (folder of the front window) as text¨
   make new folder at Selected_Finder_Item with properties {name:new_name}

The second line above is nominally making a new folder at some text rather than at a folder, although it performs the action actually intended because the Finder’s designed to tolerate such errors. A similar tolerance applies where you tell the Finder to ‘open Path_Of_New_TXT_As_Text’.

Since the introduction of Mac OS X some twelve or so years ago, the class of Finder window which shows items in a folder has been a ‘Finder window’ and the folder on which it’s open has been its ‘target’ ” although ‘window’ works because it’s the generic class for every kind of window in the Finder and ‘folder’ still works presumably for backwards compatibility.

It’s seldom a good idea to nest ‘tell’ statements to different applications and in this case it’s not necessary.

Please excuse the pendantry, but this is Code Exchange. :wink:

# I launch this with an app like apptivate or keyboard maestro
set PATH_OF_YOUR_TEMPLATE_TXT_FILE to text_here
tell application "Finder"
	activate
	display dialog "new_name_dialogue" default answer " "
	set new_name to (text returned of result)
	set Selected_Finder_Item to (target of the front Finder window)
	set New_Folder to (make new folder at Selected_Finder_Item with properties {name:new_name})
	-- Get an alias when duplicating the file so that changing the name won't require a new reference and it'll be understandable to TextEdit.
	set New_TXT_file to (duplicate file PATH_OF_YOUR_TEMPLATE_TXT_FILE to New_Folder) as alias
	set name of New_TXT_file to new_name & ".txt"
end tell

tell application "TextEdit"
	activate
	open New_TXT_file
end tell

this too :cool: but without a template file but with template contents.

set template to "Hello World!" --template text; you won't need a template file

tell application "Finder"
	set currentFolder to folder of front window
	set newFolderName to text returned of (display dialog "Name the new folder:" default answer "")
	set {newFolder, newFileInFolder} to {POSIX path of (currentFolder as string) & newFolderName, POSIX path of (currentFolder as string) & newFolderName & "/" & newFolderName & ".txt"}
end tell

do shell script "mkdir " & quoted form of newFolder
do shell script "/bin/echo -n " & quoted form of template & " > " & quoted form of newFileInFolder
do shell script "open -e " & quoted form of newFileInFolder

I’m came here for pedantry, not adulation. :stuck_out_tongue: Thank you guys! I’ll start using these. (Not needing a template file will be particularly nice.)