Skip to content

Instantly share code, notes, and snippets.

Last active September 9, 2021 19:10
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save PeterFaiman/d8964912eeba66be60c0ea5d15577eab to your computer and use it in GitHub Desktop.
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) )
# :a modifier only works on paths that exist.
echo "$0: ${relative_path}: No such file or directory" >&2
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
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment