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

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