Reputation: 29
I need to select a bunch of files with different names from a folder. This is the script I've got to work on a small scale so far.
tell application "Finder" to select (every file of target of front window whose
name contains "3123" or name contains "3125" or name contains 3166)
But I need a script that will work on a large scale where I can just enter myValues something similar to
set myValues to {3101, 3102, 3103, 3456, 6789, etc. etc.}
tell application "Finder" to select (every file of target of front window whose
name contains myValues)
But when I use that script it won't select any files when I have more than one number set to myValues. Also if you could show me how to change it to work with text as well that would be awesome.
Thanks for all the help!
Upvotes: 0
Views: 5629
Reputation: 11238
Try:
set myValues to {"3101", "3102", "3103", "3456", "6789"}
tell application "Finder" to set fileList to files of target of front window
set matchedFiles to {}
repeat with aFile in my fileList
repeat with aValue in myValues
tell application "System Events" to if aFile's name contains (contents of aValue) then set end of matchedFiles to (aFile as text)
end repeat
end repeat
tell application "Finder" to select matchedFiles
EDIT - added Finder window and regulus6633's suggestion:
set myValues to {"3101", "3102", "3103", "3456", "6789"}
tell application "Finder" to set fileList to files of target of front Finder window as alias list
set matchedFiles to {}
repeat with aFile in my fileList
repeat with aValue in myValues
tell application "System Events" to if aFile's name contains (contents of aValue) then set end of matchedFiles to (aFile as text)
end repeat
end repeat
tell application "Finder" to select matchedFiles
EDIT 2
set myValues to {"3125", "3123"}
tell application "Finder" to set fileList to files of target of front Finder window as alias list
set matchedFiles to {}
repeat with aFile in my fileList
repeat with aValue in myValues
tell application "System Events" to if aFile's name contains (contents of aValue) then set end of matchedFiles to (aFile as text)
end repeat
end repeat
if matchedFiles ≠ {} then
tell application "Finder"
select matchedFiles -- you don't need to select files to duplicate them
duplicate matchedFiles to (choose folder)
end tell
end if
Upvotes: 2