The trouble with your return line above, however, is that symbolic links seem to be a subset of aliases, so your sum counts symbolic links twice.
If you want to know the number of “real” alias files, you should subtract the number of symbolic links from the number of aliases …
I got curious when the respective item counts in my test run where 90223 and 90288, astonishingly. And, BTW, my ~/Library is not hidden, so it got included here (as well as a folder of old SDKs which use symbolic links a lot).
Ah. Thanks. peavine’s original doesn’t count the aliases. I just stuck them in.
Thanks red_menace and pjh for the information on the ~/Library folder. I learned something new.
I always view hidden files by way of the Shift-Command-. (aka Shift-Command-Period) keyboard shortcut, and when I do that hidden files are not returned by the enumeratorAtURL method with the option enabled to skip hidden files.
However, when the Show Library Folder option in the Finder Show View Options pane is checked, the contents of the Library folder are returned. I guess this makes sense, because the Library folder is no longer hidden. Anyways, I agree that when discussing file count of the Home folder it’s probably a good idea to note whether the Library folder is visible or not
BTW, I set the Library folder to be visible and reran my timing tests on my script and Nigel’s most recent script. Both scripts returned 62,288 items. Nigel’s script took 3.038 seconds and my script took 3.782 seconds. The speed advantage of Nigel’s script is almost 20 percent, which is the same figure I obtained with the Library folder not visible.
This thread has given me a massive performance boost from my previous methods! Big thanks to all especially @peavine for your thoroughness on this topic! Incorporating peavine’s methods and Nigels dictionary revisions ( #4) I made the following subroutines.
These subroutines receive an array of NSURLs and return a filtered array of NSURLs. Demo code returns these as lists for Script Editor compatibility.
NSURL_Filter_Aliases_Of(NSURLArray) returns NSURLs of aliases only. NSURL_Filter_Files_Of(NSURLArray) returns NSURLs of files only. NSURL_Filter_Folders_Of(NSURLArray) returns NSURLs of folders only. NSURL_Filter_Packages_Of(NSURLArray) returns NSURLs of packages only. NSURL_Filter_SymbolicLinks_Of(NSURLArray) returns NSURLs of symbolicLinks only.
--5/26/26 https://www.macscripter.net/u/paulskinner/
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
set fileSystemObject to current application's NSHomeDirectory()
set fileSystemObject to current application's class "NSURL"'s fileURLWithPath:fileSystemObject
set NSURLArray to ((current application's NSFileManager's defaultManager's enumeratorAtURL:fileSystemObject includingPropertiesForKeys:{} options:6 errorHandler:(missing value))'s allObjects())'s mutableCopy()
set aliasList to NSURL_Filter_Aliases_Of(NSURLArray)
set fileList to NSURL_Filter_Files_Of(NSURLArray)
set foldersList to NSURL_Filter_Folders_Of(NSURLArray)
set packagesList to NSURL_Filter_Packages_Of(NSURLArray)
set symbolicLinksList to NSURL_Filter_SymbolicLinks_Of(NSURLArray)
return {aliasList as list, fileList as list, foldersList as list, packagesList as list, symbolicLinksList as list}
on NSURL_Filter_Aliases_Of(NSURLArray)
--Receiving an array of NSURLs, returns the NSURLs of aliases only.
--https://www.macscripter.net/u/peavine https://www.macscripter.net/u/Nigel_Garvey https://www.macscripter.net/t/resource-key-for-nsurls/77929/14
--Create array of dictionaries with resource key and URL values
set aliasKey to current application's NSURLIsAliasFileKey
set symbolicLinkKey to current application's NSURLIsSymbolicLinkKey
set theData to NSURLArray's mutableCopy()
repeat with indx from 1 to (count NSURLArray)
set anItem to (NSURLArray's item indx)
set theData's item indx to {URLKey:anItem, keys:(anItem's resourceValuesForKeys:{aliasKey, symbolicLinkKey} |error|:(missing value))}
end repeat
--Aliases including alias files and symbolic links AND NOT symbolic links.
set thePredicate to current application's NSPredicate's predicateWithFormat_("keys.%K == YES AND NOT keys.%K == YES", aliasKey, symbolicLinkKey)
set folderData to theData's filteredArrayUsingPredicate:thePredicate
set filteredArray to (folderData's valueForKey:"URLKey")
return filteredArray
end NSURL_Filter_Aliases_Of
on NSURL_Filter_Files_Of(NSURLArray)
--Receiving an array of NSURLs, returns the NSURLs of regular files only.
--https://www.macscripter.net/u/peavine https://www.macscripter.net/u/Nigel_Garvey https://www.macscripter.net/t/resource-key-for-nsurls/77929/14
--Create array of dictionaries with resource key and URL values
set regularFileKey to current application's NSURLIsRegularFileKey
set aliasKey to current application's NSURLIsAliasFileKey
set theData to NSURLArray's mutableCopy()
repeat with indx from 1 to (count NSURLArray)
set anItem to (NSURLArray's item indx)
set theData's item indx to {URLKey:anItem, keys:(anItem's resourceValuesForKeys:{regularFileKey, aliasKey} |error|:(missing value))}
end repeat
--Regular files including alias files and symbolic links AND NOT alias files and symbolic links
set thePredicate to current application's NSPredicate's predicateWithFormat_("keys.%K == YES AND NOT keys.%K == YES", regularFileKey, aliasKey)
set folderData to theData's filteredArrayUsingPredicate:thePredicate
set filteredArray to (folderData's valueForKey:"URLKey")
return filteredArray
end NSURL_Filter_Files_Of
on NSURL_Filter_Folders_Of(NSURLArray)
--Receiving an array of NSURLs, returns the NSURLs of folders only.
--https://www.macscripter.net/u/peavine https://www.macscripter.net/u/Nigel_Garvey https://www.macscripter.net/t/resource-key-for-nsurls/77929/14
--Create array of dictionaries with resource key and URL values
set folderKey to current application's NSURLIsDirectoryKey
set packageKey to current application's NSURLIsPackageKey
set theData to NSURLArray's mutableCopy()
repeat with indx from 1 to (count NSURLArray)
set anItem to (NSURLArray's item indx)
set theData's item indx to {URLKey:anItem, keys:(anItem's resourceValuesForKeys:{folderKey, packageKey} |error|:(missing value))}
end repeat
--List folders including package files AND NOT package files
set thePredicate to current application's NSPredicate's predicateWithFormat_("keys.%K == YES AND NOT keys.%K == YES", folderKey, packageKey)
set folderData to theData's filteredArrayUsingPredicate:thePredicate
set filteredArray to (folderData's valueForKey:"URLKey")
return filteredArray
end NSURL_Filter_Folders_Of
on NSURL_Filter_Packages_Of(NSURLArray)
--Receiving an array of NSURLs, returns the NSURLs of packages only.
--https://www.macscripter.net/u/peavine https://www.macscripter.net/u/Nigel_Garvey https://www.macscripter.net/t/resource-key-for-nsurls/77929/14
set packageKey to current application's NSURLIsPackageKey --Create array of dictionaries with resource key and URL values
--Create array of dictionaries with resource key and URL values
set theData to NSURLArray's mutableCopy()
repeat with indx from 1 to (count NSURLArray)
set anItem to (NSURLArray's item indx)
set theData's item indx to {URLKey:anItem, keys:(anItem's resourceValuesForKeys:{packageKey} |error|:(missing value))}
end repeat
--List package files
set thePredicate to current application's NSPredicate's predicateWithFormat_("keys.%K == YES", packageKey)
set folderData to theData's filteredArrayUsingPredicate:thePredicate
set filteredArray to (folderData's valueForKey:"URLKey")
return filteredArray
end NSURL_Filter_Packages_Of
on NSURL_Filter_SymbolicLinks_Of(NSURLArray)
--Receiving an array of NSURLs, returns the NSURLs of symbolicLinks only.
--https://www.macscripter.net/u/peavine https://www.macscripter.net/u/Nigel_Garvey https://www.macscripter.net/t/resource-key-for-nsurls/77929/14
set symbolicLinkKey to current application's NSURLIsSymbolicLinkKey --Create array of dictionaries with resource key and URL values
--Create array of dictionaries with resource key and URL values
set theData to NSURLArray's mutableCopy()
repeat with indx from 1 to (count NSURLArray)
set anItem to (NSURLArray's item indx)
set theData's item indx to {URLKey:anItem, keys:(anItem's resourceValuesForKeys:{symbolicLinkKey} |error|:(missing value))}
end repeat
--List symbolicLink files
set thePredicate to current application's NSPredicate's predicateWithFormat_("keys.%K == YES", symbolicLinkKey)
set folderData to theData's filteredArrayUsingPredicate:thePredicate
set filteredArray to (folderData's valueForKey:"URLKey")
return filteredArray
end NSURL_Filter_SymbolicLinks_Of
Timing here is in a different range from the milliseconds I see @peavine post, but I have a very large home folder. Still, massive speedup from previous methods. List and filter any one filetype ~ 65 s.
List contents
322687 items
10 s
item 1 - aliasList
list of 5 items
54 s
item 2 - fileList
list of 269909 items
55 s
item 3 - foldersList
list of 52568 items
54 s
item 4 - packagesList
list of 201 items
53 s
item 5 - symbolicLinksList
list of 3
52 s
convert all values to list - Script Debugger timing does not update.
~120 s
Paul. I tested your script and it works great. The construction of the predicate that is used to get one item but not another (e.g. folders but not packages) is ingenious. Thanks for the script.

