This discussion on find
started out of a particular fondness for it, particularly in contrast to ls
, which I used on the OP's original problem. And I quite agree: find
is really the right tool to use for most file-getting jobs, particularly if you plan on using the output from it to pipe through into subsequent commands. You can do this with ls
too, but it's not advised because, in theory, the output is less structured and not designed for other commands to read.
But, generalising this discussion, which you may wish to move to separate thread, you can use either plain AppleScript or AppleScript-ObjC to get file and package lists without package contents.
You probably know how to do this in vanilla AppleScript, using either Finder or System Events. The problem with AppleScript is not being able to recurse through a directory tree very easily.
ASObjC can do this, and can do it very fast, and can do it without descending into packages:
use framework "Foundation"
property NSDirectoryEnumerationSkipsPackageDescendants : a reference to 2
property NSDirectoryEnumerationSkipsHiddenFiles : a reference to 4
set d to "/Applications" -- Directory to recurse
set FileManager to current application's NSFileManager's defaultManager()
FileManager's enumeratorAtURL:(current application's NSURL's URLWithString:d) ¬
includingPropertiesForKeys:{} ¬
options:(NSDirectoryEnumerationSkipsPackageDescendants + ¬
NSDirectoryEnumerationSkipsHiddenFiles) ¬
errorHandler:(missing value)
result's allObjects() as list
As you can see, there are specific options one can set to ignore hidden files and package contents. There are a million other options too. This returns a list of AppleScript file
objects.
If you just want to do a shallow search of the directory, then you can do this:
(FileManager's contentsOfDirectoryAtPath:d |error|:(missing value)) as list
which returns a list of filenames (with extensions) as strings.
Returning to the deep directory search results, the object initially returned (before requesting allObjects()
) is an object enumerator that allows you to iterator through each object and identify those of relevance versus those that are not. Sadly, there's no ASObjC one-liner equivalent of the AppleScript whose
filter, so these object enumerators have to be iterated through manually to read an object's properties.
set f to {}
set fs to FileManager's enumeratorAtURL:(current application's NSURL's URLWithString:d) ¬
includingPropertiesForKeys:{} ¬
options:(NSDirectoryEnumerationSkipsPackageDescendants + ¬
NSDirectoryEnumerationSkipsHiddenFiles) ¬
errorHandler:(missing value)
tell fs to repeat
set [isScriptable, fname] to [null, null]
tell its nextObject()
if it is missing value then exit repeat
set [true, isScriptable] to ¬
(its getResourceValue:(reference) ¬
forKey:(current application's NSURLApplicationIsScriptableKey) ¬
|error|:(missing value))
if (isScriptable as anything) = true then set [true, fname] to ¬
(its getResourceValue:(reference) ¬
forKey:(current application's NSURLNameKey) ¬
|error|:(missing value))
set end of f to fname as anything
end tell
end repeat
text of f
This returns a list of all the apps in the /Applications
directory that are scriptable. Each property is read using getResourceValue
, and there are properties that tell you whether a file is a regular file, a directory, a package file, an alias (symlink), etc.