Trouble implementing solution posted elsewhere on this forum...

Hello, I have a functioning Automator Quick Action that creates a dated text file in whatever current directory I’m in. I just want to Automator to append numbers if a file name already exists, but I’m unsure how to do so using a solution posted elsewhere on this forum. Here’s the script I have working:


set myName to date_format((current date) as string)

to date_format(old_date) -- Old_date is text, not a date.
	set {year:y, month:m, day:d} to date old_date
	tell (y * 10000 + m * 100 + d) as string to text 1 thru 4 & "-" & text 5 thru 6 & "-" & text 7 thru 8 & "_Note_.txt"
end date_format

tell application "Finder"
	set txt to make new file at (the target of the front window) as alias with properties {name:myName as string}
	select txt
end tell

I am trying to implement DJ Bazzie Wazzie’s solution posted here:
https://macscripter.net/viewtopic.php?id=36082

but I am stuck on the line:

set existingFiles to list folder fileLocation with invisibles

… I’m not sure what to use as fileLocation if I want the service to operate in any directory on my computer? Is his solution not the way to go for what I have already? I appreciate any help.

List folder of Scripting Additions is deprecated. Other post from this post uses Finder, which is slow solution. And I don’t know if it works at least. But, no need list folder, no need Finder. Here is my solution:


set uniqueName to createUniqueFileName((choose folder) as text, "myWallpaperScript", ".scpt")

on createUniqueFileName(fileLocation, fileName, fileExtension)
	try
		alias (fileLocation & fileName & fileExtension)
		set i to 1
		repeat
			try
				alias (fileLocation & fileName & space & i & fileExtension)
				set i to i + 1
			on error
				return (fileName & space & i & fileExtension)
			end try
		end repeat
	on error
		return (fileName & fileExtension)
	end try
end createUniqueFileName