"Find" CLI to list all visible files on a disc

Hi Tempel: Basically, the find and the Pass Results worked. However, I couldn’t manage to filter things like folder lines and package contents. An Ends With could be useful in addition to the Begins With, but I don’t think it can currently give me the one normal visible file per line output I was seeking. Thanks!

Peavine, your script outputs exactly what I was looking for: one normal visible full file path per line. It’s fast too, taking just a few seconds for around 600,000 files. I’m going to use it to populate Filemaker databases.
Note: I must have googled and tried dozens of scripts, but they all failed or I couldn’t construct the output criteria correctly. I eventually had almost given up and I used my Livecode license to make a little stack, which worked fine. But I think it’s essential to have this script in my repertoire, even if I don’t understand ASObjC! So thanks thanks thanks!

2 Likes

Peavine, I’ve tried your script and is does in fact work perfectly, with one small issue. The output text file does not list in alphabetical order. Any chance that feature can be added?

I see you got your task resolved with Peavine’s help, but I’d still like to make sure that FAF can handle special searches like yours, too.
Would you be willing to help me improve FAF in this regard (I’d like to understand better what didn’t work and make sure you didn’t miss an option that would’ve solved it)? Then please contact me here with a PM.
Thanks!

Sure. Here is an example of the desired output:

/Volumes/2GB_230515/Fonts/fonts-id/Cabrito-ConBol.otf
/Volumes/2GB_230515/Fonts/fonts-id/Cabrito-ConBolIt.otf
/Volumes/2GB_230515/Fonts/fonts-id/Cabrito-ConBoo.otf
/Volumes/2GB_230515/Fonts/fonts-id/Cabrito-ConBooIt.otf
/Volumes/2GB_230515/Fonts/fonts-id/Cabrito-ConDem.otf
/Volumes/2GB_230515/Fonts/fonts-id/Cabrito-ConDemIt.otf
/Volumes/2GB_230515/Fonts/fonts-id/Cabrito-ConExB.otf
/Volumes/2GB_230515/Fonts/fonts-id/Cabrito-ConExBIt.otf
/Volumes/2GB_230515/Fonts/fonts-id/Cabrito-ConLig.otf
/Volumes/2GB_230515/Fonts/fonts-id/Cabrito-ConLigIt.otf
/Volumes/2GB_230515/Fonts/fonts-id/Cabrito-ConMed.otf
/Volumes/2GB_230515/Fonts/fonts-id/Cabrito-ConMedIt.otf
/Volumes/2GB_230515/Fonts/fonts-id/Cabrito-ConReg.otf
/Volumes/2GB_230515/Fonts/fonts-id/Cabrito-ConRegIt.otf
/Volumes/2GB_230515/Fonts/fonts-id/Cabrito-ConThi.otf
/Volumes/2GB_230515/Fonts/fonts-id/Cabrito-ConThiIt.otf

It’s very very simple. :slight_smile: An Ends With would be useful for folder elimination, and suppression of package items somehow. Thanks!

There’s a “Name ends with” rule you can add. Or the newer “Extension is” rule. And there’s also “Kind is not Directory”, or the hidden (“expert”, see manual) rule “Is Folder No”.
Then there’s the rule “Package Items are not shown”, though that may indeed not have the desired effect when saving the results to a file (meaning that they’ll end up in the file regardless because this option is only filtering them out in the results window, even though they’re still part of the “found” set - that’s something I could fix, indeed).
Give these rules a try if you have the time and let me know if that gives you better results.

Offtopic: if you like to make paragraphs you need to use double linefeeds, I think :slight_smile:

Homer712. I’ve modified my script to sort the output text in alphabetical order. I’ve also changed some variable names, since this and my previous script work with both volumes and folders. The sort selector used works in the same fashion as the Finder (it is case insensitive and numbers are sorted numerically instead of lexically). This is easily changed.

use framework "Foundation"
use scripting additions

set theFolder to "/Users/Robert/Documents/" -- set to desired value
set textFile to POSIX path of (path to desktop) & "All Files.txt"
writeFiles(theFolder, textFile)

on writeFiles(theFolder, textFile)
	set fileManager to current application's NSFileManager's defaultManager()
	set theFolder to current application's |NSURL|'s fileURLWithPath:theFolder
	set fileKey to current application's NSURLIsRegularFileKey
	set theFiles to (fileManager's enumeratorAtURL:theFolder includingPropertiesForKeys:{} options:6 errorHandler:(missing value))'s allObjects()'s mutableCopy()
	repeat with i from theFiles's |count|() to 1 by -1
		set {theResult, aRegularFile} to ((theFiles's objectAtIndex:(i - 1))'s getResourceValue:(reference) forKey:fileKey |error|:(missing value))
		if aRegularFile as boolean is false then (theFiles's removeObjectAtIndex:(i - 1))
	end repeat
	set sortedFiles to (theFiles's valueForKey:"path")'s sortedArrayUsingSelector:"localizedStandardCompare:"
	set theString to sortedFiles's componentsJoinedByString:linefeed
	theString's writeToFile:textFile atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)
end writeFiles

Not only does the script work perfectly, it is incredibly fast. The file seems to pop up on the desktop just as soon as the run arrow is clicked. Thank you, I can see many uses for this kind of script. Now, I’m not asking (because I’d like to see if I can figure it out myself), but I’m going to try to have the output be a PDF. I asked you once in another thread and I’ll look at that one where you went from TXT to PDF.

After three cups of coffee I have a working txt to pdf conversion. Not very polished, but it works.

use framework "Foundation"
use scripting additions

set theFolder to "/Users/homer/Documents/" -- set to desired value
set textFile to POSIX path of (path to desktop) & "All Files.txt"
writeFiles(theFolder, textFile)

on writeFiles(theFolder, textFile)
	set fileManager to current application's NSFileManager's defaultManager()
	set theFolder to current application's |NSURL|'s fileURLWithPath:theFolder
	set fileKey to current application's NSURLIsRegularFileKey
	set theFiles to (fileManager's enumeratorAtURL:theFolder includingPropertiesForKeys:{} options:6 errorHandler:(missing value))'s allObjects()'s mutableCopy()
	repeat with i from theFiles's |count|() to 1 by -1
		set {theResult, aRegularFile} to ((theFiles's objectAtIndex:(i - 1))'s getResourceValue:(reference) forKey:fileKey |error|:(missing value))
		if aRegularFile as boolean is false then (theFiles's removeObjectAtIndex:(i - 1))
	end repeat
	set sortedFiles to (theFiles's valueForKey:"path")'s sortedArrayUsingSelector:"localizedStandardCompare:"
	set theString to sortedFiles's componentsJoinedByString:linefeed
	theString's writeToFile:textFile atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)
end writeFiles

tell application "TextEdit"
	activate
	open "/Users/homer/Desktop/All Files.txt/"
	tell (windows whose id is not (get id of front window) and visible is true)
		set miniaturized to true
	end tell
	set bounds of front window to {279, 111, 1180, 719}
	
	tell application "System Events"
		click menu item "Export as PDF…" of menu 1 of menu bar item "File" of menu bar 1 of application process "TextEdit"
	end tell
end tell

Homer712. I looked at your script and the approach seems sound, but it didn’t work as expected on my Sonoma computer. The following did work, although my GUI scripting skills are poor and there may be a better way to do this.

use framework "Foundation"
use scripting additions

set theFolder to "/Users/Robert/Documents/" -- set to desired value
set posixTextFile to POSIX path of (path to desktop) & "All Files.txt" -- for the writeFiles handler
set hfsTextFile to (path to desktop as text) & "All Files.txt" -- for TextEdit
writeFiles(theFolder, posixTextFile)
delay 1 -- a repeat loop that checks the existence of the text file would be better

tell application "TextEdit"
	activate
	open file hfsTextFile
	tell (windows whose id is not (get id of front window) and visible is true)
		set miniaturized to true
	end tell
	set bounds of front window to {279, 111, 1180, 719}
end tell
delay 0.5 -- test different values

tell application "System Events" to tell process "TextEdit"
	click menu item "Export as PDF…" of menu "File" of menu bar 1
	delay 0.2
	click button "Save" of sheet 1 of window 1 -- if desired
end tell

on writeFiles(theFolder, textFile)
	set fileManager to current application's NSFileManager's defaultManager()
	set theFolder to current application's |NSURL|'s fileURLWithPath:theFolder
	set fileKey to current application's NSURLIsRegularFileKey
	set theFiles to (fileManager's enumeratorAtURL:theFolder includingPropertiesForKeys:{} options:6 errorHandler:(missing value))'s allObjects()'s mutableCopy()
	repeat with i from theFiles's |count|() to 1 by -1
		set {theResult, aRegularFile} to ((theFiles's objectAtIndex:(i - 1))'s getResourceValue:(reference) forKey:fileKey |error|:(missing value))
		if aRegularFile as boolean is false then (theFiles's removeObjectAtIndex:(i - 1))
	end repeat
	set sortedFiles to (theFiles's valueForKey:"path")'s sortedArrayUsingSelector:"localizedStandardCompare:"
	set theString to sortedFiles's componentsJoinedByString:linefeed
	theString's writeToFile:textFile atomically:true encoding:(current application's NSUTF8StringEncoding) |error|:(missing value)
end writeFiles

You used it in post 24 of this discussion; Help Learning AppleScript

Just for fun, I created a script using “System Events”.
See if it is faster than the “Finder” version.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

property excluded : {"iSCPreboot", "xarts", "PreBoot", "VM", "Update", "home"}
property fileList : {}

on run
	local myDisks, i, t, tid
	tell application "System Events"
		set t to name of disks
	end tell
	set myDisks to {}
	repeat with i in t
		set i to contents of i
		if i is not in excluded then set end of myDisks to i
	end repeat
	set t to choose from list myDisks with title "Export File List of Disk…" with prompt "Choose an External Disk…"
	if class of t is boolean then return
	tell application "System Events" to set t to disk (item 1 of t)
	getitems(t)
	try
		set t to open for access file ((path to desktop folder as text) & "All Files.txt") with write permission
	on error errMsg
		display dialog errMsg giving up after 10
		return
	end try
	set tid to text item delimiters
	set text item delimiters to linefeed
	write (fileList as text) to t as text
	close access t
	set text item delimiters to tid
end run

on getitems(diskItem)
	local myFiles, myFolders, i
	tell application "System Events"
		set myFiles to POSIX path of (files of diskItem whose visible is true)
		set myFolders to POSIX path of (folders of diskItem whose visible is true)
	end tell
	repeat with i in myFiles
		set i to contents of i
		set end of my fileList to i
	end repeat
	repeat with i in myFolders
		set i to contents of i
		set end of my fileList to i
		tell application "System Events" to set i to folder i
		getitems(i)
	end repeat
end getitems

** EDIT ** - added “iSCPreboot” and “xarts” to the excluded list

Here’s the popup window to select the disk.

Screenshot

This command can be used in shell script to convert text to PDF:

cupsfilter test.txt > test.pdf

Add iSCPreboot and xarts to the excluded list

Here’s what worked for me, and very nicely. Peavine’s script in post 18, and the leo_r hint on using cupsfilter.

In Automator (Run Applescript) I ran the script and added a 2 second delay at the end (just adds a nice visual). Then I added (Run Shell Script) with the following: cupsfilter “/Users/homer/Desktop/All Files.txt” > “/Users/homer/Desktop/All Files.pdf”.

Works like a charm, thanks all.

1 Like

Thanks Homer712. I don’t know how I missed that. :frowning: I’m glad you found good solution.

P.S. Just thought I’d mention that the cupsfilter command is officially deprecated (even though it’s still working). Just in case it stops working for you at some point.

For more info check its man page, here’s from the NOTES:

CUPS printer drivers, filters, and backends are deprecated and will no longer be supported in a future feature release of CUPS.

Thanks, I’m still very new at all of this, so other than it worked, I knew nothing about CUPS. Did some reading (Find Any File found over 200 files on my MacBook Pro when I searched for “cups” and the HTML files were readable).

Thanks again, pretty interesting stuff.

1 Like