the current solution for darwin is horrible
cos we need a 50 KB bundle
to solve a problem that can be solved in like 1 KB
when on darwin, we can use platform-dependent child_process.spawnSync
calls
#!/bin/bash
# https://stackoverflow.com/a/32465364/10440128
x=~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist; \
plutil -convert xml1 $x; \
grep 'https' -b3 $x | awk 'NR==2 {split($2, arr, "[><]"); print arr[3]}'; \
plutil -convert binary1 $x
this is just one sample solution
at first sight, the file in $x
should not be converted back-and-forth between xml and binary format
only once from binary to xml
or something readable like JSON - yes, plutil can translate to json
also avoid using temp-files
here is a draft version, i have no darwin machine to test = no warranty
// darwin os: get default browser name
// use the `plutil` tool https://www.manpagez.com/man/1/plutil/
const child_process = require('child_process');
const lsf = '~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist';
const lso = JSON.parse(child_process.spawnSync(
'plutil', ['-convert', 'json', '-o', '-', lsf],
{maxBuffer: Infinity, encoding: 'utf-8'}).stdout);
// find value in lso
// ....
// print lso
console.log(JSON.stringify(lso, null, 2));
but this is still a bad solution, the convert step should go
it should be even easier with
// darwin os: get default browser name
// use the `defaults` tool https://ss64.com/osx/defaults.html
// defaults accepts short names for lsf
// https://superuser.com/a/20857/951886
const child_process = require('child_process');
const lsf = 'com.apple.LaunchServices';
const keyOfDefaultBrowser = 'TODO_key_path_here';
const val = child_process.spawnSync(
'defaults', ['read', lsf, keyOfDefaultBrowser]).stdout;
the standard API function is LSCopyDefaultApplicationURLForURL
(this is used by electron to get default apps)
but its closed source, so we cant use that to get the key path
whos got a darwin machine?
what is the structure of com.apple.LaunchServices
?
edit: here is a sample output from defaults read
output format is NeXTSTEP plist format
$ defaults read ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist
{
LSHandlers = (
{
LSHandlerContentType = "org.7-zip.7-zip-archive";
LSHandlerPreferredVersions = {
LSHandlerRoleViewer = "-";
};
LSHandlerRoleViewer = "com.aone.keka";
},
{
LSHandlerContentType = "public.html";
LSHandlerPreferredVersions = {
LSHandlerRoleAll = "-";
};
LSHandlerRoleAll = "com.apple.safari";
}
);
}
ugly! and expensive to parse
so back to
plutil -convert json -o - com.apple.LaunchServices
then the minimal solution should be
(using the logic from default-browser-id)
// darwin os: get default browser name
// use the `plutil` tool https://www.manpagez.com/man/1/plutil/
const child_process = require('child_process');
const getDefaultBrowserOnDarwin = () => (
JSON.parse(child_process.execSync(
"plutil -convert json -o - ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist",
{maxBuffer: 1/0, encoding: "utf-8"}
)).LSHandlers.filter(h => h.LSHandlerURLScheme == "http")
.map(h => h.LSHandlerRoleAll).filter(Boolean)[0]
|| 'com.apple.Safari'
);
that is 414 bytes of code : P
told ya, we can do this in a 1 KB bundle