#!/bin/bash # this script will return the current default application for a url scheme # for use as a Jamf Extension attribute # by Armin Briegel - Scripting OS X # Permission is granted to use this code in any way you want. # Credit would be nice, but not obligatory. # Provided "as is", without warranty of any kind, express or implied. # the url scheme to check for # change to check for other url schemes, e.g. `mailto`, `ssh` urlScheme="http" # Note that macOS does not allow separate settings for `http` and `https`. # When a user changes one, it changes both, so it is sufficient to check just one. currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' ) # global check if there is a user logged in if [ -z "$currentUser" ] || [ "$currentUser" = "loginwindow" ]; then echo "no user logged in, cannot proceed" exit 1 fi # now we know a user is logged in # get the current user's UID uid=$(id -u "$currentUser") # run a command as the current user runAsUser() { if [ "$currentUser" != "loginwindow" ]; then launchctl asuser "$uid" sudo -u "$currentUser" "$@" fi } getDefaultAppForScheme() { # $1: scheme, e.g. http, mailto, ... local urlScheme="${1?:"arg 1 required: url scheme "}" runAsUser osascript -l JavaScript << EndOfScript if ($.NSProcessInfo.processInfo.isOperatingSystemAtLeastVersion({majorVersion: 12, minorVersion: 0, patchVersion: 0})) { ObjC.import("AppKit") const url = $.NSURL.URLWithString(ObjC.wrap("${urlScheme}:")) const result = $.NSWorkspace.sharedWorkspace.URLForApplicationToOpenURL(url) result.path.js } else { ObjC.import("LaunchServices") const url = $.NSURL.URLWithString(ObjC.wrap("http:")) $.LSCopyDefaultApplicationURLForURL(ObjC.wrap(url), 4294967295, null).path.js } EndOfScript } defaultApp=$(getDefaultAppForScheme "$urlScheme") # for Jamf extension attributes, wrap in `result` tags echo "$defaultApp" # for non Extension Attribute use, comment above line and uncomment bleow: # echo "$defaultApp"