Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
trash: ZSH function and AppleScript to send files and folders to the MacOS Trash
trash() {
# AppleScript POSIX file needs absolute paths.
local -a absolute_paths
for relative_path in "$@"; do
if [[ -e "$relative_path" ]]; then
# :a - ZSH absolute path modifier.
absolute_paths+=( "$relative_path"(:a) )
else
# :a modifier only works on paths that exist.
echo "$0: ${relative_path}: No such file or directory" >&2
fi
done
osascript ~/Code/AppleScripts/TrashFiles.applescript "${absolute_paths[@]}"
}
on run args
set theFiles to {}
-- POSIX file throws an error inside tell Finder, so build a list
-- before entering tell Finder.
repeat with thePath in args
set the end of theFiles to POSIX file thePath
end repeat
tell application "Finder"
repeat with theFile in theFiles
delete theFile
end repeat
end tell
return
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment