Creating text files for each file in folder

Guys I have been trying to learn how to achieve this but I am not skilled enough to make it work. This is the code I have been tweaking back and fourth but I just can’t get it to show the extension I know I am removing it in the code as the code I started with was made for creating folders so probably it was its purpose to remove the extension?

I would appreciate a lot if any one could spend a minute checking what I am doing wrong.

tell application “Finder”
set selectedFolder to selection
set current_folder to item 1 of selectedFolder
set theList to every file of current_folder
repeat with this_file in theList
set cur_ext to name extension of this_file
set new_name to text 1 thru -((length of cur_ext) + 2) of (name of this_file as text)
make new file at current_folder with properties {name:new_name, file type:“TEXT”, creator type:“ttxt”}
end repeat
end tell

Hi LuisB. Welcome to MacScripter.

I’m not sure what you’re asking. The code you’ve posted seems to work: that is, if you select a folder and run the script, empty files are created in the folder corresponding to all the files currently there. These new files have the same names as the originals but without the extensions. If any of the original names don’t have extensions, or if you run the script again without removing the new files first, you’ll get an error because one or more of the new names already exist in the folder.

Could you please clarify what it is that isn’t working for you?

Hey Nigel thanks for the quick reply.

The problem I has was that the files didn’t have extension so I added another piece of code to make extensions for those files and now it works fine but I was wondering how to join them as its like running two script in one. Not a problem to be honest but maybe it could look cleaner?

tell application “Finder”
set selectedFolder to selection
set current_folder to item 1 of selectedFolder
set theList to every file of current_folder
repeat with this_file in theList
set cur_ext to name extension of this_file
set new_name to text 1 thru -((length of cur_ext) + 2) of (name of this_file as text)
make new file at current_folder with properties {name:new_name, file type:“TEXT”, creator type:“ttxt”}
end repeat
end tell
set ext to “.txt”
tell application “Finder”
set selectedFolder to selection
set current_folder to item 1 of selectedFolder
set theList to every file of current_folder
repeat with curFile in theList
if the name of curFile does not contain “.” then
set the name of curFile to (the name of curFile & ext)
end if
end repeat
end tell

Ah. I see. Well. You can append the new extension to the text you’re using to name each file:

set ext to ".txt"

tell application "Finder"
	set selectedFolder to selection
	set current_folder to item 1 of selectedFolder
	set theList to every file of current_folder
	repeat with this_file in theList
		set cur_ext to name extension of this_file
		set new_name to text 1 thru -((length of cur_ext) + 2) of (name of this_file as text) & ext
		make new file at current_folder with properties {name:new_name, file type:"TEXT", creator type:"ttxt"}
	end repeat
end tell

Wow how simple when you have the skills! A million thanks mate.

Now I was going to research how to add some text within the files created they will all have the same paragraph. Any easy way to do that?

I tried using the value:“text here” within the properties but it does nothing probably that is not the way.

I am also trying to add this line to the equation:

set extension hidden of new_name to false

But it gives an error:

error “Can’t set extension hidden of "filename.txt" to false.” number -10006 from extension hidden of “filename.txt”

I get this error:

error "TextEdit got an error: Can’t set alias "I7

when using this:

set myFiles to (choose file with multiple selections allowed)
tell application “TextEdit”
repeat with aFile in myFiles
set text of (aFile) to the clipboard
save
close
end repeat
end tell

Hi LuisB.

I’m not sure what to suggest about hiding the extensions. The reason you’re getting the error is that ‘extension hidden’ is a property of the file itself, not of the name. So you’d need to do something like this:


tell application "Finder"
	-- Rest of code snipped snipped for brevity.
	
	set new_file to (make new file at current_folder with properties {name:new_name, file type:"TEXT", creator type:"ttxt"})
	set extension hidden of new_file to true
	
end tell

Or possibly:


tell application "Finder"
	-- Rest of code snipped snipped for brevity.
	
	make new file at current_folder with properties {name:new_name, file type:"TEXT", creator type:"ttxt", extension hidden:true}
	
end tell

The reason I’m not sure about it myself is that I always have “Show all filename extensions” checked in my Finder preferences. When I run the script with either of the above modifications, the “.txt” extensions are shown in the window anyway. They don’t get hidden. If I uncheck the preference and run the script again ” with or without the modifications ” the new files’ extensions are hidden but those of the originals remain visible. If I run the script again without the modifications and with “Show all filename extensions” checked, the extensions remain visible as expected. But if I then uncheck “Show all filename extensions”, only the extensions of the new files disappear. The extensions of the older files remain visible. :confused:

With regard to creating the files and then writing text to them, there are a few combinations of approaches.

