Chrizm
Chrizm

Reputation: 11

Applescript, Automator - Find & Copy imgs in Dir/sub-dir

I am looking to make an automator workflow, or applescript (but I am not familiar with the language yet) that can use a list of names (spreadsheet or a .csv) to search a directory and its sub-directories for those specific file names (with varying extensions) and copy that image to a folder created for these images.

I've found a script that seems to be somewhat similar to what I need, but it doesn't search within sub-directories, so I've yet to actually find any images with it.

After extensive research, I've found 2 different scripts that seem to be what I need, but neither of them seem to search sub-directories. Below are the 2 scripts I've tried. If anyone could help me get these to search sub-directories, I would really appreciate it!

Script 1:

set thePhotos to paragraphs of (read (choose file with prompt "Choose a text file"))
set theSourceFolder to (choose folder with prompt "Choose source folder")
set theDestination to (choose folder with prompt "Choose destination folder")
set dupeList to {}
repeat with theName in thePhotos
    try
        set end of dupeList to alias ((theSourceFolder as text) & theName)
    end try
end repeat

tell application "Finder" to duplicate dupeList to theDestination with replacing

set theCount1 to (count of dupeList) as text
set theCount2 to (count of thePhotos) as text
display dialog (theCount1 & " of " & theCount2 & " items copied to " & (theDestination as text)) buttons {"OK"}

Script 2

set fileContents to read (choose file with prompt "Choose a comma-delimited text file")
set theText to result
set AppleScript's text item delimiters to ","
set theTextItems to text items of theText
set AppleScript's text item delimiters to {""}
theTextItems
set theSourceFolder to (choose folder with prompt "Choose source folder") as string
set theDestination to (choose folder with prompt "Choose destination folder")
repeat with theEPSName in theTextItems
    tell application "Finder"
        set theEPSFile to theSourceFolder & theEPSName
        move file theEPSFile to folder theDestination with replacing
    end tell
end repeat

So it lets me choose the .csv to use, choose the directory to save and choose the directory to search. How can I get this script to search within sub-directories? And to someone who understands Applescript, does this look like it will function as needed?

Thank you in advance! I really appreciate any help on this, as it is my first Applescript experience, but I am excited to learn it!

Upvotes: 0

Views: 61

Answers (1)

vadian
vadian

Reputation: 285082

This is a very fast solution using the shell and Spotlight searching.

The script creates a search criteria string

'kMDItemFSName == picture1.png || kMDItemFSName == picture2.jpg || ...'

then performs the search and copies all files by passing the found items to the cp command.

There might be a caveat if the name list is too long and exceeds the maximum length of shell parameters.

set theNames to paragraphs of (read (choose file with prompt "Choose a text file" of type "txt"))
set sourceFolder to quoted form of POSIX path of (choose folder with prompt "Choose source folder")
set destinationFolder to quoted form of POSIX path of (choose folder with prompt "Choose destination folder")

set nameList to {}
repeat with aName in theNames
    set end of nameList to "kMDItemFSName == " & quoted form of aName
end repeat
set {TID, text item delimiters} to {text item delimiters, " || "}
set nameFilter to quoted form of (nameList as text)
set text item delimiters to TID
do shell script "mdfind -onlyin " & sourceFolder & " -0 " & nameFilter & " | xargs -0 -J {} cp {} " & destinationFolder

Upvotes: 0

Related Questions