Find command-line tools from a macOS app
By Flavio
Tagged: Swift
Fix tools that work in Terminal but disappear from Finder-launched macOS apps by locating binaries and building an explicit child-process PATH.
A command can work fine in Terminal and still fail when you run it from a macOS app under the same user account. That happens because an app launched from Finder or the Dock doesn’t inherit your interactive shell’s PATH.
Do not trust the inherited PATH
Start by reading whatever PATH the process already has, then add the common user and system directories where CLI tools usually live:
let inherited = environment["PATH", default: ""]
.split(separator: ":")
.map {
URL(
fileURLWithPath: String($0),
isDirectory: true
)
}
let known = [
home.appendingPathComponent(".local/bin"),
home.appendingPathComponent(".deno/bin"),
home.appendingPathComponent("bin"),
URL(fileURLWithPath: "/opt/homebrew/bin"),
URL(fileURLWithPath: "/usr/local/bin"),
URL(fileURLWithPath: "/usr/bin")
]
With those fallback directories included, you cover all the usual places:
- user-local installations
- Deno’s default directory
- Apple Silicon Homebrew
- Intel Homebrew
- system tools
Remove duplicates
Once you combine the inherited PATH with your fallback list, you’ll often end up with repeated entries. Deduplicate the list while keeping the original order so the first matching executable still wins:
var seen = Set<String>()
let directories = (inherited + known).filter { url in
seen.insert(url.standardizedFileURL.path).inserted
}
Check executability
Checking whether a file exists at that path isn’t enough, because a matching file might not be executable. Check executability directly before accepting a candidate:
func executable(
named name: String,
in directories: [URL]
) -> URL? {
directories
.map {
$0.appendingPathComponent(name)
}
.first {
FileManager.default.isExecutableFile(
atPath: $0.path
)
}
}
From there, resolve and return absolute URLs for every tool your app needs:
struct Toolchain {
let ytDLP: URL
let ffmpeg: URL
let deno: URL
}
Locating the parent command is only half the fix
Even if your app finds and launches yt-dlp without any trouble, it can still fail a moment later when yt-dlp tries to call ffmpeg or Deno from that same minimal child environment.
To prevent that, build an explicit PATH for the child process:
let required = [
toolchain.ytDLP.deletingLastPathComponent().path,
toolchain.ffmpeg.deletingLastPathComponent().path,
toolchain.deno.deletingLastPathComponent().path,
"/opt/homebrew/bin",
"/usr/local/bin",
"/usr/bin",
"/bin",
"/usr/sbin",
"/sbin"
]
let existing = environment["PATH", default: ""]
.split(separator: ":")
.map(String.init)
var childEnvironment = environment
var seen = Set<String>()
childEnvironment["PATH"] = (required + existing)
.filter { seen.insert($0).inserted }
.joined(separator: ":")
Then assign that environment to the process before you run it:
process.environment = childEnvironment
Pass absolute dependency paths when supported
When a tool lets you pass dependency locations directly through flags, use them. yt-dlp accepts an explicit JavaScript runtime path:
--js-runtimes deno:/Users/flavio/.deno/bin/deno
You can also hand it the exact location of ffmpeg:
--ffmpeg-location /opt/homebrew/bin/ffmpeg
Passing explicit paths is much more reliable than hoping a nested process searches PATH the way you expect.
Test a Finder-like environment
Don’t run your tests against real Homebrew directories. Instead, build a temporary directory tree that mimics the minimal environment an app gets outside Terminal, and inject it into the locator:
let root = FileManager.default.temporaryDirectory
.appendingPathComponent(UUID().uuidString)
let localBin = root.appendingPathComponent(".local/bin")
let denoBin = root.appendingPathComponent(".deno/bin")
let toolsBin = root.appendingPathComponent("test-tools")
let locator = ToolchainLocator(
searchDirectories: [
localBin,
denoBin,
toolsBin
]
)
Drop fake executable files into those temporary locations:
<temporary root>/.local/bin/yt-dlp
<temporary root>/.deno/bin/deno
<temporary root>/test-tools/ffmpeg
Then assert that your locator finds them. Never create a fake executable in /opt/homebrew/bin during a test, because it can overwrite a real installation on your machine.
Want me to talk about your product? You can sponsor this site.
Related posts about swift: