{"id":1195,"date":"2022-09-21T21:36:30","date_gmt":"2022-09-22T02:36:30","guid":{"rendered":"https:\/\/www.brunerd.com\/blog\/?p=1195"},"modified":"2022-10-06T22:42:25","modified_gmt":"2022-10-07T03:42:25","slug":"determining-url-scheme-handlers-in-macos","status":"publish","type":"post","link":"https:\/\/www.brunerd.com\/blog\/2022\/09\/21\/determining-url-scheme-handlers-in-macos\/","title":{"rendered":"Determining URL scheme handlers in macOS"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">If you&#8217;ve worked at a place that uses Outlook for email or Chrome for browsers, knowing is half the battle when it comes to helping out your users. For a long while you could easily determine the <a href=\"https:\/\/developer.apple.com\/documentation\/xcode\/defining-a-custom-url-scheme-for-your-app\" target=\"_blank\" rel=\"noreferrer noopener\">URL scheme<\/a> handlers via the built-in Python or JXA, however things have changed. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example this JXA used to work brilliantly in Mojave and under:<\/p>\n\n\n\n<pre id=\"language-bash\" class=\"wp-block-code language-bash\"><code>#!\/bin\/sh\nURLScheme=\"${1}\"\n\nif &#91; -z \"${URLScheme}\" ]; then\n    echo \"Please provide a URL scheme name\"\n    exit 1\nfi\n\nosascript -l JavaScript &lt;&lt;&lt; \"ObjC.import('Cocoa'); $.LSCopyDefaultHandlerForURLScheme(\\\"${URLScheme}\\\")\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Nothing lasts forever though and all that broke in 10.15 when the result of <code>[object Ref]<\/code> became the taunting reply. Even if you could hit the juke box like the Fonz and recast the CF value you were getting back into an NS value (thanks for the <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/JXA-Cookbook\/JXA-Cookbook\/issues\/13#issuecomment-174820487\" target=\"_blank\">link<\/a> Pico), it was a moot point by Big Sur. Apple has killed off about every useful API to figure out what app handles a URL as <a rel=\"noreferrer noopener\" href=\"https:\/\/mjtsai.com\/blog\/2020\/09\/01\/setting-default-url-handlers\/\" target=\"_blank\">others have noted<\/a>. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So after you look at <a rel=\"noreferrer noopener\" href=\"https:\/\/developer.apple.com\/documentation\/coreservices\/1441725-lscopydefaulthandlerforurlscheme?language=objc\" target=\"_blank\">LSCopyDefaultHandlerForURLScheme<\/a> at the \uf8ff Dev Docs and then see the <strong>scores<\/strong> of other deprecated LaunchServices functions, you might make some joke like &#8220;I see dead <s>people<\/s> APIs&#8221; (a la Sixth Sense), but still you wonder: Is there a reliable way to divine this information without resorting to a hacky grep?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Why, yes there is, and it just so happens to use my tool <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/brunerd\/ljt\" target=\"_blank\">ljt<\/a> too! While definitely a step up from grepping the output it&#8217;s still kinda sad. What was once a few lines must now be accomplished by many more in <a href=\"https:\/\/github.com\/brunerd\/macAdminTools\/blob\/main\/Scripts\/getDefaultRoleHandler.sh\">getDefaultRoleHandler.sh<\/a> and the minified version <a href=\"https:\/\/github.com\/brunerd\/macAdminTools\/blob\/main\/Scripts\/getDefaultRoleHandler.min.sh\">getDefaultRoleHandler.min.sh<\/a>.<\/p>\n\n\n\n<pre id=\"language-bash\" class=\"wp-block-code language-bash\"><code>#!\/bin\/sh\n: &lt;&lt;-LICENSE_BLOCK\ngetDefaultRoleHandler - (https:\/\/github.com\/brunerd)\nCopyright (c) 2022 Joel Bruner (https:\/\/github.com\/brunerd)\nLicensed under the MIT License\n\nPermission 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:\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\nTHE 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.\nLICENSE_BLOCK\n\n#############\n# FUNCTIONS #\n#############\n\n#use this self-contained function in your script to detect the default role handle\nfunction getDefaultRoleHandler() (\n\t#provide a URL scheme like: http, https, ftp, etc...\n\tURLScheme=${1}\n\t\n\t#fail quickly\n\tif &#91; -z \"${URLScheme}\" ]; then\n\t\t&gt;\/dev\/stderr echo \"No URL scheme specified\"\n\t\treturn 1\n\tfi\n\n\t#Little JSON Tool (ljt) v1.0.7 - https:\/\/github.com\/brunerd\/ljt - MIT License\n\tfunction ljt () ( \n\t&#91; -n \"${-\/\/&#91;^x]\/}\" ] &amp;&amp; set +x; read -r -d '' JSCode &lt;&lt;-'EOT'\n\ttry{var query=decodeURIComponent(escape(arguments&#91;0])),file=decodeURIComponent(escape(arguments&#91;1]));if(\"\/\"===query&#91;0]||\"\"===query){if(\/~&#91;^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\"&#91;\"+JSON.stringify(a.replace(\/~1\/g,\"\/\").replace(\/~0\/g,\"~\"))+\"]\"}).join(\"\")}else if(\"$\"===query&#91;0]||\".\"===query&#91;0]||\"&#91;\"===query&#91;0]){if(\/&#91;^A-Za-z_$\\d\\.\\&#91;\\]'\"]\/.test(query.split(\"\").reverse().join(\"\").replace(\/(&#91;\"'])(.*?)\\1(?!\\\\)\/g,\"\")))throw Error(\"Invalid path: \"+query);}else query=query.replace(\"\\\\.\",\"\\udead\").split(\".\").map(function(a){return\"&#91;\"+JSON.stringify(a.replace(\"\\udead\",\".\"))+\"]\"}).join(\"\");\"$\"===query&#91;0]&amp;&amp;(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&amp;&amp;result.constructor===String?print(result):print(JSON.stringify(result,null,2)):printErr(\"Path not found.\");\n\tEOT\n\tqueryArg=\"${1}\"; fileArg=\"${2}\";jsc=$(find \"\/System\/Library\/Frameworks\/JavaScriptCore.framework\/Versions\/Current\/\" -name 'jsc');&#91; -z \"${jsc}\" ] &amp;&amp; jsc=$(which jsc);&#91; -f \"${queryArg}\" -a -z \"${fileArg}\" ] &amp;&amp; fileArg=\"${queryArg}\" &amp;&amp; unset queryArg;if &#91; -f \"${fileArg:=\/dev\/stdin}\" ]; then { errOut=$( { { \"${jsc}\" -e \"${JSCode}\" -- \"${queryArg}\" \"${fileArg}\"; } 1&gt;&amp;3 ; } 2&gt;&amp;1); } 3&gt;&amp;1;else &#91; -t '0' ] &amp;&amp; echo -e \"ljt (v1.0.7) - Little JSON Tool (https:\/\/github.com\/brunerd\/ljt)\\nUsage: ljt &#91;query] &#91;filepath]\\n  &#91;query] is optional and can be JSON Pointer, canonical JSONPath (with or without leading $), or plutil-style keypath\\n  &#91;filepath] is optional, input can also be via file redirection, piped input, here doc, or here strings\" &gt;\/dev\/stderr &amp;&amp; exit 0; { errOut=$( { { \"${jsc}\" -e \"${JSCode}\" -- \"${queryArg}\" \"\/dev\/stdin\" &lt;&lt;&lt; \"$(cat)\"; } 1&gt;&amp;3 ; } 2&gt;&amp;1); } 3&gt;&amp;1; fi;if &#91; -n \"${errOut}\" ]; then \/bin\/echo \"$errOut\" &gt;&amp;2; return 1; fi\n\t)\n\n\t#in case being run as root get the current console user\n\tconsoleUserHomeFolder=$(sudo -u \"$(stat -f %Su \/dev\/console)\" sh -c 'echo ~')\n\t#get the LaunchServices LSHandlers JSON of the console user\n\tlaunchServicesJSON=$(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)\n\n\t#loop through JSON and try and find matching URLScheme within\n\tfor ((i=0;;i++)); do\n\t\t#if we are at the END of the array or nothing exists bail\n\t\tif ! ljt \"\/$i\" &lt;&lt;&lt; \"${launchServicesJSON}\" &amp;&gt;\/dev\/null; then\n\t\t\treturn 1\n\t\telif &#91; \"$(ljt \"\/$i\/LSHandlerURLScheme\" &lt;&lt;&lt; \"${launchServicesJSON}\" 2&gt;\/dev\/null)\" = \"$URLScheme\" ]; then\n\t\t\t#run query, print result, errors go to \/dev\/null, if ljt fails to find something return non-zero\n\t\t\tif ! ljt \"\/$i\/LSHandlerRoleAll\" &lt;&lt;&lt; \"${launchServicesJSON}\" 2&gt;\/dev\/null; then\n\t\t\t\t#error\n\t\t\t\treturn 1\n\t\t\telse\n\t\t\t\t#success\n\t\t\t\treturn 0\n\t\t\tfi\n\t\tfi\n\tdone\n\t\n\t#if we are here, we did NOT find a match\n\treturn 1\t\n)\n\n########\n# MAIN #\n########\n\ngetDefaultRoleHandler \"$@\"<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We use <code>plutil<\/code> to extract the contents of the <code>LSHandlers<\/code> key as <strong>JSON<\/strong> from <code>~\/Library\/Preferences\/com.apple.LaunchServices\/com.apple.launchservices.secure.plist<\/code> then we iterate over the array using a <code>for<\/code> loop and <code><a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/brunerd\/ljt\" target=\"_blank\">ljt<\/a><\/code> until it finds a matching <code>LSHandlerURLScheme<\/code> and then it prints out the value of <code>LSHandlerRoleAll<\/code>. The operation and output are simple and no nonsense. Give it a URL scheme and it gives you the Bundle ID of the app which will open it or if not found, nothing. (Note: <code>mailto<\/code> will be blank by default, only after switching to another client will it return a result)<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/www.brunerd.com\/blog\/wp-content\/uploads\/getDefaultRoleHandler-example2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"453\" height=\"173\" src=\"https:\/\/www.brunerd.com\/blog\/wp-content\/uploads\/getDefaultRoleHandler-example2.png\" alt=\"\" class=\"wp-image-1207\" srcset=\"https:\/\/www.brunerd.com\/blog\/wp-content\/uploads\/getDefaultRoleHandler-example2.png 453w, https:\/\/www.brunerd.com\/blog\/wp-content\/uploads\/getDefaultRoleHandler-example2-300x115.png 300w\" sizes=\"auto, (max-width: 453px) 100vw, 453px\" \/><\/a><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">You can use this function in <em>your<\/em> scripts. Head over to my GitHub to download either <a href=\"https:\/\/github.com\/brunerd\/macAdminTools\/blob\/main\/Scripts\/getDefaultRoleHandler.sh\">getDefaultRoleHandler.sh<\/a> or the minified <a href=\"https:\/\/github.com\/brunerd\/macAdminTools\/blob\/main\/Scripts\/getDefaultRoleHandler.min.sh\">getDefaultRoleHandler.min.sh<\/a>. Perhaps you need to alert a user to change their application preferences after installation of an email client or web browser. You can use my <a href=\"https:\/\/github.com\/brunerd\/shui\">shui<\/a> function to alert them, btw. But I&#8217;m <strong>not<\/strong> getting into setting handlers via script in this post, writing to the plist can be a trick but the <em>real<\/em> trick is getting LaunchService to <strong>read<\/strong> from that plist (without asking for a reboot or logout, ick)! So for now &#8220;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.youtube.com\/watch?v=_GK9RtrNX_s\" target=\"_blank\">Knowing Is Half The Battle\u2122<\/a>&#8221; <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Post Posting Update<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Armin Briegel, over at the wonderful wealth of aggregated MacAdmin knowledge that is <a rel=\"noreferrer noopener\" href=\"https:\/\/scriptingosx.com\" target=\"_blank\">scriptingosx.com<\/a>, is always kind enough to post my articles and this very one appears in the <a rel=\"noreferrer noopener\" href=\"https:\/\/scriptingosx.com\/2022\/09\/weekly-news-summary-for-admins-2022-09-22\/\" target=\"_blank\">Week of 2022-09-22<\/a>. Along with this post is a link to a MacAdmins <a rel=\"noreferrer noopener\" href=\"https:\/\/macadmins.slack.com\/archives\/C4ENKD38A\/p1663852083468069\" target=\"_blank\">Slack thread<\/a> where he kindly provided some JXA that returns the file path for a URL scheme handler (vs. the above Bundle ID), then Pico jumped in and crushed it down to a 1 liner! I&#8217;ve added the thinnest of function wrappers over top:<\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>#!\/bin\/sh\n#getDefaultRoleHandlerPath - Joel Bruner with \ud83d\ude4f thanks to a couple stalwarts of the MacAdmin Slack Armin Briegel and Pico\n\nfunction getDefaultRoleHandlerPath()(urlScheme=\"${1}\"; &#91; -z \"${urlScheme}\" ] &amp;&amp; return 1; osascript -l 'JavaScript' -e \"ObjC.import('AppKit'); $.NSWorkspace.sharedWorkspace.URLForApplicationToOpenURL($.NSURL.URLWithString('${urlScheme}:')).path.js\")\n\ngetDefaultRoleHandlerPath \"$@\"\nexit $?<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">So it seems Apple has spared an API in AppKit&#8217;s NSWorkspace by the name of <code>URLForApplicationToOpenURL<\/code>, which has been around since macOS 10.6! Perhaps when I get over my trust issues with JXA, I&#8217;ll explore these APIs to see what else might be useful. For now though this does the trick of returning a <strong>filepath<\/strong> (not Bundle ID)<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/www.brunerd.com\/blog\/wp-content\/uploads\/defaultRoleHandlerPath.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.brunerd.com\/blog\/wp-content\/uploads\/defaultRoleHandlerPath.png\" alt=\"\" class=\"wp-image-1252\" width=\"309\" height=\"48\" srcset=\"https:\/\/www.brunerd.com\/blog\/wp-content\/uploads\/defaultRoleHandlerPath.png 422w, https:\/\/www.brunerd.com\/blog\/wp-content\/uploads\/defaultRoleHandlerPath-300x46.png 300w\" sizes=\"auto, (max-width: 309px) 100vw, 309px\" \/><\/a><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">Listing All Schemes for User<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">I got a question about listing all the schemes a user might have registered. This is an example one-liner (granted <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/brunerd\/jpt\" target=\"_blank\">jpt<\/a> is embedded in the script or installed locally) to be run by the current user.  If you want to run as root, cannibalize <a href=\"https:\/\/github.com\/brunerd\/macAdminTools\/blob\/main\/Scripts\/getDefaultRoleHandler.sh\">getDefaultRoleHandler.sh<\/a> for code) <\/p>\n\n\n\n<pre class=\"wp-block-code language-bash\"><code>jpt -T '$.*.LSHandlerURLScheme' &lt;&lt;&lt; $(plutil -extract LSHandlers json -o - ~\/Library\/Preferences\/com.apple.LaunchServices\/com.apple.launchservices.secure.plist)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>-T<\/code> option will output text (vs. an array of double quoted JSON strings) <code>.*<\/code> looks at every array member to print out every LSHandlerURLScheme entry for the current console user. It&#8217;ll look something like this<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/www.brunerd.com\/blog\/wp-content\/uploads\/All_LSHandlerURLSchemes.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.brunerd.com\/blog\/wp-content\/uploads\/All_LSHandlerURLSchemes-1024x455.png\" alt=\"\" class=\"wp-image-1257\" width=\"540\" height=\"240\" srcset=\"https:\/\/www.brunerd.com\/blog\/wp-content\/uploads\/All_LSHandlerURLSchemes-1024x455.png 1024w, https:\/\/www.brunerd.com\/blog\/wp-content\/uploads\/All_LSHandlerURLSchemes-300x133.png 300w, https:\/\/www.brunerd.com\/blog\/wp-content\/uploads\/All_LSHandlerURLSchemes-768x341.png 768w, https:\/\/www.brunerd.com\/blog\/wp-content\/uploads\/All_LSHandlerURLSchemes.png 1166w\" sizes=\"auto, (max-width: 540px) 100vw, 540px\" \/><\/a><\/figure>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>If you&#8217;ve worked at a place that uses Outlook for email or Chrome for browsers, knowing is half the battle when it comes to helping out your users. For a long while you could easily determine the URL scheme handlers via the built-in Python or JXA, however things have changed. For example this JXA used [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,51,53,12],"tags":[],"class_list":["post-1195","post","type-post","status-publish","format-standard","hentry","category-bash","category-json","category-ljt","category-scripting"],"_links":{"self":[{"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/posts\/1195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/comments?post=1195"}],"version-history":[{"count":19,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/posts\/1195\/revisions"}],"predecessor-version":[{"id":1258,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/posts\/1195\/revisions\/1258"}],"wp:attachment":[{"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/media?parent=1195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/categories?post=1195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.brunerd.com\/blog\/wp-json\/wp\/v2\/tags?post=1195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}