Trash Script

Hi everyone,

I wrote a script ages ago that I just found a major glitch with. Basically what the script does is check the desktop for any files and places them in the trash (without emptying). If the trash reaches a certain amount it empties. Simple right? Well since it’s a logout hook I’m attempting not to use Finder by relying on shell commands instead. The problem is if the user empties the trash BEFORE logging out the hidden trash directory will not exist and prompt the user of this. I’m trying to override this by quitting the program but it doesn’t seem to identify the paths non-existence as an error. This is what I have thus far:



tell app "Finder"
move (items of the desktop whose kind is not "Volume") to trash
end tell
do shell script "ls /Users/student/.trash"
if result is "" then 
tell me to quit
end if
else
do shell script "du -ks /Users/student/.trash"
if (first word of (do shell script "du -ks /Users/student/.trash") as number) > 50000 the do shell script "rm -r /Users/student/.trash/*"
end if

See if this helps.

set myPath to text
tell application "Finder"
	move (items of the desktop whose kind is not "Volume") to trash
end tell
do shell script "ls /Users/student/.trash"
if result is "" then
	set myPath to path to me
	quit
else
	try
		do shell script "du -ks /Users/student/.trash"
		if (first word of (do shell script "du -ks /Users/student/.trash") as number) > 50000 then do shell script "rm -r /Users/student/.trash/*"
	end try
end if

Tom

Hi Tom,

Thanks for the suggestion by changing the shell result to text, but still no luck. The outcome is the same. The dialog box I receive is “ls /Users/student/.Trash: No such file or directory”. This has been the problem all along.

Anyways I figured it out by including the shell commands coupled on the same line, while eliminating the list shell command. It’s not needed if Finder can determine the exists of the hidden trash directory. On the other hand the trick is to rely on the Finder as little as necessary for a logout hook. If not then the script only partially executes.

This statement made no sense to me. The .Trash folder is always there whether or not it has items in it. So I tried to write a short script and got the “No such file or directory” error that you talked about. I’m not sure why… but then I let applescript determine the paths for me and those errors went away. So somehow the error is not because the directory doesn’t exist, but because we were referring to the directory improperly.

The following script doesn’t cause any errors whether or not there are files on the Desktop or files in the Trash. Also it doesn’t use the Finder at all, and you mentioned that was a goal of yours.

set posix_trashFolder to POSIX path of ((path to home folder as text) & ".Trash:")
set posix_desktopFolder to POSIX path of (path to desktop)
set desktopItems to paragraphs of (do shell script "ls " & quoted form of posix_desktopFolder)

repeat with anItem in desktopItems
	set thisItem to posix_desktopFolder & anItem
	do shell script "mv " & quoted form of thisItem & space & quoted form of posix_trashFolder
end repeat

set theSize to first word of (do shell script "du -ks " & quoted form of posix_trashFolder) as number
if theSize > 50000 then do shell script "rm -r " & posix_trashFolder & "*"

Thank you Regulus for replying.

I researched POSIX paths while trying to enhance this script and figured it was part of the equation. Unfortunately though your script is not working on my machine and rendering the same result of directory not found when executing the “ls” UNIX command. I modified your ideas (while removing the “ls” - it’s only there to identify the trash directory and can be confirmed when running “du”) and started coming up with invalid arguments. Quite the challenge when we both know the hidden trash directory is available.

What worked for me is placing the du shell command in a try handler. As a result it shouldn’t prompt you that the script doesn’t see the directory. At least that’s my theory. I’m sure my theories hold little weight though since I’m still a newbie to Apple Scripting.

A better way to identify the trash folder would be:

set posix_trashFolder to POSIX path of (path to trash)

This should find the trash folder, wherever it is, or create it if (for some reason) it doesn’t exist.

:slight_smile: Thank you both Nigel and Regulus!!!

What happened is even after changing the POSIX path on the trash as suggested by Nigel, it would flag an error of stating the Desktop folder cannot be moved since it was already in the trash (which would happen every time the user logs out since it’s a logout hook). After I placed the mv in a try handler it worked like a charm…and the entire script never calls on Finder.

YOU GUYS ROCK.:cool: Thanks again.

Quick question on the matter of the trash.

How do you get the path to the trash on a particular mounted volume?

I have a mounted volume (hard drive), which has the folder".trashes" at the root but how does this work? I cannot get into it as I keep being denied access.

I want to move files from the volume to the volume’s trash using “mv” command.

Thanks

It should be hidden, but you shouldn’t be denied access or you’d never be able to empty your trash in the first place. I would make sure you’re not trying to delete the entire .trash directory, only its contents. If you look at the code that regulus6633 provided, you’ll see it’s removing the contents of that folder once it reaches 50MB and not the entire folder itself.

If the permissions have been inadvertently changed on the trash directory then I would run a repair permissions from Disk Utility.

Every volume has an invisible folder .Trashes on the root level of the volume.
The folder .Trashes contains one folder for each user named with the uid (501, 502. or 0 for root)
You have no permission for /Volumes/myVolume/.Trashes (it works like a dropbox), but assuming you are user 501 you have access to /Volumes/myVolume/.Trashes/501

What’s your purpose for moving files and folders to trash?
Either you want to delete the files immediately, then better use /bin/rm
or you want really to move the files to the trash folder to keep them, then better use the Finder’s delete command