Skip to content

Instantly share code, notes, and snippets.

@scriptingosx
Created August 18, 2023 12:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scriptingosx/5f5ea7fcb1d6839a83fd02ab2729d0e9 to your computer and use it in GitHub Desktop.
Save scriptingosx/5f5ea7fcb1d6839a83fd02ab2729d0e9 to your computer and use it in GitHub Desktop.
two scripts to set and read default app for url schemes (http, mailto, ssh, etc.) for use with Jamf Pro
#!/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 "<result>$defaultApp</result>"
# for non Extension Attribute use, comment above line and uncomment bleow:
# echo "$defaultApp"
#!/bin/bash
# this script will set the current default application for a url scheme
# by Armin Briegel - Scripting OS X
# IMPORTANT: when changing default app for 'http' user is prompted
# also 'https' will be automatically set when 'http' is changed
# attempting to change 'https' will result in an error
# 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.
# get data from arguments
# when running as a Jamf policy, first three args with be target volume,
# computer name and user name. Target volume will always be `/`, unless
# you are imaging, which is dead...
# Then we shift the first three args. That way we can run interactively
# from Terminal as well as from a Jamf policy.
if [[ "$1" == "/" ]]; then
shift 3
fi
urlScheme=${1?:"argument urlScheme required, e.g. 'http'"}
identifier=${2?:"argument bundle identifier required, e.g. 'com.apple.safari'"}
# you can get the identifier for an app with:
# mdls /Applications/Firefox.app -name kMDItemCFBundleIdentifier -raw
currentUser=$( echo "show State:/Users/ConsoleUser" | scutil | awk '/Name :/ { print $3 }' )
uid=$(id -u "$currentUser")
# 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
# run a command as the current user
runAsUser() {
if [ "$currentUser" != "loginwindow" ]; then
launchctl asuser "$uid" sudo -u "$currentUser" "$@"
fi
}
setDefaultAppForScheme() {
local urlScheme=${1?:"arg 1 required: url scheme "}
local bundleIdentifier=${2?:"arg 2 required: bundle identifier"}
runAsUser osascript -l JavaScript << EndOfScript
// create NSURL from scheme string
const url = $.NSURL.URLWithString(ObjC.wrap("${urlScheme}:"))
if ($.NSProcessInfo.processInfo.isOperatingSystemAtLeastVersion({majorVersion: 12, minorVersion: 0, patchVersion: 0})) {
// macOS 12 and higher, use new NSWorkspace API
ObjC.import("AppKit")
const ws=$.NSWorkspace.sharedWorkspace
const appURL = ws.URLForApplicationWithBundleIdentifier(ObjC.wrap("$bundleIdentifier"))
ws.setDefaultApplicationAtURLToOpenURLsWithSchemeCompletionHandler(appURL, ObjC.wrap("$urlScheme"), err => {} )
delay(1)
} else {
// macOS 11 and earlier use deprecated LaunchServices API
ObjC.import("LaunchServices")
$.LSSetDefaultHandlerForURLScheme(ObjC.wrap("$urlScheme"), ObjC.wrap("$bundleIdentifier"))
}
EndOfScript
}
setDefaultAppForScheme "$urlScheme" "$identifier"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment