Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Use OS X's trash in a Finder-like way from Terminal' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Use OS X's trash in a Finder-like way from Terminal
Authored by: Sesquipedalian on Apr 22, '10 08:58:19AM

This code looks like it will be very helpful.

However, instead of putting it into .bashrc (or .bashprofile or .profile), I plan to turn it into some executable scripts and save them in /usr/local/bin. That way they will be available to all users and to any type of script that I might want to write.



[ Reply to This | # ]
Separated scripts, instead of functions
Authored by: GaelicWizard on Apr 22, '10 04:50:54PM
I've been meaning to do that for a while, especially since these functions are basically self-contained. Their only dependencies are on seq(). I'll update the SourceForge page with that at some point.

[ Reply to This | # ]
Use OS X's trash in a Finder-like way from Terminal
Authored by: Sesquipedalian on Apr 23, '10 10:50:32AM
It occurred to me after posting my previous comment that there is a much simpler way of accomplishing the goals of this hint. Moreover, this alternative method avoids the possible problems that others have posted about here, since it works not by emulating the process Finder uses when trashing things, but by actually having the Finder itself do the job.

To send files to the Trash using the command line, save the following code in a file on your Desktop called trash, make it executable (chmod +x ~/Desktop/trash) and store it somewhere on your bash $PATH (I recommend /usr/local/bin)

#!/bin/bash
for arg in "$@"
do
if [ -e "$PWD/$arg" ]
then
osascript -e "tell application \"Finder\" to delete POSIX file \"$PWD/$arg\"" &>/dev/null
elif [ -e "$arg" ]
then
osascript -e "tell application \"Finder\" to delete POSIX file \"$arg\"" &>/dev/null
else
echo File not found
fi
done

To empty the trash, this code will work nicely:
#!/bin/bash
osascript -e "tell application \"Finder\" to empty trash"

To empty the trash securely, use this:

#!/bin/bash
osascript -e "tell application \"Finder\" to empty trash with security"

EDIT: Updated the trash script's code to handle relative path arguments.

Edited on Apr 23, '10 11:31:41AM by Sesquipedalian


[ Reply to This | # ]
Just script the Finder via osascript
Authored by: GaelicWizard on Apr 24, '10 10:58:35AM

But, of course, this method fails completely in any situation where Finder won't cooperate, such as:
(1) when Finder isn't running.
(2) when AppleScript (via osascript) won't work, e.g. when ssh'd in.
(3) when Finder is blocked waiting for something else, e.g. with a modal dialogue box on screen.

JP

Edited on Apr 24, '10 10:59:23AM by GaelicWizard



[ Reply to This | # ]