I wrote this last night, hope it helps, Should be up on github soon.
The script is designed to help manage macOS applications installed on a system, particularly those that are available through Homebrew Cask. The main purpose of the script is to identify applications that are installed on the system but not managed by Homebrew Cask, and provide the user with an option to install those applications using Homebrew Cask.
#!/usr/bin/env bash
# Change the current directory to /Applications, where most macOS applications are installed
cd /Applications
# Generate two text files: apps.txt and casks.txt
# apps.txt contains a list of applications already installed using Homebrew Cask
# casks.txt contains a list of all available Homebrew Cask applications
brew list --cask > apps.txt
brew search --casks "" > casks.txt
# Iterate through all .app files in the /Applications directory
for f in *.app; do
# Check if the application name is present in the casks.txt file (available through Homebrew Cask)
# and not present in the apps.txt file (not currently installed using Homebrew Cask)
if grep -Fixq "${${f%.*}// /-}" casks.txt && ! grep -Fixq "${${f%.*}// /-}" apps.txt; then
# Assign the original file name to a variable
original_file_name="$f"
# Assign the Homebrew Cask-friendly application name to a variable
brew_app_name="${${f%.*}// /-}"
# Prompt the user to confirm the installation
while true; do
read -p "Do you want to install '$brew_app_name'? (y/n/b) " confirmation
case "$confirmation" in
[yY])
echo "Installing '$brew_app_name'..."
brew cask install --force "$brew_app_name"
break
;;
[nN])
echo "Skipping installation of '$brew_app_name'."
break
;;
[bB])
echo "Backing up '$original_file_name' to /Users/macuser/appbackups..."
if [ ! -d "/Users/macuser/appbackups" ]; then
echo "Creating /Users/macuser/appbackups directory..."
mkdir -p "/Users/macuser/appbackups"
fi
mv "$original_file_name" "/Users/macuser/appbackups/$original_file_name"
echo "Installing '$brew_app_name'..."
HOMEBREW_NO_AUTO_UPDATE=1 brew cask install --force "$brew_app_name"
break
;;
*)
echo "Invalid response. Please try again."
continue
;;
esac
done
fi
done
# Remove the temporary files
rm apps.txt casks.txt