Last active
September 9, 2021 19:10
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[@]}" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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