Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator/Applescript: Copy Files to Folder from a list (text file)

Hi all. I have a folder of files, and a list (text file) of ones I need from that folder. I'm trying to make an Automator application that will read the list, find the files, and copy them to a new folder. I found this previous post (https://discussions.apple.com/thread/7928846) and tried both the method in that post as well as the method referenced in the first link. In both cases, Finder times out before it finds and copies any files, and then it has to be relaunched. I've tested with just one name in the text file, which I've confirmed is in the folder, and it still won't work. I've posted the AppleScript I'm using below. Any suggestions?


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 err msgnumber 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

Posted on Aug 18, 2021 7:02 AM

Reply
Question marked as Best reply

Posted on Aug 18, 2021 8:19 AM

The script that I wrote expects a file containing a list of just filenames, not their full path. It also was written to recursively drill into the selected folder and subfolders within it. If you have other folders in the starting folder that you do not want to be processed, then change:


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

to

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


And, if you have thousands of files that cause AppleScript to time out, then there is a fix for that too:


-- give Finder 30 minutes or less to finish
with timeout of 1800 seconds
	tell application "Finder"
		-- get every filename in selected folder whose filename matches names in name list
		set match_list to (every item 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
end timeout


Similar questions

4 replies
Question marked as Best reply

Aug 18, 2021 8:19 AM in response to Coquito

The script that I wrote expects a file containing a list of just filenames, not their full path. It also was written to recursively drill into the selected folder and subfolders within it. If you have other folders in the starting folder that you do not want to be processed, then change:


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

to

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


And, if you have thousands of files that cause AppleScript to time out, then there is a fix for that too:


-- give Finder 30 minutes or less to finish
with timeout of 1800 seconds
	tell application "Finder"
		-- get every filename in selected folder whose filename matches names in name list
		set match_list to (every item 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
end timeout


Aug 18, 2021 8:28 AM in response to VikingOSX

hey Viking (i didn't expect, after all this time, for you to still be commenting. Thank you). I am just using filenames, and while I started with a set of nested folders, I eventually moved every file (yes, thousands) into just one folder. What I don't understand about the time out is that even with just one item in the list it was timing out. When I type the name into Spotlight for example, it comes up instantly. It's hard for me to understand it would take more than 2 minutes (I think that's the default for time out) to find one file.

Aug 18, 2021 8:49 AM in response to Coquito

Look at that Finder clause: (every item in folder whose name is in name_list).


AppleScript will literally look at every item as it is told before it will turn the result of your single file. That is the big drawback of AppleScript sniffing in a folder with thousands of files. It is not using Spotlight behind the scenes to shortcut this process.


If the script were written as a zsh shell script, then the files in the chosen directory and those of your filename list would be treated as arrays and one simply has to perform an array difference in one command line to return the files in the folder that match. Results in a fraction of a second instead of the AppleScript delay.

Automator/Applescript: Copy Files to Folder from a list (text file)

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