Here is a bit lengthy, pure AppleScript solution. The handler’s input arguments are as follows:
folderHFSPath
- Self-explanatory
fileStubs
- Text string: Files with this file name stub (i.e., file name without extension) will be considered
- List of text strings: Files with these file name stubs will be considered
- {}, "", missing value, or null: All files with the file name stub of files whose extension is in the excluded extensions list will be considered
excludedExtensions
- Text string: Files whose stub is in the file stubs list but without this extension will be returned
- List of text strings: Files whose stub is in the file stubs list but without these extensions will be returned
on getFilesOfFolder:folderHFSPath withFileStubs:fileStubs excludedExtensions:excludedExtensions
-- Embedded script containing a utility handler for the main handler
script util
on componentsForFileName:utilFileName
-- Return the file stub and extension components of the input file name
tell (get utilFileName's text items)
if (its length = 1) or ((its length = 2) and (first item = "")) then
-- Handle file names without an extension and those beginning with a period character but without a file extension
set {utilFileStub, utilFileExtension} to {utilFileName, ""}
else
-- Handle all other file names
set {utilFileStub, utilFileExtension} to {items 1 thru -2 as text, item -1}
end if
end tell
return {utilFileStub, utilFileExtension}
end componentsForFileName:
end script
-- Baseline return values
set {fileNames, filePOSIXPaths, fileHFSPaths} to {{}, {}, {}}
-- Convert an extension coded as a text string into a one-item list
set excludedExtensions to excludedExtensions as list
-- Get the names and POSIX and HFS paths of all files in the input folder
tell application "System Events" to set {allNames, allPOSIXPaths, allHFSPaths} to {name, POSIX path, path} of files of folder folderHFSPath
-- Set the text item delimiters to the period character "." here; it will be used by the utility script "util" to extract a file's stub and extension from its full name
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
try
if {fileStubs} is in {{}, "", missing value, null} then
-- If not provided in the input argument, get the file stubs of those input files whose file extension is one of the excluded file extensions
set fileStubs to {}
repeat with currName in allNames
set {currStub, currExtension} to (util's componentsForFileName:(currName's contents))
if ({currStub} is not in fileStubs) and ({currExtension} is in excludedExtensions) then set end of fileStubs to currStub
end repeat
else
-- Convert a file stub coded as a text string into a one-item list
set fileStubs to fileStubs as list
end if
-- Get the names and POSIX and HFS paths of those input files whose file extension is not one of the excluded file extensions
repeat with i from 1 to allNames's length
set {currName, currPOSIXPath, currHFSPath} to {allNames's item i, allPOSIXPaths's item i, allHFSPaths's item i}
set {currStub, currExtension} to (util's componentsForFileName:currName)
if ({currStub} is in fileStubs) and ({currExtension} is not in excludedExtensions) then set {end of fileNames, end of filePOSIXPaths, end of fileHFSPaths} to {currName, currPOSIXPath, currHFSPath}
end repeat
-- Restore the text item delimiters to their baseline value, then return the results
set AppleScript's text item delimiters to tid
return {fileNames, filePOSIXPaths, fileHFSPaths}
on error m number n
-- If an error is encountered, restore the text item delimiters to their baseline value, then optionally process the error
set AppleScript's text item delimiters to tid
### Error-processing code ###
end try
end getFilesOfFolder:withFileStubs:excludedExtensions:
Examples:
my getFilesOfFolder:(path to desktop as text) withFileStubs:"GoodFileName" excludedExtensions:"meta"
--> Returns all files on the Desktop whose file stub = "GoodFileName" and whose extension ≠ "meta"
my getFilesOfFolder:(path to desktop as text) withFileStubs:{"GoodFileName", "AnotherGoodFileName"} excludedExtensions:{"meta", ".app"}
--> Returns all files on the Desktop whose file stub = "GoodFileName" or "AnotherGoodFileName" and whose extension ≠ "meta" or "app"
my getFilesOfFolder:(path to desktop as text) withFileStubs:null excludedExtensions:{"meta", ".app"}
--> Returns all files on the Desktop whose extension ≠ "meta" or "app", provided that another desktop file with the same file stub does have a "meta" or "app" extension