Using a file as source for a new files using names in existing folder

I have a number of folders, each with a lot of files. I want to keep each folder’s respective filenames, but have each file’s contents replaced by a smaller dummy file. (Allows me to have a residual file system that mimics the original, for some testing purposes.) So, for argument’s sake, let’s say I have a file on my Macintosh HD called Dummy.txt, a folder on Macintosh HD called FileSource that contains my original documents of various sizes, and an empty folder on Macintosh HD called FileTarget in which I want to create my copies of the files in FileSource using the content in Dummy.txt.

Sounds simple enough. Use an on open, drop the FileSource folder on it, get the names of the files in FileSource, and for each of these names, duplicate Dummy.txt to FileTarget, assigning name from FileSource.

Well, newbie that I am, I can’t even get my “on open” to give me a list of files. When I do this:

on open theFolder
display dialog "theFolder: " & theFolder
set the_files to contents of theFolder
repeat with an_item in the_files
display dialog "an_item: " & an_item

I simply get “Macintosh HD:FileSource:” as both theFolder and an_item. How do I get the file list?

Hi, dlminehart.

The parameter for ‘on open’ is a list of one or more aliases. The ‘contents’ of this list are the same aliases.

-- The 'open' parameter is a *list* of aliases to the dropped items.
-- Using braces round the variable name limits the variable to the first item in the list.
on open {theFolder}
	-- Check that 'theFolder' is indeed a folder.
	set {folder:isFolder, package folder:isPackage, name:folderName} to (info for theFolder)
	if ((not isFolder) or (isPackage)) then
		display dialog "Item " & folderName & " is not a folder." buttons {"Cancel"} default button 1 with icon stop
		error number -128
	end if
	
	tell application "Finder"
		-- Check that the text file and destination folder exist.
		try
			set dummyFile to file "Dummy.txt" of startup disk
			set destfolder to folder "FileTarget" of startup disk
		on error
			display dialog "Folder FileTarget wasn't found on the startup disk." buttons {"Cancel"} default button 1 with icon stop
			error number -128
		end try
		
		-- Duplicate the dummy file using the names of the dropped folder's contents.
		set theNames to name of every item of theFolder
		repeat with thisName in theNames
			set thisDupe to (duplicate dummyFile to destfolder)
			set thisDupe's name to thisName's contents
		end repeat
	end tell
end open