The simplest in your case would be to create the files as you have done and to use ‘write’ ” which is one of the File Read/Write commands, not a Finder command ” to write the text to each file as it’s created.


set ext to ".txt"
set some_text to "This is some very interesting text."

tell application "Finder"
	set selectedFolder to selection
	set current_folder to item 1 of selectedFolder
	set theList to every file of current_folder
end tell

repeat with this_file in theList
	tell application "Finder"
		set cur_ext to name extension of this_file
		set new_name to text 1 thru -((length of cur_ext) + 2) of (name of this_file as text) & ext
		set new_file to (make new file at current_folder with properties {name:new_name, file type:"TEXT", creator type:"ttxt"}) as alias
		--set extension hidden of new_file to true
	end tell
	
	write some_text to new_file
end repeat

Obviously the text used here is just for demonstration purposes. Also, ‘write’ has optional parameters which control the kind of text (Mac Roman, UTF-8, UTF-16) written to the file and whether the text is appended to the file or written over the existing contents.

Another approach would be to use the File Read/Write commands to create the files too. This is more complex and certain precautions have to be taken, but it can be faster if there are a great many files in the folder. I won’t confuse you with code for this here unless you think you may need it.

Thanks again Nigel I am really in debt here as I was going nuts trying to find a solution for it.

The extension wasn’t really an issue as you can select all file types and do a get summary info and uncheck hide extension which will change them all to visible. But the code set extension hidden of new_file to false worked too.

I also tested using this set some_text to the clipboard and it seems to work perfectly fine so pasting the clipboard content its another option.

I got one last question that might be too complex.

Can some_text dynamically change a portion of the paragraph according to the number set in the filename

Not sure if I am explaining this properly but this is an example:

This text is added through out every new text file:

Please enter no more than 4 comma-delineated tags.

And now if one file is called:

filename_35.txt

The internal text would be:

Please enter no more than 35 comma-delineated tags.

And so on with different names.

:rolleyes:

Are the names of all the files in the folder guaranteed to end with an underscore, a numeric representation, and then an extension?

Yes thats the best nomenclature I can think off.

OK. See how this goes:


-- This script assumes that the name of every file in the selected folder ends with an underscore, a numeral, and an extension.

set ext to ".txt"

tell application "Finder"
	set selectedFolder to selection
	set current_folder to item 1 of selectedFolder
	set theList to every file of current_folder
end tell

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"_", "."}
repeat with this_file in theList
	tell application "Finder"
		set cur_ext to name extension of this_file
		set new_name to text 1 thru -((length of cur_ext) + 2) of (name of this_file as text) & ext
		set new_file to (make new file at current_folder with properties {name:new_name, file type:"TEXT", creator type:"ttxt"}) as alias
	end tell
	
	-- If the files are named as assumed, then with the delimiters "_" and ".", the penultimate 'text item' in each name is the numeral required in the file text.
	set numeral to text item -2 of new_name
	if (numeral is "1") then
		set some_text to "Please enter no more than 1 tag."
	else
		set some_text to "Please enter no more than " & numeral & " comma-delineated tags."
	end if
	write some_text to new_file
end repeat
set AppleScript's text item delimiters to astid

Omg it works 100% a dream come true. Thanks so much for taking the time in solving this puzzle. I was thinking in adding a few more delimiters in order to change other parts of the text. In your opinion would this nomenclature in the filename work:

filename_yes_6_no.jpg

So the script uses yes, 6 and no as the modifiers.

this text is an example of how it would be:

line 1 - Please enter no more than 4 comma-delineated tags: yes
line 2 - Please enter no more than 4 comma-delineated tags: 6
line 3 - Please enter no more than 4 comma-delineated tags: no
rest of the paragraph

Does that make sense? If so would this be too complex to achieve?

Not in the iight of your previous request, no. :wink:

Not too complex ” again, as long as the files were consistently named. Otherwisewise you’d need quite a bit more code to identify the files to handle.

If the names are something like “filename_yes_6_no.jpg” and the delimiters have been set to “_” and “.” as in the script above, you can extract the three relevant bits like so:

set {bool1, numeral, bool2} to text items - 4 thru -2 of new_name

Then build your text something like this:

set some_text to "Please enter no more than 4 comma-delineated tags: " & bool1 & linefeed & ¬
	"Please enter no more than 4 comma-delineated tags: " & numeral & linefeed & ¬
	"Please enter no more than 4 comma-delineated tags: " & bool2 & linefeed & ¬
	"rest of the paragraph"

Thanks a lot Nigel and sorry for the confusion as I thought the way to escalate to do what you just did was by using the previous method. If it wasn’t for you I would have been stuck a lot of days trying to figure things out. I really appreciate it. Glad to find a place like this.

Thanks again!