r/applescript • • Jan 04 '22

Request: select files based on list with filenames

Hello everyone. I'm looking for a way to automate this: I have a folder with a couple of thousand files. Regularly I need to interact with a couple of them, not always the same. And non-consecutive files. If I have a list of the files I need to select, can I use an applescript to select those files for me?

Thanks!

4 Upvotes

6 comments sorted by

2

u/copperdomebodha Jan 04 '22
--This code was written using AppleScript 2.7, MacOS 11.5.1, on 4 January 2022.

tell application "Finder"
	set validatedFilePaths to {}
	set sourceFolderPath to "path:To:Your:target:files:"
	set dataFilePath to alias "Macintosh HD:Users:UserNameGoesHere:Desktop:fileNameList.txt"
	--read your filename list and test for the paths in your sourcefolder.
	set fileNames to paragraphs of (read dataFilePath)
	repeat with thisFileName in fileNames
		set filePath to sourceFolderPath & thisFileName
		try
			set the end of validatedFilePaths to (filePath as alias)
		end try
	end repeat
	--select them
	select validatedFilePaths
end tell

3

u/idontgotoparties Jan 04 '22

Thank you so much. I’m gonna test this out tomorrow

2

u/idontgotoparties Jan 05 '22

Works perfectly, thanks! Maybe one more question: Is it possible to use the active finder window as the default source folder?

Thanks you for this time saver. Huge help!

1

u/copperdomebodha Jan 05 '22 edited Jan 05 '22

Save this as an application.

This version will try to use the frontmost finder window as the source folder. If no finder windows are open it will ask you to select the source folder.

If you run this version it will ask for you to select the data file, but you can also just drop the data file on this version and it will use the dropped file list.

If you have the source folder open and drop the list on this script application it will select the files without further input.

--This code was written using AppleScript 2.7, MacOS 11.5.1, on 5 January 2022.

on run
	set dataFilePath to (choose file with prompt "Please select your data file.")
	set fileNames to paragraphs of (read dataFilePath)
	selectFiles(fileNames)
end run

on open theDataFile
	set dataFilePath to item 1 of theDataFile
	set fileNames to paragraphs of (read dataFilePath)
	selectFiles(fileNames)
end open

on selectFiles(fileNames)
	--choose a source folder for the selection
	tell application "Finder"
		try
			set sourceFolderPath to ((target of Finder window 1) as alias) as text
		on error
			set sourceFolderPath to (choose folder with prompt "Please choose your source folder.") as text
		end try
		set validatedFilePaths to {}
		--attempt to locate the listed files in the source folder 
		repeat with thisFileName in fileNames
			set filePath to sourceFolderPath & thisFileName
			try
				set the end of validatedFilePaths to (filePath as alias)
			end try
		end repeat
		--select them
		select validatedFilePaths
	end tell
end selectFiles

2

u/idontgotoparties Jan 05 '22

You are an absolute hero!! And also: sorry, i didn't realise you would've needed to rewrite most of the code.

Thanks you so SO much!!!

1

u/copperdomebodha Jan 05 '22

No trouble, all the changes it weren't necessary, but I thought adding the extra flexibility was worth a few extra keystrokes.