AppleScript to find files by exact name match from an Automator variable
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: "2.7", system version: "10.13.5"
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 "Foundation"
property ca : a reference to current application
on run {input, parameters}
set MusicFolder to "/Volumes/Media/Music/iTunes/iTunes Music/Music/"
set FileManager to ca's NSFileManager's defaultManager()
set MusicFiles to (FileManager's subpathsAtPath:MusicFolder)'s ¬
pathsMatchingExtensions:{"mp3", "m4a"}
set [filename] to input
set format to "SELF endswith " & ("/" & filename & ".mp3")'s quoted form & ¬
" OR SELF endswith " & ("/" & filename & ".m4a")'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 & 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 "Everywhere I Go"
(i.e. without the preceding "04"
), 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 "01 Song.mp3"
, both files will be returned in the search.