Force emptying the trash on macOS by calling shell script

If I run this shell command in macOS (11.2.1) Terminal:

[format]sudo rm -rf ~/.Trash/*[/format]

… after entering my Mac password in the system dialog, the trash is successfully forced empty.

But if I try to force empty the trash from this AppleScript, the system dialog appears OK, and the notification works as expected, but the trash is not force-emptied:

[format]do shell script “/Users/nev/Documents/shell-scripts/force-empty-trash.sh”[/format]

The “force-empty-trash.sh” shell script being referenced is:

[format]#!/bin/bash
if osascript -e “do shell script "sudo rm -rf ~/.Trash/*" with administrator privileges”;
then
osascript -e “display notification "Trash was forced empty." with title "OK"”
else
osascript -e “display notification "Trash was NOT forced empty!" with title "ERROR"”
fi[/format]

The shell script shows the same behaviour when run from Terminal.

Anyone have any ideas?

Hi. Like the terminal, the shell requires a password for sudo.

set wordpass to (display dialog "An admin password is requested for super user access." with icon stop with title "Take heed!" default answer "password")'s text returned
do shell script "printf " & wordpass's quoted form & space & "| sudo -S env_reset,timestamp_timeout=0 rm -rf ~/.Trash/*"
set wordpass to ""

Yes I know that for sudo the password must be entered, and the calling of the shell script from the AppleScript evokes the system dialog, where that password is entered. Yes, your script works. I’m still wondering why my attempt doesn’t - using my method, the shell command [format]rm -rf ~/.Trash/*[/format] is not executed, despite the password being entered.

I believe that your attempt was also directed to the wrong shell. I called your osascript, sans the first line—#!/bin/bash—from my desktop with the below code, and it worked as expected, including the prompt.

do shell script "/bin/sh " & (choose file)'s POSIX path's quoted form

The “do shell script” command has the ability to run as an administrator

try this

do shell script "rm -rf ~/.Trash/*" user name "MyUser" password "test" with administrator privileges

This will run the command as root, provided “MyUser” is in the sudoers file

Hm. I can’t reproduce your success with that — if I’m reproducing it correctly, that is.

That’s interesting, thanks for the tip. Though, I think I’d rather have a dialog for the password than embed it in the code.

OK!

Here’s one with a prompt for password


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set pwd to ""
set pwd to display dialog "Please enter password for user MyUser…" default answer pwd with title "Password" with hidden answer
if button returned of pwd is "OK" then
	set pwd to text returned of pwd
	do shell script "rm -rf ~/.Trash/*" user name "MyUser" password pwd with administrator privileges
end if

Here is a better version that will loop on given a bad username/password combo until user cancels or gives valid credentials

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set pwd to ""
set dialogText to "Please enter password for user MyUser."
repeat
	set pwd to display dialog dialogText default answer pwd with title "Enter Password…" with hidden answer
	if button returned of pwd is "OK" then
		set pwd to text returned of pwd
		try
			do shell script "rm -rf ~/.Trash/*" user name "MyUser" password pwd with administrator privileges
			exit repeat
		on error errStr number errorNumber
			if errorNumber ≠ -60007 then -- The administrator user name or password was incorrect.
				exit repeat
			end if
			set dialogText to "Error! The administrator user name or password was incorrect." & return & "Try again…"
		end try
	end if
end repeat

You’re going to hate me saying so but neither of those work! Maybe it’s macOS 11 ?

:expressionless:

Make sure the loaded file is plain text. If you happen to be using Catalina, you may have a different default shell—zsh. This isn’t a necessary change, but avoiding the manual quoting is desirable.

[format]if osascript -e ‘do shell script “sudo rm -rf ~/.Trash/*” with administrator privileges’;
then
osascript -e 'display notification “Trash was force emptied.” with title “OK” ’
else
osascript -e 'display notification “Trash was NOT force emptied!” with title “ERROR” ’
fi[/format]

Edited for clarity

Sorry, I had a test command i was using - “whoami”.
I put back “rm -rf ~/.Trash/*”

Try it now

also have you tried the empty trash command in the Finder

tell application "Finder" to empty trash

Well, I’ve tried many which ways, and post #2 from Marc Anthony works, which is always a plus! Thank you for that!

I was trying to get the external script working, so as to have the benefit of some sort of feedback notification, instead of it just working silently, but no variation of shell or whatever seems to work for that. Maybe an AppleScript equivalent is possible.

But that working script is obviously better than no result at all!

Thanks very much all for your tips and help.

:slight_smile:

You can use regular dialog statements in a try block.

set wordpass to (display dialog "An admin password is requested for super user access." with icon stop with title "Take heed!" default answer "password")'s text returned
try
	do shell script "printf " & wordpass's quoted form & space & "| sudo -S env_reset,timestamp_timeout=0 rm -rf ~/.Trash/*"
	set wordpass to ""
	display dialog "The trash was force emptied."
on error
	display dialog "That force emptying didn't go so well."
end try