Applescript database?

Is there any chance one of you script gurus know if the following is possible? I need to manage the contents of a folder by date, any item (file or directory) that is put into the folder needs to be deleted after 14 days. But the problem is we can’t use the items modification or creation date. As some of these items will older than 14 days, also we can’t modify the date either as the date must stay the same.

So, is there a way I can track the contents of a folder, see when items were placed in it and then know when to remove them? Im guessing this would need a database of some kind?

Hi,

as AppleScript is able to manage databases with Database Events, try this.
It’s a folder action, the script checks and deletes the files every time when new files are added


property databaseName : "folderManager"

on adding folder items to this_folder after receiving added_items
	set twoWeeksAgo to (current date) - 2 * weeks
	tell application "Database Events"
		try
			set dbase to database databaseName
		on error
			set dbase to (make new database with properties {name:databaseName})
		end try
		
		tell dbase
			set recordsToDelete to every record whose value of field "date added" < twoWeeksAgo
		end tell
		repeat with oneRecord in recordsToDelete
			my deleteFile(value of field "path" of oneRecord)
			delete oneRecord
		end repeat
		save
	end tell
	repeat with oneFile in added_items
		set fileName to name of (info for oneFile)
		set dateAdded to (current date)
		set filePOSIXpath to POSIX path of oneFile
		tell application "Database Events"
			tell dbase
				set newRecord to (make new record with properties {name:fileName})
				tell newRecord
					make new field with properties {name:"path", value:filePOSIXpath}
					make new field with properties {name:"date added", value:dateAdded}
				end tell
			end tell
		end tell
	end repeat
	tell application "Database Events"
		save
		delete every database -- doesn't delete anything, just closes the database
	end tell
end adding folder items to


on deleteFile(filePath)
	do shell script "/bin/rm -r " & quoted form of filePath
end deleteFile


Wow, I had visions of setting up a mysql database and trying to figure out how to get Applescript to communicate with it.

Are there any limitations on which version of OS X this will run on?

It should work at least on Tiger

Great script StefanK. You make it look so easy. The only thing I would add is that there’s other folder action handlers that you’d probably want to add to the script. For example you could add one that would get called whenever the folder was opened. It could check for old files and remove them. In addition you’d want an action taken when files are physically removed from the folder. Take a look here to see all the folder action handlers you can use.

Hi.

I never got around to post that you beat me to it even before I got started yesterday Stefan.

I just wonder wether this line is correct

    set recordsToDelete to every record whose value of field "date added" < twoWeeksAgo

Shouldn’t it be:

set recordsToDelete to every record whose value of field "date added" > twoWeeksAgo

I’ve been testing this, by setting the timescale to 2 minutes and it does the job perfectly. Thank you so much for helping out.

Just replace in mind

< with is older than
> with is newer then

There are also the synonyms comes before and comes after, which are nice to use in time contexts.

I’m feeling enlightened! :slight_smile:

It is still counter intuitive though. I haven’t replaced anything, but I will keep it in there :slight_smile:
I tried to figure out a better way to write it, but drew the conclusion that given the filtering statement (whose clause), and not doing any extra date calculations to enhance readability, it must be like that.

Is there any reason for this script not to work in 10.5? I’m struggling with it at the moment.

no, I wrote it in 10.5

Fair enough, it just seems to be a bit unpredictable. It might be the folder actions though, I have had problem with those before.

Do you think I could get it to run using Lingon?

Maybe it’s sufficient to run it once at startup, then remove the folder action,
save it as application and add it to the startup items.

But of course you can run the script scheduled by launchd (Lingon)

If I edit the script to specify a folder, then save it as an Application in the start up items, would that work?

Yes, of course, just change these lines


-- on adding folder items to this_folder after receiving added_items
set this_folder to alias "MacHD:path:to:folder:"
tell application "Finder" to set added_items to files of this_folder as alias list
-- .

--.
-- end adding folder items to

Thanks for your time on this script, I think Im doing something wrong though. This is where I am at the moment:

property databaseName : "folderManager"
set this_folder to alias "Wallop:delete:"
tell application "Finder" to set added_items to items of this_folder as alias list

on adding folder items to this_folder after receiving added_items
	set twoWeeksAgo to (current date) - 2 * minutes
	tell application "Database Events"
		try
			set dbase to database databaseName
		on error
			set dbase to (make new database with properties {name:databaseName})
		end try
		
		tell dbase
			set recordsToDelete to every item whose value of field "date added" < twoWeeksAgo
		end tell
		repeat with oneRecord in recordsToDelete
			my deleteFile(value of field "path" of oneRecord)
			delete oneRecord
		end repeat
		save
	end tell
	repeat with oneFile in added_items
		set fileName to name of (info for oneFile)
		set dateAdded to (current date)
		set filePOSIXpath to POSIX path of oneFile
		tell application "Database Events"
			tell dbase
				set newRecord to (make new record with properties {name:fileName})
				tell newRecord
					make new field with properties {name:"path", value:filePOSIXpath}
					make new field with properties {name:"date added", value:dateAdded}
				end tell
			end tell
		end tell
	end repeat
	tell application "Database Events"
		save
		delete every database -- doesn't delete anything, just closes the database
	end tell
end adding folder items to


on deleteFile(filePath)
	do shell script "/bin/rm -r " & quoted form of filePath
end deleteFile

But all I get in the result area is just a list of the files to be removed, but they dont get removed.

{alias "Wallop:delete:BrookeThor_Invite.jpg", alias "Wallop:delete:Dancing In The Moonlight Tab by Thin Lizzy tabs @ Ultimate Guitar Archive.pdf", alias "Wallop:delete:Invites:", alias "Wallop:delete:Outgoing.rtf", alias "Wallop:delete:images-2.jpeg", alias "Wallop:delete:images-3.jpeg", alias "Wallop:delete:images.jpeg", alias "Wallop:delete:untitled folder:", alias "Wallop:delete:untitled folder 2:", alias "Wallop:delete:untitled folder 3:", alias "Wallop:delete:untitled folder 4:", alias "Wallop:delete:untitled folder 5:", alias "Wallop:delete:untitled folder 6:", alias "Wallop:delete:untitled folder 7:", alias "Wallop:delete:wedding-inv-front.jpg"}

Also it only seems to run once? Any help is appreciated.

as described above you must remove or comment out these two lines


““ on adding folder items to this_folder after receiving added_items

-- end adding folder items to

I don’t know what I am doing wrong, I keep getting this result:

error "rm: /Users/liam/Desktop/test delete/Hiya copy/: No such file or directory" number 1

Thats a folder I was testing the script with last week, it doesn’t exist or isn’t referenced in the script. Is the database still there from last week do you think?

The database is located at ~/Documents/Databases/folderManager.dbev

The specified folder “Wallop:delete:” is not the same as the folder “/Users/liam/Desktop/test delete” in the database.
I recommend to delete the database and start over