AppleScript to find files by exact name match from an Automator variable I have a workflow that gets the track name of a single track in my mp3 player and stores it in a variable. I then use this in the Spotlight action to find that track in my iTunes Library, but the Spotlight action gives multiple results when I want only want the exact track not all tracks that contains the name.

I need some way or a script to replace this action. All searches gives Terminal commands that I don't understand or how to use with Automator.



Here's an applescript I found that works but is really really slow, it beach-balls the Finder on every loop, Is there a way to speed this up,my library has over 10,000 tracks? thanks
https://www.dropbox.com/s/o14eb19b2459vdo/by%20default%202018-07-19%20at%20Thursday%2C%2019%20Jul%20%2017.55.18.PNG?dl=0
I don't have Snow Leopard, so I'm limited in what I can test as being viable for your system.  Therefore, please bear in mind that the following solution was devised and tested using the following:
System info: AppleScript version: &quot;2.7&quot;, system version: &quot;10.13.5&quot;
With that said, I see now reason that this shouldn't work on El Capitan.  It might work on Snow Leopard, but I'm less sure.
My proposed solution is to replace your Spotlight Automator action with a Run AppleScript action:

The code contained in the Run AppleScript is as follows:
    use framework &quot;Foundation&quot;
    
    property ca : a reference to current application
    
    
    on run {input, parameters}
        set MusicFolder to &quot;/Volumes/Media/Music/iTunes/iTunes Music/Music/&quot;
        
        set FileManager to ca's NSFileManager's defaultManager()
        set MusicFiles to (FileManager's subpathsAtPath:MusicFolder)'s ¬
            pathsMatchingExtensions:{&quot;mp3&quot;, &quot;m4a&quot;}
        set [filename] to input
        set format to &quot;SELF endswith &quot; &amp; (&quot;/&quot; &amp; filename &amp; &quot;.mp3&quot;)'s quoted form &amp; ¬
            &quot; OR SELF endswith &quot; &amp; (&quot;/&quot; &amp; filename &amp; &quot;.m4a&quot;)'s quoted form
        set filter to ca's NSPredicate's predicateWithFormat:format
        
        set matches to {}
        
        repeat with match in (MusicFiles's filteredArrayUsingPredicate:filter) #'
            set end of matches to POSIX file (MusicFolder &amp; match as text) as alias
        end repeat
        
        return matches
    end run

This will return tracks with the exact filename supplied by the trackName variable in the workflow.  Therefore, had I set trackName in my test run to &quot;Everywhere I Go&quot; (i.e. without the preceding &quot;04&quot;), the search would have returned an empty result.
Also, bear in mind that you may have some tracks that have identical filenames from different folders/albums.  If there are two files in different folders both called &quot;01 Song.mp3&quot;, both files will be returned in the search.