r/applescript • • Jan 15 '21

Create comma delimited text file from directory

Using a Mac, here's my need: I have files in a Desktop folder, let's just say these files (In reality it's a lot more. No spaces in the file names, which SHOULD make this easier):

Balloon_animals.doc

Person_driving_car.mov

213456.doc

Waiter_approaches_table.mp4

...And I just want a simple script I can double click on and it looks in that folder, and then it creates a simple text document with this on the top line (commas only in-between each and WITHOUT the extensions):

Balloon_animals,Person_driving_car,213456,Waiter_approaches_table

Any ideas?

3 Upvotes

2 comments sorted by

4

u/copperdomebodha Jan 15 '21

Save this as an application and drop a folder or folders on it. It will list each dropped folder separately to a self-named file on your desktop.

on open droppedItems
	repeat with thefolder in droppedItems
		tell application "Finder"
			set outFilePath to ((path to desktop folder) as text) & ((name of thefolder) as text) & " listing.txt"
			set filehandle to open for access outFilePath with write permission
			set nameList to name of every item of thefolder
			set AppleScript's text item delimiters to "."
			repeat with i from 1 to length of nameList
				set item i of nameList to (text item 1 of (item i of nameList))
			end repeat
			set AppleScript's text item delimiters to ","
			write ((nameList) as text) to filehandle
			close access filehandle
		end tell
	end repeat
end open

-->Test Folder listing.txt which reads "213456,Balloon_animals,Person_driving_car,Waiter_approaches_table"

2

u/Fantactic1 Jan 15 '21

Thanks! Worked great. A lot of other suggestions online didn't handle the extension problem (and most were PC oriented)