Skip to content

Instantly share code, notes, and snippets.

@frankhinek
Last active April 14, 2023 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankhinek/8782d03fc469448fa11dbc2eb8086b5a to your computer and use it in GitHub Desktop.
Save frankhinek/8782d03fc469448fa11dbc2eb8086b5a to your computer and use it in GitHub Desktop.
Determining URL scheme handlers in macOS

Determining URL scheme handlers in macOS

Credit for code to Joel Bruner in this post.

Usage

Get Bundle ID for Registered Handler

./getDefaultRoleHandler.sh web5

com.github.electron

Get File Path for a URL scheme handler

./getDefaultRoleHandlerPath.sh web5

/Users/loginID/Code/desktop-agent/node_modules/electron/dist/Electron.app

Listing All Schemes for User

plutil -extract LSHandlers \
  json -o - ~/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist | \
  node formatAllSchemes.js

Registered URL Scheme Handlers
  ┌─────────┬────────────────────────────────┬─────────────────────────────┐
  │ (index) │             Scheme             │           Handler           │
  ├─────────┼────────────────────────────────┼─────────────────────────────┤
  │    0    │           'postman''com.postmanlabs.mac'    │
  │    1    │            'slack''com.tinyspeck.slackmacgap' │
  │    2    │        'docker-desktop''com.docker.docker'     │
  │    3    │             'web5''com.github.electron'    │
  │    4    │           'facetime''com.apple.facetime'     │
  │    5    │             'http''com.google.chrome'     │
  │    6    │            'https''com.google.chrome'     │
  │    7    │            'mailto''com.google.chrome'     │
  └─────────┴────────────────────────────────┴─────────────────────────────┘

References:

