Script to empty downloadsfolder

I am trying to create a small script that lets me empty the downloads folder of my account. A feature is that I want to be able to specify through a dialog box how many days back i want to keep files.

The script looks like this:

on run
	repeat
		set dialogResult to (display dialog "Tast inn antall dager du vil at skal bevares" default answer "1")
		try
			set antallDager to (text returned of dialogResult) as number
			
			if (class of antallDager is integer) then exit repeat
			(* if (startNumber = startNumber as integer) then
			set startNumber to startNumber as integer
			exit repeat
		end if *)
		end try
		display dialog "The starting number needs to be a valid integer!" buttons {"Enter again", "Cancel"} default button 1
	end repeat
	tell application "Finder"
		try
			delete (every item of folder "Downloads" of folder "JSF" of folder "Users" of disk "OS X" whose modification date is less than (((get current date) - antallDager * days)))
		end try
	end tell
end run

Its my first script, so the code is probably far from “best practice”, but at the moment it does the trick except for 1 thing.

How can i get a hold of an items “Date Added” information? I see that you can sort your download folder by it, but in the Finder Dictionary in Apple Script Editor i can only find “modification date” and “creation date”.

Any inputs on this? THings to improve the script? How to change modification date to date added?

Kringon

Hi,

there is no date added information in the file system of OS X.
Take a look at this thread.
It describes a folder action which stores the date added information in a database.

Here’s a slightly optimized version of your script


repeat
	set {text returned:antallDager} to display dialog "Tast inn antall dager du vil at skal bevares" default answer "1"
	try
		set antallDager to antallDager as integer
		exit repeat
	on error
		display dialog "The starting number needs to be a valid integer!" buttons {"Enter again", "Cancel"} default button 1
	end try
end repeat
set downloadsFolder to path to downloads folder
tell application "Finder"
	delete (every item of downloadsFolder whose modification date is less than ((get current date) - antallDager * days))
end tell


Thanks for optimizing my newbie script :slight_smile:

I will check out the thread on the database and see if i can make a script that utilizes it.

Much appreciated.

Kringon