Here’s a reworking which combines the two handlers above, taking “files”, “folders”, or “items” (or their singulars) as an additional parameter. It also dispenses with the resolving of alias files and tidies up some nomenclature confusion in the variable names and comments. One thing to note about the predicate parameter is that it’s case-sensitive with regard to string values. If you need case-insensitivity, put “[c]” after the operator:
[format]“lastPathComponent BEGINSWITH[c] ‘F’”[/format]
(*
Based upon http://macscripter.net/viewtopic.php?id=44806&p=2
*)
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
on getNamesInFolder(aliasOrFile, itemType, theFormat)
set regularFileKey to current application's NSURLIsRegularFileKey
set packageKey to current application's NSURLIsPackageKey
-- Get NSURLs for all the visible items in the folder.
set fileManager to current application's class "NSFileManager"'s defaultManager()
set theNSURLs to (fileManager's contentsOfDirectoryAtURL:(aliasOrFile) includingPropertiesForKeys:({regularFileKey, packageKey}) options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value))
set theNSURLs to theNSURLs's filteredArrayUsingPredicate:(current application's class "NSPredicate"'s predicateWithFormat:theFormat)
-- Return all the items' names if we're simply looking for "items".
if (itemType begins with "item") then return (theNSURLs's valueForKey:("lastPathComponent")) as list
-- Otherwise we need to differentiate between folders and files. Initialise an AS list to store the names.
script o
property nameList : {}
end script
set lookingForFiles to (itemType begins with "file") -- true if files wanted, false if folders.
repeat with anNSURL in theNSURLs
-- Test filehood and packagehood (!).
set fileOrPackageResults to (anNSURL's resourceValuesForKeys:({regularFileKey, packageKey}) |error|:(missing value))
-- If no problem and either (we're looking for files and this item's either a file or a package) or (we're not looking for files and this item's neither a file nor package) then store this item's name.
if ((fileOrPackageResults is not missing value) and ((lookingForFiles) = ((fileOrPackageResults's containsObject:(true)) as boolean))) then set end of o's nameList to (anNSURL's lastPathComponent) as text
end repeat
return o's nameList
end getNamesInFolder
#=====
set f to choose folder
# Builds a list of folderNames which doesn't start with a space char
set folderNames to getNamesInFolder(f, "folders", "NOT(lastPathComponent BEGINSWITH ' ')")
# Builds a list of fileNames starting with the digit 2
set fileNames to getNamesInFolder(f, "files", "lastPathComponent BEGINSWITH '2'")
Another approach, perhaps easier to use if you’re unfamiliar with ASObjC predicate formats, would be this:
(* Parameters:
f: alias, bookmark, or Finder reference to a folder.
itemType: one of these strings: "files", "folders", or "items".
propertyKey: string representation of a property label, eg. "name", "name extension", "size".
comparisonOperator: string representation of a comparison operation, eg. "is", ">", "begins with".
comparisonValue: the value against which to compare the items (not a string representation thereof).
*)
on getNamesInFolder(f, itemType, propertyKey, comparisonOperator, comparisonValue)
if (propertyKey is "name") then
set FinderStuff to "tell app \"Finder\"
set o's allNames to name of " & itemType & " of f
set o's propertyValues to o's allNames
end"
else
set FinderStuff to "tell app \"Finder\" to set {o's allNames, o's propertyValues} to {name, " & propertyKey & "} of " & itemType & " of f"
end if
run script "on run {f, comparisonValue}
script o
prop allNames: missing value
prop propertyValues: missing value
prop nameList: {}
end
" & FinderStuff & "
repeat with i from 1 to (count o's propertyValues)
set thisValue to item i of o's propertyValues
if (thisValue " & comparisonOperator & " comparisonValue) then set end of o's nameList to item i of o's allNames
end
return o's nameList
end" with parameters {f, comparisonValue}
end getNamesInFolder
-- Examples:
set f to (choose folder)
set folderNames to getNamesInFolder(f, "folders", "name", "does not begin with", " ")
set fileNames to getNamesInFolder(f, "files", "name", "begins with", "2")
set itemNames to getNamesInFolder(f, "items", "size", ">", 16384)