Hello, is it possible to filter finder items by “date added”?
I want to take all the files in a folder that were added today, and then move the files to a folder based on their movie dimensions. Please help.
Chad
tell application "Finder" to set myFiles to every file of folder "MEDIA:IMPORT BIN" whose date added is greater than ((get current date) - 1 * days)
repeat with f in myFiles
tell application "System Events"
set mov_dem to natural dimensions of movie file (f as text)
end tell
if mov_dem is {0, 0, 1920, 1080} then
tell application "Finder" to duplicate f to folder "HD Files" of disk "MEDIA" with replacing
end if
end repeat
there is no date added property in the Finder’s dictionary, there’s only creation date and modification date.
Maybe one of them is sufficient for your purpose.
To monitor when a file has been added to a folder you could attach a folder action to the folder and write the timestamp into a database.
However this requires some code to maintain the database (Database Events or “ easier “ sqlite3)
I think you’ll get it right by looking a little at this snippet.
tell application "Finder"
if exists disk "Storage" then
set aMonthAgo to (current date) - (30 * days)
copy (every item in folder "Correspondence" of startup disk ¬
whose modification date < aMonthAgo) ¬
to folder "Old Letters" of disk "Storage"
delete (every item in folder "Correspondence" of startup disk ¬
whose modification date < aMonthAgo)
else
display dialog "The Storage cartridge isn't mounted."
end if
end tell
I’m not sure that the modification date of files added in a given period of time (last month in your proposal) are also in the last month.
Today, the last file added in my download folder was a zip file whose expanded result is dated 2014/08/10.
Yvan KOENIG (VALLAURIS, France) mercredi 4 mars 2015 22:11:06
The example moves all the files that are older than 30 days, then naturally a file with a modification date older than 30 days will be listed.
You address a very important issue however, how can we be sure that the modification date is less than 30 days or whatever, when we add the file to the folder. (Access time, is totally unusable, because then the file would have to be read.
The simple solution, is to get the posix path of the file, and use the touch utility, to update the modification date, when the item is added to the folder.
Here is a little folder action, that is supposed to do just that.
on adding folder items to this_folder after receiving added_items
try
repeat with i from 1 to (count added_items)
set pxPath to quoted form of POSIX path of (item i of added_items)
try
do shell script "/usr/bin/touch " & pxPath
end try
end repeat
end try
end adding folder items to
Edit
Surrounded the do shell script with a try block, -It is untested.
To confirm, the best way to filter a finder item is to create a folder action that changes the modification date of the file after it is dropped in the folder.
I tried the script from McUsrII, but I was unable to get it to work.
Can anyone else confirm that the script is working for them?
While I had the script open, I found a folder with Finder that I wanted to attach it with.
I rightclicked on the folder, went to the “Services menu”, there I choose the folder actions Item, I chose an item there, that I wanted to edit, so I got to the folder, where I should save the folder action, I saved it there, and then I attached it to the folder, by the folder actions setup.
I tested it, and it worked.
You may try the one below instead, to see if it generates an error message, and just for the record, this is a very bad idea for your download folder, since the folder action will be triggered before your file is saved, and that will probably generate an error message about a busy file, since we are trying to do things with the file, while it is busy!
on adding folder items to this_folder after receiving added_items
try
repeat with i from 1 to (count added_items)
try
set pxPath to quoted form of POSIX path of (item i of added_items)
on error e number n
tell me to display alert "get posix path: " & e & " : " & n
end try
try
do shell script "/usr/bin/touch " & pxPath
on error e number n
tell me to display alert "do shell script: " & e & " : " & n
end try
end repeat
end try
end adding folder items to
Assuming the OP is running Yosemite, it can be done without any need to mess with modification dates. The key is a new file attribute introduced in Yosemite, NSURLAddedToDirectoryDateKey. The docs say:
Anyway, that sounds like just what the OP wants. So:
use AppleScript version "2.4" -- requires Yosemite
use scripting additions
use framework "Foundation"
on copyNewFilesFrom:sourcePosixPath toFolder:destPosixPath
-- make file URLs of the source and destination folders
set sourceNSURL to current application's |NSURL|'s fileURLWithPath:sourcePosixPath
set destNSURL to current application's |NSURL|'s fileURLWithPath:destPosixPath
-- get date of one day ago
set theNSDate to current application's NSDate's dateWithTimeIntervalSinceNow:(-24 * 60 * 60)
-- get the file manager
set theNSFileManager to current application's NSFileManager's defaultManager()
-- get list of file URLs in source folder, getting their date added values at the same time, skipping hidden files
set allNSURLs to (theNSFileManager's contentsOfDirectoryAtURL:sourceNSURL includingPropertiesForKeys:{current application's NSURLAddedToDirectoryDateKey} options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)) as list
-- loop through the files
repeat with i from 1 to count of allNSURLs
set thisNSURL to item i of allNSURLs
-- get the date added
set {theResult, theValue, theError} to (thisNSURL's getResourceValue:(reference) forKey:(current application's NSURLAddedToDirectoryDateKey) |error|:(reference))
-- compare the date with that of one day ago
if theResult and (theValue's compare:theNSDate) as integer > -1 then
-- make HFS path for System Events
set hfsPath to (thisNSURL's |path|()) as text as POSIX file as text
tell application "System Events"
set mov_dem to natural dimensions of movie file hfsPath
end tell
if mov_dem is {0, 0, 1920, 1080} then
-- make full destination URL
set destFileURL to (destNSURL's URLByAppendingPathComponent:(thisNSURL's lastPathComponent()))
-- copy the file
(theNSFileManager's copyItemAtURL:thisNSURL toURL:destFileURL |error|:(missing value))
end if
end if
end repeat
end copyNewFilesFrom:toFolder:
Now that is a nice attribute and a nice handler! Hopefully the attribute is made in such a way that it evades the busy file problem, that is , it is written right then and there, before the file is closed.
That is maybe too much to hope for, but it is a leap forward in the right direction anyways.
Edit
While I am ranting away anyways, -truth is, I don’t use folder actions, for a variety of reasons! But, at least, the folder actions worked, so that an item was added before it was closed. A lot of problems would vanish, if a folder action wasn’t triggered with the “item added event”, before the file in question actually was closed.
That would make folder actions alot easier to relate to, but I guess there is a whole curmudgeon of inconsistencies that would arise, by trying to make them work that way.
Changing the modification date of a file is really not a clean soluce.
Believe it or no, when I receive files from different sources, I do my best to keep them with all their original attributes. If I must work upon them I do that on a copy, not on the original.
Yvan KOENIG (VALLAURIS, France) jeudi 5 mars 2015 09:40:20
No, it isn’t a clean solution at all. But there aren’t many quick alternatives to do so, reliably. One way would be to use the Spotlight metadata, I just don’t have the time to investigate that further, but Shane has posted a fairly good solution, if the OP uses Yosemite.