Waiting A Process on Logout

I’ve written a logout hook (with assistance from StefanK) that was successful, but recently found after a little beta testing that the script ends prematurely (while emptying the trash) without waiting for all processes to complete. After making some adjustments I’ve discovered how to make the script wait but it ends up crashing Finder (which also kills the logout process).

This is what I have thus far (which does not wait for completion of “empty trash” and suppresses any error windows that Finder flags when trying to logout and clearing the trash bin simultaneously):

tell app “Finder”
move (items of the desktop whose kind is not “Volume”) to trash
do shell script “du -ks /Users/student/.Trash”
if (first word of(do shell script “du -ks /Users/student/.Trash”)as number) > 50000 then empty trash
delay 2
if (first word of(do shell script “du -ks /Users/student/.Trash”)as number) < 150 then repeat
end repeat
on error
tell app “Trasher App” to close (first window whose frontmost is true) in window 1
end try
end tell
end if

So should I have System Events keeping track of the “Trash” window process?? If so how do I prevent this (or any other modification to the script) from interfering with the logout process in a logout hook??!


I strongly recommend not to involve the Finder or System Events in a logout hook.
Wouldn’t it be easier to use the shell for everything

do shell script "rm -r /Users/student/Desktop/*" -- deletes all items on desktop (volumes don't exist on desktop in the shell)
if (first word of (do shell script "du -ks /Users/student/.Trash") as number) > 50000 then do shell script "rm -r /Users/student/.Trash/*"

the second line empties the trash immediately if the size of the trash folder is greater than 50000

Or, even better, use a shell script without AppleScript

Thank you Stefan, your insight is extremely helpful.

I’ve been trying to steer away from most of the vanilla script anyways and script primarily with UNIX. It’s just a learning process of not only commands but switches as well.