I have an AppleScript that first calls an Automator application, then executes the remaining actions of the script, then at the very end there is this.
tell application "Finder"
empty trash
end tell
The issue is that there are times there may not be anything in the Trash (the remaining part of the script checks a number of folders and deletes any files over 90 days old, so it’s a hit or miss if there’s any items/files in the trash). But running from ScriptDebugger, the script just stops dead if there’s nothing in the trash. I confirmed this by creating a folder on the Desktop, moved it to the Trash, and the script completed perfectly.
Is there a way to do a “try” statement with Finder as far as emptying the trash. Another words, if there’s items in the Trash, then empty, if not, move on.
Is there a way to do a “try” statement with Finder as far as emptying the tras
You mean like:
tell application "Finder"
try
empty trash
end try
end tell
Not sure it should be necessary though. OMM ‘empty trash’ fails silently if there’s nothing to be trashed. Either way, this is the standard try syntax.
Homer712. I never noticed this before, but I see the same behavior with both Script Editor and Script Debugger. The try statement is probably the simplest solution, but the following is an alternative:
tell application "Finder"
if (count items in trash) is not 0 then empty trash
end tell
That worked with the script running in ScriptDebugger, although once turned into an application, the original (without the “try”) worked also. Must be one of those can’t figure this out things.
It’s another good day, learned something new, thanks!
The error probably occurs in the applet too. But unless it’s trapped and displayed, the applet will simply crash out without complaining. And since this happens right at the end of the script, you don’t see any difference.
This also appears to work in the editors:
tell application "Finder"
delete items of trash
end tell
It returns no result if there are items in the trash and an empty list if not.
I have another thread here: Looking for Help with Dialog Box
I eventually gave up, as I could not reliably get the notifications to come up. Your post gave me an idea, that since each backup had the “empty trash” just before the call for the notification to come up, the “empty trash” was silently failing before the script ever got to the notification request. Sure enough, changed from the “empty trash” to the “try empty trash” and the notifications now come up perfectly.
So it seems that with one post, you have solved two issues for me, thank you.
FWIW, the following is another solution that works when run in Script Debugger and when saved and run as an AppleScript app.
tell application "Finder"
ignoring application responses
empty trash
end ignoring
end tell
The above script takes about half a millisecond to run when the trash is empty as compared with 4 milliseconds for the version that uses a try statement. I didn’t test this, but I would expect the difference to be much greater if there are a significant number of files in the trash. A few milliseconds one way or another is not significant, though.
Hi @peavine.
A major reason why ignoring application responses is so fast is that it tells the script not to wait for response(s) from the application(s) being sent the enclosed command(s). Normally, a script will wait for an application to signal back that it’s either completed a command or not before moving on to the next instruction. This helps to ensure that things happen in the right order. Not waiting for a response may or may not be a good way to ignore possible errors. It depends on how long it takes the application to carry out the command successfully and what comes next in the script.