Skip to content

Instantly share code, notes, and snippets.

@luluco250
Last active July 8, 2022 23:07
Show Gist options
  • Save luluco250/11a89603e8e8c03d982a5b5637997e0a to your computer and use it in GitHub Desktop.
Save luluco250/11a89603e8e8c03d982a5b5637997e0a to your computer and use it in GitHub Desktop.
File deletion service script for Finder on macOS
#!/bin/bash
#################
## Description ##
#################
# This service script will allow you to select some files and folders and
# delete them permanently (bypassing the trash), with a confirmation prompt.
#
# To install the script:
# 1. Open Automator.
# 2. Create a "Quick Action".
# 3. Add a "Run Shell Script" action.
# 4. Set "Workflow receives current" to "files or folders".
# 5. Set "in" to "Finder".
# 6. Set "Shell" to "/bin/bash".
# 7. Set "Pass input" to "as arguments".
# 8. Paste this script in the script text box.
# 9. Save as "Delete".
#
# After that, you can bind a keyboard shortcut to it by doing the following:
# 1. Open System Preferences.
# 2. Click on Keyboard.
# 3. Go to the Shortcuts tab.
# 4. Click on "Services".
# 5. Find the "Delete" service.
# 6. Click on it and then click on "Add shortcut".
# 7. Press the shortcut you want to use to delete files.
#
# Notes:
# - Delete/backspace don't seem to be allowed for custom shortcuts.
# - I use and can recommend "Command+Shift+X" since it's simple and memorable.
# - The first time you try to run the service, it will show a macOS prompt to
# allow the script to run, this is normal and only happens once per app (and
# since we're only using it on Finder, only *once*).
# -
###################
## Configuration ##
###################
# Set to 0 to disable the confirmation prompt (be careful as this will make
# it DELETE any file selected PERMANENTLY without warning).
confirm=1
############
## Script ##
############
# Quit early if no arguments are received.
[[ $# == 0 ]] && exit 0
items=("$@")
function delete_items {
# Remove the selected files and folders recursively.
rm -rf "${items[@]}"
}
# If the confirmation dialog is disable, let's just go ahead and delete the
# items.
[[ $confirm == 0 ]] && delete_items && exit 0
# Title and text for the message box.
# We'll replicate Windows' behavior and show different messages if the
# selected item is a folder, file or if there are multiple items.
if [[ $# == 1 ]]; then
# If the item doesn't exist let's drop everything and quit.
if ! [[ -e "$1" ]]; then
exit 0
fi
# If the file's been modified since its creation, we'll print the modified
# time, else the created time.
created=$(stat -f %B "$1")
modified=$(stat -f %c "$1")
if [[ "$created" != "$modified" ]]; then
date="Date modified: $(date -r $modified +%c)"
else
date="Date created: $(date -r $created +%c)"
fi
# Get only the file name from the path.
name=$(basename "$1")
# If we're deleting a folder...
if [[ -d "$1" ]]; then
title='Delete Folder'
text="\
Are you sure you want to permanently delete this folder?\n\
\n\
$name\n\
$date"
# ...or a file.
else
size=$(du -h --si "$1" | awk '{ print $1 }')
title='Delete File'
text="\
Are you sure you want to permanently delete this file?\n\
\n\
$name\n\
$date\n\
Size: $size"
fi
else
title='Delete Multiple Items'
text="Are you sure you want to permanently delete these $# items?"
fi
# AppleScript to show an ok/cancel message box to confirm deletion.
script="display dialog \"$text\" with title \"$title\""
# Run the script and suppress any error output in case the user cancels.
response=$(osascript -e "$script" 2>/dev/null)
# If we get a response that means the user confirmed the prompt, so let's delete
# the items, otherwise we'll quit gracefully.
[[ -n "$response" ]] && delete_items || exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment