The Tobemonster The Tobemonster - 1 month ago 8
AppleScript Question

Locate a file anywhere on the hard drive and transfer it to a different folder

I'm trying to create an app to transfer any files with a certain name a different folder. So far, I have the following:

tell application "Finder"
move (every item of (get path to home folder) whose name is "extended image name.jpg") to ((get path to home folder) & "Pictures" as string)
end tell

While this doesn't return any errors, it doesn't do what I want it to either. I am also aware that this only searches the home folder, so if there is any way to do a broader search of the whole drive without having to enter a username, that would be great (I want this to be able to run on more computers than one without them having to edit the script.)


Answer Source

The code considers only files in the home folder but not in its subfolders, to consider all subfolders you have to add entire contents

tell application "Finder"
    move (every item of entire contents of home whose name is "extended image name.jpg") to folder "Pictures" of home
end tell

But be aware that entire contents is extremely slow. The shell command find or a spotlight search with mdfind is much faster for example

set homeFolder to POSIX path of (path to home folder)
set picturesFolder to POSIX path of (path to pictures folder)
do shell script "mdfind -onlyin " & quoted form of homeFolder & " -0 'kMDItemDisplayName = \"extended image name.jpg\"' | xargs -0 -J {} mv {} " & quoted form of picturesFolder

Important Note:

As you are moving multiple files with the same name the Finder version will ask for overwriting and the shell version will overwrite all files with the same name.