Find a specific file with automator The basic question is:


  How can I pass a filename to the Spotlight search module in Automator?


The essential problem:

I have hundreds of broken aliases scattered throughout a particular directory on my HD. The original files DO STILL EXIST, but for some reason the aliases have lost their connection.  I would like to replace them all with symlinks, so that they can be backed up by Rsync.

Here is a scaled-down example of a folder with broken aliases in it:



Now here is an example of a sub-directory that contains both the originals of some of the broken aliases, as well as additional copies of the SAME alias, that still point to another location for their originals:



And lastly, here is an image of a directory that contains the master files to some of the aliases that were located in both of the previous 2 locations:



So what I need to do is:


Identify all aliases in a user-defined directory 
Use the filenames of those aliases to locate their original files, in a second user-defined directory (recursively and ensuring not to identify a second alias as an original) 
remove the alias
create a symlink to the original file, in the former location of the alias


I'm using Automator wherever I can, and Applescript wherever I must.  Prior to the response that prompted these edits, my workflow did the following:


Prompt for the location to look for original files
Prompt for location to look for aliases
Get POSIX paths to all aliases
Get text filenames of all the aliases


Now, it gets a bit further but with some problems:


I had an alias titled "3dfx &amp; ambience.rfl" (changed already in the screenshots to "3dfx and ambience.rfl").  The applescript provided would fail on that file due to the ampersand.
After fixing #1, the script ran and did indeed remove broken aliases and replace them with symlinks.  But the symlinks pointed to the parent folder that contained the originals, not the originals themselves.  So I ended up with 2 symlinks, "Luftrum 1 - Thor Ambient.rfl" and "Luftrum 2 - Thor Ambient II.rfl", that in reality pointed to the "Luftrum" folder shown in my third screenshot above.


Hopefully this will be enough detail to ultimately arrive at a working solution.  I can tell it's very close.
Usually "broken alias" refers to an alias where the original file no longer exists. I have used this script to find aliases like that:

set l to {}
tell application "Finder"
    try
        alias files of entire contents of (POSIX file "/Users/username" as alias)
        result as list
    on error
        return
    end try
    repeat with f in result
        try
            original item of contents of f
        on error
            --move contents of f to trash
            set end of l to POSIX path of (f as text)
        end try
    end repeat
end tell
set text item delimiters to linefeed
l as text


If the script finds the aliases you want to replace symlinks, save the relevant lines of the output to a text file. Then try running a shell script like this:

cat /tmp/aliases.txt | while IFS= read -r f; do
  original=$(find ~/directory/with/original/files -name "${f##*/}" | head -n1)
  echo "$f:$original"
  # rm "$f"; ln -s "$original" "$f"
done