Skip to content

Commit

Permalink
Implement dynamic database path detection for multi-user support
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolodiamante committed Aug 5, 2024
1 parent 43839df commit 4ef5588
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,18 @@ This command will instruct Prune to specifically remove the `"QuickTime Player"`

### Accessing Help

Prune comes with a built-in help option to provide quick access to its usage instructions right from the terminal. Whether you're unsure about how to reset the Launchpad layout or need a reminder about how to launch Prune, the help option is there to assist you. To access this, simply type `prune --help` or `prune -h` in the terminal. This will display a summary of available options and how to use them.<br><br>
Prune comes with a built-in help option to provide quick access to its usage instructions right from the terminal. Whether you're unsure about how to reset the Launchpad layout or need a reminder about how to launch Prune, the help option is there to assist you. To access this, simply type `prune --help` or `prune -h` in the terminal. This will display a summary of available options and how to use them.
<br><br>

## What's new in Prune

### v1.1

Release Highlights:

- Dynamic Database Path Detection: The script now dynamically finds the correct com.apple.dock.launchpad database path for the current user. This enhancement ensures that the script works seamlessly on systems with multiple users, addressing the issue where the incorrect path might be selected.

<br><br>

## Notes

Expand Down
24 changes: 21 additions & 3 deletions script/prune.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,26 @@ for cmd in "${required_commands[@]}"; do
fi
done

# Function to find the correct launchpad database path for the current user.
find_launchpad_db() {
local user_db_paths
user_db_paths=($(find /private/var/folders -name com.apple.dock.launchpad 2>/dev/null))
for path in "${user_db_paths[@]}"; do
if [[ -d "$path" && -w "$path" ]]; then
echo "$path/db/db"
return 0
fi
done
echo "Error: unable to find writable Launchpad database." >&2
exit 1
}

# Function to remove a single app.
remove_single_app() {
local app_to_remove=$1
local sqlite_cmd="sqlite3 \$(find /private/var/folders -name com.apple.dock.launchpad)/db/db \"DELETE FROM apps WHERE title='$app_to_remove';\" && killall Dock"
local launchpad_db=$(find_launchpad_db)

local sqlite_cmd="sqlite3 ${launchpad_db} \"DELETE FROM apps WHERE title='$app_to_remove';\" && killall Dock"

if ! sudo zsh -c "$sqlite_cmd" 2> "${LOG_FILE}"; then
echo "Failed to remove ${app_to_remove}. Check log for details." >&2
Expand Down Expand Up @@ -97,6 +113,8 @@ fi

# Function to update pruneops.zsh if the apps file has changed.
update_pruneops() {
local launchpad_db=$(find_launchpad_db)

# Read the new list of applications from the applications file.
applications=("${(@f)$(awk -F"'" '/\047/ {print $2}' "$APPLICATIONS_FILE")}")

Expand All @@ -106,11 +124,11 @@ update_pruneops() {

# Build the cleanup commands.
for app in "${applications[@]:0:${#applications[@]}-1}"; do
echo -n "sqlite3 \$(find /private/var/folders -name com.apple.dock.launchpad)/db/db \"DELETE FROM apps WHERE title='$app';\"; " >> "${TEMP_FILE}"
echo -n "sqlite3 ${launchpad_db} \"DELETE FROM apps WHERE title='$app';\"; " >> "${TEMP_FILE}"
done

# Add the last command without the semicolon (;) divider, followed by '&& killall Dock'.
echo -n "sqlite3 \$(find /private/var/folders -name com.apple.dock.launchpad)/db/db \"DELETE FROM apps WHERE title='${applications[-1]}';\" && killall Dock" >> "${TEMP_FILE}"
echo -n "sqlite3 ${launchpad_db} \"DELETE FROM apps WHERE title='${applications[-1]}';\" && killall Dock" >> "${TEMP_FILE}"

# Replace the existing pruneops.zsh file with the new commands
mv "${TEMP_FILE}" "${PRUNEOPS_FILE}"
Expand Down

0 comments on commit 4ef5588

Please sign in to comment.