allow user to choose both folders and files with prompt

I’m new to AppleScript so please forgive me if this is widely known. I couldn’t find an answer despite looking for some time.

I’m trying to get a user selected backup of multiple files to an externally mounted volume. I can get it to mount the volume and save either multiple files or multiple folders to the server based on user selection but I can’t do both at once. The only solution I’ve been able to come up with is very awkward and I’m hoping someone will point me toward something more elegant.

This is the akwared solution I’m currently using.

if button returned of q is “Yes” then
set r to choose folder with multiple selections allowed
set s to choose file with multiple selections allowed
try with timeout of 600 seconds
tell application “Finder”
duplicate r to user_name with replacing
duplicate s to user_name with replacing
display dialog “The files have been saved.”
end tell
end timeout
end try

teacher:

The main issue (it appears to me, anyway) is that you want a single dialog with lists of folders and files from which the user can make their selections for backing up. You are correct, that you cannot do that with the native [choose folder] or [choose file] commands. You have to make your own list for the user by extracting the folders and files, then use the [choose from list] command. Here is an extremely crude example of what I mean, using the documents folder as the example:

tell application "Finder"
	set p_doc to path to documents folder --Just look in this folder
	set a to every folder in folder p_doc --Get all the folders in the Documents folder
	set b to every file in folder p_doc --Get all the files in the Documents folder
end tell
set c to {} --Start with an empty list
repeat with f1 in a
	set end of c to (f1 as Unicode text) --Add all the folders to the list, after converting them to a more readable style.
end repeat
repeat with f2 in b
	set end of c to (f2 as Unicode text) --Add all the files to the list, after converting them to a more readable style.
end repeat
set d to choose from list c with multiple selections allowed --Now, choose from that list
try
	with timeout of 600 seconds
		tell application "Finder"
			repeat with ff in d
				duplicate ff to user_name with replacing
			end repeat
			display dialog "The files have been saved."
		end tell
	end timeout
end try

You can tell the folders from the files in your list (while choosing) because they all end with a colon [:].

The only problem, of course, is that if you have hundreds of files and/or folders in the selected folder (whether or not you use the Documents folder as in my example), this baby will take a long time to run, and the list to choose from may be unwieldy for the users.

Hope this helps

CAS