Last active
October 30, 2023 18:43
-
-
Save macshome/f7a72f170c52fe4cda12078db356f6a9 to your computer and use it in GitHub Desktop.
Paste into a Swift playground to find all the executable binaries and shell scripts on your system with blinding speed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import PlaygroundSupport | |
PlaygroundPage.current.needsIndefiniteExecution = true | |
// Simple class to search for things with `NSMetadataQuery`. | |
class Searcher { | |
// This is just for timing things | |
let startTime = Date() | |
// Setup the bits we need to construct a Spotlight search | |
let metadataSearch = NSMetadataQuery() | |
// The simple predicate here is any sort of executable file' | |
let searchPredicate = NSPredicate(fromMetadataQueryString: "kMDItemContentTypeTree == 'public.executable' || kMDItemContentTypeTree == 'public.shell-script'") | |
// We only need to search the Data volume as we can't install on the system one. | |
let dataVolume = URL(fileURLWithPath: "/System/Volumes/Data") | |
// In init we use the properties we just created. | |
init() { | |
self.metadataSearch.predicate = self.searchPredicate | |
self.metadataSearch.searchScopes = [self.dataVolume] | |
// I'm grouping results here by content type. | |
self.metadataSearch.groupingAttributes = ["kMDItemContentType"] | |
// Metadata Queries are async and we listen for updates. In this case just that the search finished. | |
NotificationCenter.default.addObserver(self, | |
selector: #selector(done), | |
name: .NSMetadataQueryDidFinishGathering, | |
object: self.metadataSearch) | |
} | |
func startSearch() { | |
// Just a wrapper to start the search. | |
metadataSearch.start() | |
} | |
func stopSearch() { | |
// Stop the search and the playground. | |
metadataSearch.stop() | |
PlaygroundPage.current.finishExecution() | |
} | |
// Simple method to be called when the search is done and print results. | |
@objc func done() { | |
// Just our search time for utility. | |
let time = Date().timeIntervalSince(startTime) | |
// How many results did we find? | |
let count = metadataSearch.resultCount | |
print("Found \(count) results on APFS Data volume in \(time) seconds.") | |
// Look at each group of results and print out the first 10 paths. | |
for group in metadataSearch.groupedResults { | |
print("\nFound: \(group.resultCount) of type: \(group.value)") | |
var count = 0 | |
for item in group.results { | |
if count > 10 { break } | |
let mdItem = item as! NSMetadataItem | |
print("Path: \(mdItem.value(forAttribute: kMDItemPath as String) as Any)") | |
count += 1 | |
} | |
} | |
// Stop the search as we are all done. | |
stopSearch() | |
} | |
} | |
let foo = Searcher() | |
foo.startSearch() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This uses the Spotlight metadata searching to find all the executable items on your Mac and group them by type. It will print out the paths to each executable by category, but just the first 10 avoid spamming you to death.
Example output: