Skip to content

Instantly share code, notes, and snippets.

@JayBrown
Last active June 4, 2023 21:14
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 JayBrown/d4aff170df9095d35cea89c5d975c8e2 to your computer and use it in GitHub Desktop.
Save JayBrown/d4aff170df9095d35cea89c5d975c8e2 to your computer and use it in GitHub Desktop.
macOS LSIsAppleDefaultNoOverrideForType to automatically open pkg or mpkg installer files in Suspicious Package or Pacifist in addition to macOS Installer app / prerequisites: EventScripts app or NSWorkspaceNotifications observer for NSWorkspaceDidLaunchApplicationNotification
#!/bin/zsh
# shellcheck shell=bash
# applaunched v0.2
export LANG=en_US.UTF-8
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin:/opt/homebrew/bin:/opt/homebrew/sbin:/opt/local/bin:/opt/local/sbin:"$HOME"/.local/bin:"$HOME"/.local/sbin
logloc="/tmp/local.$(id -un).applaunched.log"
exec > >(tee -a "$logloc") 2>&1
currentdate=$(date)
if ! [[ $* ]] ; then
echo -e "*** APP LAUNCHED [$currentdate] ***\nERROR: no input!\n"
exit
fi
bid=$(echo "$*" | awk -F" /" '{print $1}' | awk '{print $NF}')
if ! [[ $bid ]] ; then
echo -e "*** APP LAUNCHED [$currentdate] ***\nERROR: bundle ID missing!\n"
exit
fi
# function: _installer
# LSIsAppleDefaultNoOverrideForType workaround for pkg & mpkg installer files (macOS, Always Open With)
# will automatically open pkg/mpkg files in the app Suspicious Package or Pacifist in addition to macOS default Installer.app
# Suspicious Package: https://www.mothersruin.com/software/SuspiciousPackage/
# if Suspicious Package is not installed, it will look for Pacifist instead
# Pacifist: https://www.charlessoft.com/
# trigger with "Application Has Launched" using the app EventScripts
# EventScripts: https://apps.apple.com/de/app/eventscripts/id525319418?l=en&mt=12
# alternative: trigger with a process listening for NSWorkspaceDidLaunchApplicationNotification in macOS NSWorkspaceNotifications
# note: alternative method was not tested with this script
_installer () {
# determine pkg/mpkg path
fullpkgpathall=$(lsof -c Installer | grep "/.*pkg$" | awk '{print substr($0, index($0,$9))}' | sort | awk '!a[$0]++')
if ! [[ $fullpkgpathall ]] ; then
echo -e "macOS Installer: no package detected! Exiting...\n"
else
# look for Suspicious Package or Pacifist first
suspack=false
pacifist=false
if [[ $(mdfind -onlyin /Applications/ -onlyin "$HOME"/Applications/ "kMDItemDisplayName == \"Suspicious Package.app\"" 2>/dev/null) ]] ; then
suspack=true
else
if [[ $(mdfind -onlyin /Applications/ -onlyin "$HOME"/Applications/ "kMDItemDisplayName == \"Pacifist.app\"" 2>/dev/null) ]] ; then
pacifist=true
fi
fi
if ! $suspack && ! $pacifist ; then
echo -e "GENERAL ERROR [$currentdate]: neither Suspicious Package nor Pacifist detected: exiting!"
else # open pkg/mpkg
while read -r fullpkgpath
do
pkgname=$(basename "$fullpkgpath")
pkgloc=$(dirname "$fullpkgpath")
echo -e "macOS Installer: $pkgname\nPath: $pkgloc"
if $suspack ; then
echo -e "Opening in Suspicious Package...\n"
if command -v spkg &>/dev/null ; then
spkg --reveal-file / "$fullpkgpath" 2>/dev/null &
else
open -a "Suspicious Package" "$fullpkgpath" 2>/dev/null &
fi
elif $pacifist ; then
echo -e "Opening in Pacifist...\n"
open -a "Pacifist" "$fullpkgpath" 2>/dev/null &
fi
done < <(echo "$fullpkgpathall")
fi
fi
}
read -d '' bundleids <<"EOB"
com.apple.installer;_installer
EOB
rawcheck=$(echo "$bundleids" | grep "^$bid;" 2>/dev/null)
if [[ $rawcheck ]] ; then
procedure=$(echo "$rawcheck" | awk -F";" '{print $2}')
echo -e "*** APP LAUNCHED [$currentdate] ***\n$bid\nExecuting: $procedure"
"$procedure"
fi
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment