Trash Folder Testing Problems

I would like to test whether the Trash folder is empty.

I note that the below Apple Script properly returns Empty of Full.

tell application "Finder"
	if (count of items in trash) > 0 then
		return "Full"
	else
		return "Empty"
	end if
end tell

I tried replicating this in Keyboard Maestro using the below action but note that this always evaluates to Full regardless of whether the Trash is empty or full.

Would love to i) know why and ii) understand the fix.

Thank you.

1 Like

Go to the trash can in your office (or maybe kitchen, if WFH!). Does it exist? Now lift the lid and look inside -- is there anything in there? If there is, take it all out and look again -- does the now-empty can still exist or has it mysteriously vanished?

In the AppleScript you're asking about the items in Trash, in KM you are asking about Trash itself.

@Nige_S , appreciated noting:

  1. Interesting and helpful as I see the logic in your response. I wrongly thought that the condition was checking inside the .Trash folder (hence my confusion) but clearly this is not the case.

  2. Is there an easier way using Keyboard Maestro to determine whether a folder is empty than the For Loop that I constructed below as I cannot seem -- I could be wrong -- to be able to find a Keyboard Maestro Case Statement or If Then condition that does so.

Thanks.

I'm not sure about the best way using fully native Keyboard Maestro actions (i.e. no scripts), but there are times when a shell script really might be the quickest way to the answer:

ls -l ~/.Trash | head -1 | cut -f 2 -d " "

Put that in a shell script action and save the result to a variable, then check if that variable is zero. If it is, the trash is empty.

Alternatively, use it directly in an If-Then action:

The action is quite fast—it takes a few hundredths of a second on my Mac Studio (2023).

-rob.

1 Like

Note however that this method ignores dotfiles (files that have names beginning with a dot, and which are invisible by default in the Finder). In the unlikely event that that should matter to OP, changing ls -l to ls -la would fix that, in the case of the Trash folder. For other folders, which might contain dotfiles that should be kept, I can imagine that the macro would need some tweaks that would add complexity, so I would go with the Applescript method for those.

That's the KM "native" way that's analogous to the AppleScript. You can speed it up because you don't care how many items are there, just if there's at least one.

You still have to make the initial Collection, but its only of items at the top-level of Trash.

You could, of course, use an "If" Action based on your AppleScript. But then you've got AppleScript instantiation costs.

Or an "If" with a shell script. You don't have to go as complicated as @griffman's because ls in a non-interactive shell (such as in a shell script Action) will return one item per line with no cruft and the empty string if there is nothing to list. So all you need is:

ls ~/.Trash

Or if Finder is the frontmost app, test the menu item:

Choices, choices...

2 Likes

@Nige_S , @griffman , @kevinb

Appreciated and very helpful.

Many choices, just need to pick the best for the circumstances!

Thank you.

2 Likes