let inputData = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', (chunk) => {
inputData += chunk;
});
process.stdin.on('end', () => {
const parsedData = JSON.parse(inputData);
let tableData = [];
parsedData.forEach((handler) => {
const urlScheme = handler?.LSHandlerURLScheme;
if (urlScheme) {
tableData.push({
Scheme: urlScheme,
Handler: handler?.LSHandlerRoleAll,
});
}
});
console.group('Registered URL Scheme Handlers');
console.table(tableData);
console.groupEnd();
});
#!/bin/sh
: <<-LICENSE_BLOCK
Get Default Role Handler - (https://github.com/brunerd)
Copyright (c) 2022 Joel Bruner (https://github.com/brunerd)
Licensed under the MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
LICENSE_BLOCK
#############
# FUNCTIONS #
#############
#detect the default role handler self-contained for supplied URL scheme (i.e. https)
function getDefaultRoleHandler() ( URLScheme=${1}; if [ -z "${URLScheme}" ]; then >/dev/stderr echo "No URL scheme specified";return 1;fi; function ljt () ( [ -n "${-//[^x]/}" ] && set +x; read -r -d '' JSCode <<-'EOT'
try{var query=decodeURIComponent(escape(arguments[0])),file=decodeURIComponent(escape(arguments[1]));if("/"===query[0]||""===query){if(/~[^0-1]/g.test(query+" "))throw new SyntaxError("JSON Pointer allows ~0 and ~1 only: "+query);query=query.split("/").slice(1).map(function(a){return"["+JSON.stringify(a.replace(/~1/g,"/").replace(/~0/g,"~"))+"]"}).join("")}else if("$"===query[0]||"."===query[0]||"["===query[0]){if(/[^A-Za-z_$\d\.\[\]'"]/.test(query.split("").reverse().join("").replace(/(["'])(.*?)\1(?!\\)/g,"")))throw Error("Invalid path: "+query);}else query=query.replace("\\.","\udead").split(".").map(function(a){return"["+JSON.stringify(a.replace("\udead","."))+"]"}).join("");"$"===query[0]&&(query=query.slice(1,query.length));var data=JSON.parse(readFile(file));try{var result=eval("(data)"+query)}catch(a){}}catch(a){printErr(a),quit()}void 0!==result?null!==result&&result.constructor===String?print(result):print(JSON.stringify(result,null,2)):printErr("Path not found.");
EOT
queryArg="${1}"; fileArg="${2}";jsc=$(find "/System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/" -name 'jsc');[ -z "${jsc}" ] && jsc=$(which jsc);[ -f "${queryArg}" -a -z "${fileArg}" ] && fileArg="${queryArg}" && unset queryArg;if [ -f "${fileArg:=/dev/stdin}" ]; then { errOut=$( { { "${jsc}" -e "${JSCode}" -- "${queryArg}" "${fileArg}"; } 1>&3 ; } 2>&1); } 3>&1;else [ -t '0' ] && echo -e "ljt (v1.0.7) - Little JSON Tool (https://github.com/brunerd/ljt)\nUsage: ljt [query] [filepath]\n [query] is optional and can be JSON Pointer, canonical JSONPath (with or without leading $), or plutil-style keypath\n [filepath] is optional, input can also be via file redirection, piped input, here doc, or here strings" >/dev/stderr && exit 0; { errOut=$( { { "${jsc}" -e "${JSCode}" -- "${queryArg}" "/dev/stdin" <<< "$(cat)"; } 1>&3 ; } 2>&1); } 3>&1; fi;if [ -n "${errOut}" ]; then /bin/echo "$errOut" >&2; return 1; fi); consoleUserHomeFolder=$(sudo -u "$(stat -f %Su /dev/console)" sh -c 'echo ~'); launchServicesJSON=$(launchctl asuser "$(stat -f %u /dev/console)" sudo -u "$(stat -f %Su /dev/console)" plutil -extract LSHandlers json -o - "${consoleUserHomeFolder}"/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist); for ((i=0;;i++)); do if ! ljt "/$i" <<< "${launchServicesJSON}" &>/dev/null; then return 1; elif [ "$(ljt "/$i/LSHandlerURLScheme" <<< "${launchServicesJSON}" 2>/dev/null)" = "$URLScheme" ]; then if ! ljt "/$i/LSHandlerRoleAll" <<< "${launchServicesJSON}" 2>/dev/null; then return 1; else return 0; fi; fi; done; return 1
)
########
# MAIN #
########
getDefaultRoleHandler "$@"
#!/bin/sh
: <<-LICENSE_BLOCK
Get Default Role Handler - (https://github.com/brunerd)
Copyright (c) 2022 Joel Bruner (https://github.com/brunerd)
Licensed under the MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
LICENSE_BLOCK
#############
# FUNCTIONS #
#############
#use this self-contained function in your script to detect the default role handle
function getDefaultRoleHandler() (
#provide a URL scheme like: http, https, ftp, etc...
URLScheme=${1}
#fail quickly
if [ -z "${URLScheme}" ]; then
>/dev/stderr echo "No URL scheme specified"
return 1
fi
#Little JSON Tool (ljt) v1.0.7 - https://github.com/brunerd/ljt - MIT License
function ljt () (
[ -n "${-//[^x]/}" ] && set +x; read -r -d '' JSCode <<-'EOT'
try{var query=decodeURIComponent(escape(arguments[0])),file=decodeURIComponent(escape(arguments[1]));if("/"===query[0]||""===query){if(/~[^0-1]/g.test(query+" "))throw new SyntaxError("JSON Pointer allows ~0 and ~1 only: "+query);query=query.split("/").slice(1).map(function(a){return"["+JSON.stringify(a.replace(/~1/g,"/").replace(/~0/g,"~"))+"]"}).join("")}else if("$"===query[0]||"."===query[0]||"["===query[0]){if(/[^A-Za-z_$\d\.\[\]'"]/.test(query.split("").reverse().join("").replace(/(["'])(.*?)\1(?!\\)/g,"")))throw Error("Invalid path: "+query);}else query=query.replace("\\.","\udead").split(".").map(function(a){return"["+JSON.stringify(a.replace("\udead","."))+"]"}).join("");"$"===query[0]&&(query=query.slice(1,query.length));var data=JSON.parse(readFile(file));try{var result=eval("(data)"+query)}catch(a){}}catch(a){printErr(a),quit()}void 0!==result?null!==result&&result.constructor===String?print(result):print(JSON.stringify(result,null,2)):printErr("Path not found.");
EOT
queryArg="${1}"; fileArg="${2}";jsc=$(find "/System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/" -name 'jsc');[ -z "${jsc}" ] && jsc=$(which jsc);[ -f "${queryArg}" -a -z "${fileArg}" ] && fileArg="${queryArg}" && unset queryArg;if [ -f "${fileArg:=/dev/stdin}" ]; then { errOut=$( { { "${jsc}" -e "${JSCode}" -- "${queryArg}" "${fileArg}"; } 1>&3 ; } 2>&1); } 3>&1;else [ -t '0' ] && echo -e "ljt (v1.0.7) - Little JSON Tool (https://github.com/brunerd/ljt)\nUsage: ljt [query] [filepath]\n [query] is optional and can be JSON Pointer, canonical JSONPath (with or without leading $), or plutil-style keypath\n [filepath] is optional, input can also be via file redirection, piped input, here doc, or here strings" >/dev/stderr && exit 0; { errOut=$( { { "${jsc}" -e "${JSCode}" -- "${queryArg}" "/dev/stdin" <<< "$(cat)"; } 1>&3 ; } 2>&1); } 3>&1; fi;if [ -n "${errOut}" ]; then /bin/echo "$errOut" >&2; return 1; fi
)
#in case being run as root get the current console user
consoleUserHomeFolder=$(sudo -u "$(stat -f %Su /dev/console)" sh -c 'echo ~')
#get the LaunchServices LSHandlers JSON of the console user
launchServicesJSON=$(launchctl asuser "$(stat -f %u /dev/console)" sudo -u "$(stat -f %Su /dev/console)" plutil -extract LSHandlers json -o - "${consoleUserHomeFolder}"/Library/Preferences/com.apple.LaunchServices/com.apple.launchservices.secure.plist)
#loop through JSON and try and find matching URLScheme within
for ((i=0;;i++)); do
#if we are at the END of the array or nothing exists bail
if ! ljt "/$i" <<< "${launchServicesJSON}" &>/dev/null; then
return 1
elif [ "$(ljt "/$i/LSHandlerURLScheme" <<< "${launchServicesJSON}" 2>/dev/null)" = "$URLScheme" ]; then
#run query, print result, errors go to /dev/null, if ljt fails to find something return non-zero
if ! ljt "/$i/LSHandlerRoleAll" <<< "${launchServicesJSON}" 2>/dev/null; then
#error
return 1
else
#success
return 0
fi
fi
done
#if we are here, we did NOT find a match
return 1
)
########
# MAIN #
########
getDefaultRoleHandler "$@"
#!/bin/sh
#getDefaultRoleHandlerPath - Joel Bruner with 🙏 thanks to a couple stalwarts of the MacAdmin Slack Armin Briegel and Pico
function getDefaultRoleHandlerPath()(urlScheme="${1}"; [ -z "${urlScheme}" ] && return 1; osascript -l 'JavaScript' -e "ObjC.import('AppKit'); $.NSWorkspace.sharedWorkspace.URLForApplicationToOpenURL($.NSURL.URLWithString('${urlScheme}:')).path.js")
getDefaultRoleHandlerPath "$@"
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment