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?
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
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.
It is still counter intuitive though. I haven’t replaced anything, but I will keep it in there
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.
-- 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.
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