Skip to content

Instantly share code, notes, and snippets.

@macshome
Last active October 30, 2023 18:43
Show Gist options
  • Save macshome/f7a72f170c52fe4cda12078db356f6a9 to your computer and use it in GitHub Desktop.
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.
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()
@macshome
Copy link
Author

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:

Found 22688 results on APFS Data volume in 0.43902599811553955 seconds.

Found: 53 of type: com.apple.itunes.ipsw
Path: Optional(/Users/josh.wisenbaker/Dropbox (JAMF Software)/OS Installers/11.x/macOS Big Sur 11.3 (20E232)/UniversalMac_11.3_20E232_Restore.ipsw)
Path: Optional(/Users/josh.wisenbaker/Dropbox (JAMF Software)/OS Installers/13.x/macOS 13.2 (22D49)/UniversalMac_13.2_22D49_Restore.ipsw)
Path: Optional(/Users/josh.wisenbaker/Library/Application Support/Veertu/Anka/img_lib/UniversalMac_13.2_22D49_Restore.ipsw)
Path: Optional(/Users/josh.wisenbaker/Dropbox (JAMF Software)/OS Installers/13.x/macOS 13.0.1 (22A400)/UniversalMac_13.0.1_22A400_Restore.ipsw)
Path: Optional(/Users/josh.wisenbaker/Dropbox (JAMF Software)/OS Installers/13.x/macOS 13.0 (22A380)/UniversalMac_13.0_22A380_Restore.ipsw)
Path: Optional(/Users/josh.wisenbaker/Dropbox (JAMF Software)/OS Installers/13.x/macOS 13 beta 4 (22A5311f)/UniversalMac_13.0_22A5311f_Restore.ipsw)
Path: Optional(/Users/josh.wisenbaker/Dropbox (JAMF Software)/OS Installers/12.x/macOS 12.6 (21G115)/UniversalMac_12.6_21G115_Restore.ipsw)
Path: Optional(/Users/josh.wisenbaker/Dropbox (JAMF Software)/OS Installers/12.x/macOS 12.6.1 (21G217) - (Last .ipsw version for Monterey)/UniversalMac_12.6.1_21G217_Restore.ipsw)
Path: Optional(/Users/josh.wisenbaker/Dropbox (JAMF Software)/OS Installers/11.x/macOS Big Sur 11.6 (20G165)/UniversalMac_11.6_20G165_Restore.ipsw)
Path: Optional(/Users/josh.wisenbaker/Dropbox (JAMF Software)/OS Installers/13.x/macOS 13.5 (22G74)/UniversalMac_13.5_22G74_Restore.ipsw)
Path: Optional(/Users/josh.wisenbaker/Dropbox (JAMF Software)/OS Installers/13.x/macOS 13.4.1 (22F82)/UniversalMac_13.4.1_22F82_Restore.ipsw)

Found: 2224 of type: public.ruby-script
Path: Optional(/Library/Developer/CommandLineTools/SDKs/MacOSX14.0.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/io/console/size.rb)
Path: Optional(/Library/Developer/CommandLineTools/SDKs/MacOSX14.0.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/psych/nodes/mapping.rb)
Path: Optional(/Library/Developer/CommandLineTools/SDKs/MacOSX14.0.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rdoc/markup/formatter_test_case.rb)
Path: Optional(/Library/Developer/CommandLineTools/SDKs/MacOSX14.0.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/bundler/vendor/thor/lib/thor/actions/file_manipulation.rb)
Path: Optional(/Library/Developer/CommandLineTools/SDKs/MacOSX14.0.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/gems/2.6.0/gems/CFPropertyList-2.3.6/lib/cfpropertylist/rbNokogiriParser.rb)
Path: Optional(/Library/Ruby/Gems/2.6.0/gems/rake-12.3.3/lib/rake/loaders/makefile.rb)
Path: Optional(/Library/Ruby/Gems/2.6.0/gems/test-unit-3.2.9/test/ui/test_testrunmediator.rb)
Path: Optional(/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/rubygems/dependency_list.rb)
Path: Optional(/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/abbrev.rb)
Path: Optional(/Library/Developer/CommandLineTools/SDKs/MacOSX13.3.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/bundler/ui/rg_proxy.rb)
Path: Optional(/Library/Developer/CommandLineTools/SDKs/MacOSX14.0.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0/resolv.rb)

Found: 1715 of type: public.object-code
Path: Optional(/Users/josh.wisenbaker/Library/Developer/Xcode/DerivedData/TestToy-gpvqoztkvfhjdvgghasavpomkthg/Build/Products/Debug/SwiftFormat.o)
Path: Optional(/Users/josh.wisenbaker/Library/Developer/Xcode/DerivedData/JamfConnect-dsrdurmqacqeydbbxlixuxssqlzm/Build/Products/Debug/CCryptoBoringSSLShims.o)
Path: Optional(/Users/josh.wisenbaker/Library/Developer/Xcode/DerivedData/JamfConnectLogin-aveldavhaxkawoblkllvgnliwils/Build/Products/Release/ZIPFoundation.o)
Path: Optional(/Users/josh.wisenbaker/Library/Developer/Xcode/DerivedData/Playground-fcifyigwfqytjhemzzvbvegmcyxv/Build/Products/Debug/PerfectLDAP.o)
Path: Optional(/Users/josh.wisenbaker/Downloads/llvm-project-7f773cb10377c1eb1ad27d78397985a859c3f300/clang/test/Driver/Inputs/mips_cs_tree/mips-linux-gnu/libc/el/usr/lib/crti.o)
Path: Optional(/Users/josh.wisenbaker/Downloads/llvm-project-7f773cb10377c1eb1ad27d78397985a859c3f300/clang/test/Driver/Inputs/mips_fsf_tree/sysroot/mips32/el/nan2008/usr/lib/crt1.o)
Path: Optional(/Users/josh.wisenbaker/Library/Developer/Xcode/DerivedData/wkinjector-cpbymbqftryzmraglcfnwbpklmxp/Build/Products/Debug/Half.o)
Path: Optional(/Users/josh.wisenbaker/Library/Developer/Xcode/DerivedData/JamfConnect-dsrdurmqacqeydbbxlixuxssqlzm/Build/Products/Debug/Gzip.o)
Path: Optional(/Users/josh.wisenbaker/Library/Developer/Xcode/DerivedData/JamfConnectLogin-aveldavhaxkawoblkllvgnliwils/Build/Products/Release/JamfDebugHelper.o)
Path: Optional(/Users/josh.wisenbaker/Downloads/llvm-project-7f773cb10377c1eb1ad27d78397985a859c3f300/llvm/test/tools/llvm-objdump/MachO/ARM/Inputs/it-nv.o)
Path: Optional(/Users/josh.wisenbaker/Downloads/llvm-project-7f773cb10377c1eb1ad27d78397985a859c3f300/clang/test/Driver/Inputs/mips_cs_tree/mips-linux-gnu/libc/el/usr/lib/crtn.o)

Found: 1 of type: com.apple.service-application
Path: Optional(/Users/josh.wisenbaker/Library/HTTPStorages/com.apple.ctcategories.service)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment