2

I am currently trying to write an applescript to empty the trash without displaying the warning.

I used "empty trash" it works fine if it was running as script, but if I save it as application it displays the warning "Are you sure you want to permanently erase the items in the Trash?".

so I am not sure what to add in order to disable the warning from appearing when running as an application.

I am using mac OS X 10.10.5

thank you

sarah
  • 21
  • 1
  • 5

4 Answers4

3

The warning is a preference, but you can turn it off.

tell application "Finder"
    set warns before emptying of trash to false
    empty trash
end tell

Of course, if you want to be nicer, you could save the setting and restore it at the end.

JWWalker
  • 22,385
  • 6
  • 55
  • 76
  • If I look at **Finder** > **Preferences...** > **Advanced**, the **Show warning before emptying the Trash** check box **is** checked and from **Finder** > **Empty Trash** I am prompted, however not so when running the **AppleScript** _command_ `tell application "Finder" to empty trash`. I've tested this on 4 different versions of the **OS** with the conditions being the same in both an **Admin** and **Standard** account. Any idea why under the expressed conditions I'm not being prompted when using **AppleScript**? – user3439894 Feb 03 '18 at 17:32
  • 1
    I don't know why, but I can verify what sarah says: Running the script (without my change) in the Script Editor does not show the warning, but running the same script as an application does. – JWWalker Feb 03 '18 at 18:03
2

The following works for me without any prompt:

tell application "Finder" to empty trash
user3439894
  • 7,266
  • 3
  • 17
  • 28
  • I tried it and it display warning which is "Are you sure you want to permanently erase the items in the Trash?" how can I disable it. – sarah Feb 03 '18 at 16:34
  • @sarah, I've tested this under OS X 10.8.6, OS X 10.10.4, and macOS 1013.1 and I am not prompted, it deletes the trash immediately. So I can't believe that in OS X 10.10.5 it would prompt. – user3439894 Feb 03 '18 at 16:49
0

Offering an alternative just for joy of having it out there:

do shell script "rm -R ~/.Trash"

for local trash, with the additional emptying of iCloud trashed files using:

do shell script "rm -R ~/Library/Mobile\ Documents/com~apple~CloudDocs/.Trash"

Sadly, there’s no sound effect with these.

NB. These we’re tested on MacOS 10.13. The first command will work on earlier systems. The second will depend on how your system integrates with iCloud, but hold more relevance for Sierra and High Sierra users that opt to have Desktop and Documents folders stored in iCloud.

CJK
  • 5,732
  • 1
  • 8
  • 26
0
On idle 
tell application "Finder"
   set my count to count of items of the trash
   If my count > 1 then
     set warns before emptying of trash to false
     empty trash
   End if
end tell
 Return 60 
End idle
Laurel
  • 5,965
  • 14
  • 31
  • 57
  • Note that the `idle` handler requires a stay open application, which would mean that your app would constantly be emptying the trash. – red_menace Dec 26 '22 at 20:13
  • Users find it difficult to understand code only answers with no explanation. Please consider adding some description explaining how it solves the problem or add comments in the source code at appropriate places. – Azhar Khan Dec 31 '22 at 06:15
  • Okay, sorry for the lack of info. Being a new user, I got a little over enthusiastic. – Jason Magnuson Jan 02 '23 at 21:30
  • In my script, I am using Mac OS Ventura. I used an On idle handler to let it keep running in the background as a lot of times I will move files to trash folder. The if statement is to prevent an error message from popping up when it reaches the next run cycle after 60 seconds. The set warns statement prevents that annoying "are you sure you want to delete?" message from popping up each time it checks the trash bin. – Jason Magnuson Jan 02 '23 at 21:33