Advertisement
Guest User

macOS Open With workaround for pkg & mpkg installer files

a guest
Apr 3rd, 2023
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.99 KB | None | 0 0
  1. #!/bin/zsh
  2. # shellcheck shell=bash
  3.  
  4. # applaunched v0.1
  5. # LSIsAppleDefaultNoOverrideForType workaround for pkg & mpkg installer files (macOS)
  6.  
  7. export LANG=en_US.UTF-8
  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
  9.  
  10. logloc="/tmp/local.$(id -un).applaunched.log"
  11. exec > >(tee -a "$logloc") 2>&1
  12.  
  13. currentdate=$(date)
  14.  
  15. if ! [[ $* ]] ; then
  16.     echo -e "*** APP LAUNCHED [$currentdate] ***\nERROR: no input!\n"
  17.     exit
  18. fi
  19.  
  20. bid=$(echo "$*" | awk -F" /" '{print $1}' | awk '{print $NF}')
  21. if ! [[ $bid ]] ; then
  22.     echo -e "*** APP LAUNCHED [$currentdate] ***\nERROR: bundle ID missing!\n"
  23.     exit
  24. fi
  25.  
  26. _installer () {
  27.     fullpkgpath=$(lsof -c Installer | grep "/.*pkg$" | awk '{print substr($0, index($0,$9))}' | sort | awk '!a[$0]++')
  28.     if ! [[ $fullpkgpath ]] ; then
  29.         echo -e "macOS Installer: no package detected! Exiting...\n"
  30.     else
  31.         pkgname=$(basename "$fullpkgpath")
  32.         pkgloc=$(dirname "$fullpkgpath")
  33.         echo -e "macOS Installer: $pkgname\nPath: $pkgloc"
  34.         if pgrep -x "Suspicious Package" &>/dev/null ; then
  35.             if lsof -c "Suspicious Package" | grep "$pkgname" &>/dev/null ; then
  36.                 echo -e "Package already opened in Suspicious Package: exiting...\n"
  37.                 osascript 2>/dev/null <<EOF
  38. tell application "System Events"
  39.     tell application process "Suspicious Package"
  40.         set frontmost to true
  41.     end tell
  42. end tell
  43. EOF
  44.             else
  45.                 echo -e "Opening in Suspicious Package...\n"
  46.                 open -a "Suspicious Package" "$fullpkgpath" 2>/dev/null &
  47.             fi
  48.         else
  49.             echo -e "Opening in Suspicious Package...\n"
  50.             open -a "Suspicious Package" "$fullpkgpath" 2>/dev/null &
  51.         fi
  52.     fi
  53. }
  54.  
  55. read -d '' bundleids <<"EOB"
  56. com.apple.installer;_installer
  57. EOB
  58.  
  59. rawcheck=$(echo "$bundleids" | grep "^$bid;" 2>/dev/null)
  60. if [[ $rawcheck ]] ; then
  61.     procedure=$(echo "$rawcheck" | awk -F";" '{print $2}')
  62.     echo -e "*** APP LAUNCHED [$currentdate] ***\n$bid"
  63.     "$procedure"
  64. fi
  65.  
  66. exit
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement