Read trash size before empty? My AppleScript worked before Catalina

My applescript which reads out the size of the trash before I empty it by pressing shift-command-deleted use to work before Catalina update. Can anyone tell me why it doesn’t any more? it works, it empties but it only speaks MB, does not speak the size of the trash as well like it use to.

set amount to do shell script "printf 'scale=0;%s/1024
' `du -sk ~/.trash | awk '{print $1}'` | bc -l "
say "" & amount & "MB" speaking rate 260
display dialog " " & return & "" & return & "  There are " & amount & " MB in the Trash" & return & " " & return & "   Would you like to empty it?" & return & " " & return & "   " with icon 0 giving up after 10
if button returned of result = "OK" then
	tell application "Finder"
		empty trash
	end tell
end if

It works nicely for me on Catalina (10.15.2).
L.

Maybe the sound level was set to 0 ?

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 20 décembre 2019 09:26:31

On my 10.15.2 it just says MB (so sound is ON) but doesn’t read out the size of the trash like it has been before Catalina.

  1. Check existing of printf and awk tools at location /usr/bin/
  2. If they exist, try get the full paths to them: /usr/bin/printf and /usr/bin/awk

NOTE: the script rounds the size to Mbytes, so if size of trash is <500 Kbytes, then the script should say 0 Mbytes.

Just checked and I have printf & awk tools in /usr/bin/

Just tried adding full path to script: /usr/bin/printf & /usr/bin/awk… no luck

And the script doesn’t mention any size, it use to work fine. I have over 150 MB in the trash at the moment

Any other suggestions?

PS I recently reinstalled Catalina from recovery. All of a sudden I had some weird permission issues, I couldn’t delete anything from my desktop folder. Now I wonder if that’s when this script issue started as well, maybe it wasn’t Catalina upgrade like I thought

Make sure the app or editor you’re running the script from has Full Disk Access in System Preferences.

The OP’s script works on Catalina for me. Perhaps this is a timing issue and a delay would help?

An alternative to try is something along the lines of:


repeat 5 times # 5 times to avoid endless loop
	set trashSize to size of (info for (path to trash folder))
	if trashSize is not missing value then exit repeat
	delay 0.2
end repeat

if trashSize = 0 then # as generally suggested by KniazidisR
	say "No files in the trash" speaking rate 260
else if trashSize < 1048576 then
	say "Less than one megabyte" speaking rate 260
else
	set trashSize to ((trashSize / 1048576) as integer) as text
	say trashSize & "megabytes" speaking rate 260
end if

--rest of script

For reference see:

https://macscripter.net/viewtopic.php?id=32210

Finder isn’t needed here as info for is a standard additions command.

Thanks CK. I’ve edited my script to reflect that correction.

I was receiving some odd results when using the Finder to get the size of files in the trash folder. So, I ran the following script:

Main()
on Main()
	
	set mbConversion to 1048576
	
	tell application "Finder"
		set trashSize to size of (path to trash folder)
		set physicalTrashSize to physical size of (path to trash folder)
		set dataTrashSize to data size of (path to trash folder)
	end tell
	
	((trashSize / mbConversion) as integer) & ((physicalTrashSize / mbConversion) as integer) & ((dataTrashSize / mbConversion) as integer)
end Main

My script returns:

{0, 0, 0} when trash is empty

{7, 7, 0} when trash contains 7 MB

{7, 7, 0} after emptying 7 MB in trash with Finder window not open

{0, 0, 0} after emptying 7 MB in trash with Finder window open and hidden trash folder visible

So, at least for me, the “info for” command or the “du” command-line utility seem the best alternatives to get the size of all files in the trash. I tested these alternatives in Script Geek and the “info for” alternative was faster.

An additional option is to use the Finder (System Events didn’t work) to get a list of files and folders in the trash and then to obtain and sum the size of these items. I tested this in Script Geek and it was slower than “info for”, although none of these alternatives was really slow IMO.

set trashPath to (path to trash folder)
set fileSizeTotal to 0

tell application "Finder"
	set trashFiles to items in trashPath
	repeat with i from 1 to (count trashFiles)
		set fileSize to size of (item i of trashFiles)
		set fileSizeTotal to fileSizeTotal + fileSize
	end repeat
end tell

set fileSizeTotal to (fileSizeTotal / 1048576) as integer

At :
https://app.box.com/s/ygdvy0e6xgurhf3zsq5nv25iadfhx1w4
you may grab the library “infoForAndMoreLib” built with the efficient help of Shane Stanley.
Download it
expand the archive
install it in : “/Library/Script Libraries/” folder or in “~/Library/Script Libraries” folder
Maybe you will have to create the subfolder “Script Libraries”.

After that you will be able to grab numerous useful infos about files, folders, volumes.

For instance you may run the simple:

----------------------------------------------------------------
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use script "infoForAndMoreLib" version "1.0.1"
use scripting additions
----------------------------------------------------------------

Main()

on Main()
	set trashPath to (path to trash folder)
	size of trashPath --> "36880"
	allocated size of trashPath --> "45056"
end Main

to get the two relevant values about the size of the trash.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 21 décembre 2019 16:18:42

Ok I tried everything above without luck. Even reset all permissions and the give it back to script editor and the script and now when I run my original script in script editor the wheel starts spinning at the bottom with word running… and it never finished. I can’t even stop it with the STOP button. I have to quit script editor to stop the script.

I installed library and now I can get the size of the trash in bytes, how would I implement below into the my original script? It would be nice if it spoke the size in MBs not bytes as well.

----------------------------------------------------------------
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use script "infoForAndMoreLib" version "1.0.1"
use scripting additions
----------------------------------------------------------------

Main()

on Main()
	set trashPath to (path to trash folder)
	size of trashPath --> "36880"
	allocated size of trashPath --> "45056"
end Main

I found this simple script which actually does return the correct size of the trash in MB:

set folderToSync to "~/.trash"
set folderToSyncSize to (do shell script "du -h " & folderToSync & " | tail -rn1 | awk '{print \"\" $1}'")

Can this be implemented into my original script? Sorry for newbee questions.

mymac. I don’t know why your script doesn’t work, but I thought I’d give it one more try.

To test this script, just paste it into script editor and run. I included a one second delay for test purposes, and, if by chance the script works, you can try it without the delay. As currently written, the script will not report a gigabyte of trash, although this can be fixed. Also, just for testing purposes, I simplified the dialog.

Edit: December 28, 2019. I wrote a new version FWIW. I was able to get it to run under Catalina 1) from within Script Editor, 2) if saved as a script and run by way of Fastscripts, and 3) if saved as an app and added to Accessibility and Full Disk Access in Security & Privacy. Otherwise, the du utility reported an operation-not-permitted error.

 set trashFolder to POSIX path of (path to trash folder)

try
	set theResult to (do shell script "du -m " & quoted form of trashFolder)
on error errorMessage
	display dialog "The du utility reported the following error." & linefeed & linefeed & errorMessage buttons {"OK"} cancel button 1 default button 1
end try

delay 0.5 -- experiment with no delay and with various delay values

set trashSize to the first word of the last paragraph of theResult

if trashSize = "0" then
	say "No files in the trash" speaking rate 260
	display dialog "The trash contains no files." buttons {"OK"} cancel button 1 default button 1 with icon 0 giving up after 10
else if trashSize = "1" then
	say "Less than one megabyte" speaking rate 260
	display dialog "The trash contains less than one megabyte." buttons {"Cancel", "Empty Trash"} cancel button 1 default button 2 with icon 0 giving up after 10
else
	say trashSize & " megabytes" speaking rate 260
	display dialog "The trash contains " & trashSize & " megabytes." buttons {"Cancel", "Empty Trash"} cancel button 1 default button 2 with icon 0 giving up after 10
end if

if button returned of result = "Empty Trash" then
	tell application "Finder" to empty trash
end if

running the script above give me error “The variable result is not defined.” number -2753 from result. PS I have 293 MB in the trash, its not empty or anything

.

mymac,

Run this snippet on your machine and say what result you get:

set amount to do shell script "printf 'scale=0;%s/1024
' `du -sk ~/.trash | awk '{print $1}'` | bc -l "

I get “154”. PS trash currently has 162 MB if I check trash folder info page

.

Ok I went and deleted the trash, restarted my laptop, put more stuff in the trash and ran my original script in script editor just now and it works and it speaks the size of the trash like it use to.

I am totally confused why now its working again lol. But thanks for all your guys help. Maybe the script will come in useful for other people on here as well :slight_smile:

PS we can keep this thread going if you guys think there are ways to improve it?

I just saw your message. You may use:

----------------------------------------------------------------
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use script "infoForAndMoreLib" version "1.0.1"
use scripting additions
----------------------------------------------------------------

Main()

on Main()
	set trashPath to (path to trash folder)
	# get the size in bytes
	set itsSize to size of trashPath --> "8737864"
	log result
	set MbytesSize to my bytesToMbytes(itsSize)
	set itsAllocatedSize to allocated size of trashPath --> "9883648"
	log result
	set MbytesAllocatedSize to my bytesToMbytes(itsAllocatedSize)
	{MbytesSize, MbytesAllocatedSize} --> {"8,7 Mo", "9,9 Mo"}
end Main

on bytesToMbytes(bytesCount)
	return (current application's NSByteCountFormatter's stringFromByteCount:bytesCount countStyle:(current application's NSByteCountFormatterCountStyleDecimal)) as text
end bytesToMbytes

As you may see, it uses the local settings to display the results.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 26 décembre 2019 12:05:46

It appears likely that the error occurred because the du command-line utility could not read something in the trash. It would have been interesting to know what that was. I guess another explanation is that the path to the trash folder in my script is incorrect, although it works for me. Anyways, I’m glad you got your script working.