How to get finder items from a list, automator, including searching through sub-folders

I am able to use the commands already presented in the previous questions (How to get finder items from a list, auto… - Apple Community) for Run AppleScript, but it does not copy the files to the outfolder. Does the list need to be formatted a particular way in the .txt file? I can not figure out what I am doing wrong. Thanks in advance for your help!


iMac 27″

Posted on Jan 26, 2025 11:00 PM

Reply
Question marked as Top-ranking reply

Posted on Jan 27, 2025 4:58 AM

The list of filenames, which the third prompt requests, is just a text file with one filename (and its extension) per line.


I didn't post that source AppleScript as I should have, and the hosting software removed some needed spacing in the code causing it to not compile in Apple's Script Editor. Here is the correct code posting:

property defloc : (path to desktop as text) as alias
property msg1 : "Select the input folder"
property msg2 : "Select the output folder"
property msg3 : "Select the input file with filenames"

use scripting additions

set name_list to {}
set match_list to {}

try
	set infolder to (choose folder with prompt msg1 default location defloc without invisibles, multiple selections allowed and showing package contents)
	set outfolder to (choose folder with prompt msg2 default location defloc without invisibles, multiple selections allowed and showing package contents)
	set name_file to (choose file with prompt msg3 default location defloc without invisibles, multiple selections allowed and showing package contents)
	
	display dialog infolder's POSIX path & return & outfolder's POSIX path & return & name_file's POSIX path
	-- read names from file into quoted filename list
	set name_list to read name_file as «class utf8» using delimiter linefeed
	
	tell application "Finder"
		-- get every filename in infolder hierarchy whose filename matches names in name list
		set match_list to (every item of entire contents of folder infolder whose name is in name_list) as alias list
		
		-- copy the files to outfolder with exact permissions and overwrite if required
		repeat with anItem in match_list
			duplicate anItem to outfolder with replacing and exact copy
		end repeat
	end tell
on error errmsg number errnbr
	my error_handler(errnbr, errmsg)
end try
return

on error_handler(nbr, msg)
	return display alert "[ " & nbr & " ] " & msg as critical giving up after 10
end error_handler


The choice of folders should not be sub-folders within an existing folder, just the parent folder on the Desktop. See if this revised code resolves copying files to that outfolder.

Similar questions

7 replies
Question marked as Top-ranking reply

Jan 27, 2025 4:58 AM in response to photosbyjenniferharding

The list of filenames, which the third prompt requests, is just a text file with one filename (and its extension) per line.


I didn't post that source AppleScript as I should have, and the hosting software removed some needed spacing in the code causing it to not compile in Apple's Script Editor. Here is the correct code posting:

property defloc : (path to desktop as text) as alias
property msg1 : "Select the input folder"
property msg2 : "Select the output folder"
property msg3 : "Select the input file with filenames"

use scripting additions

set name_list to {}
set match_list to {}

try
	set infolder to (choose folder with prompt msg1 default location defloc without invisibles, multiple selections allowed and showing package contents)
	set outfolder to (choose folder with prompt msg2 default location defloc without invisibles, multiple selections allowed and showing package contents)
	set name_file to (choose file with prompt msg3 default location defloc without invisibles, multiple selections allowed and showing package contents)
	
	display dialog infolder's POSIX path & return & outfolder's POSIX path & return & name_file's POSIX path
	-- read names from file into quoted filename list
	set name_list to read name_file as «class utf8» using delimiter linefeed
	
	tell application "Finder"
		-- get every filename in infolder hierarchy whose filename matches names in name list
		set match_list to (every item of entire contents of folder infolder whose name is in name_list) as alias list
		
		-- copy the files to outfolder with exact permissions and overwrite if required
		repeat with anItem in match_list
			duplicate anItem to outfolder with replacing and exact copy
		end repeat
	end tell
on error errmsg number errnbr
	my error_handler(errnbr, errmsg)
end try
return

on error_handler(nbr, msg)
	return display alert "[ " & nbr & " ] " & msg as critical giving up after 10
end error_handler


The choice of folders should not be sub-folders within an existing folder, just the parent folder on the Desktop. See if this revised code resolves copying files to that outfolder.

Feb 4, 2025 11:12 AM in response to photosbyjenniferharding

You are welcome.


The AppleScript Finder construct Is recursive into sub-folders using:

set match_list to (every item of entire contents of folder infolder whose name is in name_list) as alias list


Applescript is a pure dog when the folder hierarchy is deep and > 1000 files/folders to process.


and non-recursive when using this format:

set match_list to (every item of folder infolder whose name is in name_list) as alias list


Jan 28, 2025 8:56 AM in response to VikingOSX

Thank you again for the help and quick response. I selected the parent folder in Dropbox that contains all of the files I am trying to copy, but they are not getting copied into the outfolder. I tried having the outfolder on the desktop and also in Dropbox, but neither one worked. Do the files have to be stored locally on the computer, or is it possible to do if they are in the cloud?

Feb 2, 2025 11:27 AM in response to VikingOSX

Yes, it definitely does complicate it, but I have been able to get your solution to work, partially. I have learned that I can have the files listed in a .txt or a .csv file and both work. Your code to

set match_list to (every item of entire contents of folder infolder whose name is in name_list) as alias list

causes the process to timeout every time. I found your fix to use a timeout of 1800 seconds from another post and

set match_list to (every item of folder infolder whose name is in name_list) as alias list

works, provided the files reside in the parent folder and not a sub-folder. I am able to select a sub-folder as the infolder and the process works as advertised. Unfortunately, with the number of files I am trying to copy to the outfolder and sporadic locations in numerous sub-folders, it would be a pretty tedious task to run the process on every sub-folder. I am still searching for a way to drill into the sub-folders without timing out. Thank you again for your help with this.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to get finder items from a list, automator, including searching through sub-folders

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.